pub struct AuditRecord {Show 14 fields
pub timestamp: String,
pub request_id: String,
pub decision: &'static str,
pub stage: Option<String>,
pub reason: Option<String>,
pub code: Option<String>,
pub score: Option<f32>,
pub model: String,
pub provider: String,
pub message_count: usize,
pub pii_entities_found: Vec<String>,
pub latency_pipeline_ms: f64,
pub latency_total_ms: f64,
pub failed_open_stages: Vec<String>,
}Expand description
A structured audit record for a single pipeline decision.
This type is serializable to JSON for log shipping. The shape matches
§10 of the spec exactly, including optional fields that are only present
when relevant (e.g. score only for ONNX decisions, pii_entities_found
only for redaction decisions).
Fields§
§timestamp: StringISO 8601 timestamp with millisecond precision and Z suffix.
request_id: StringThe unique request ID (UUID v4).
decision: &'static strFinal pipeline decision: "allow", "redact", or "block".
stage: Option<String>The name of the stage that produced this decision. None for
"allow" decisions where all stages passed.
reason: Option<String>Human-readable reason string. None for "allow" decisions.
code: Option<String>Machine-readable block code ("prompt_injection", "toxicity",
"policy_violation", …). Present only on "block" decisions.
score: Option<f32>Classifier confidence score in [0, 1]. Present only when an ONNX
classifier made the decision.
model: StringThe model the request was destined for.
provider: StringThe provider (lowercase).
message_count: usizeNumber of messages in the request.
pii_entities_found: Vec<String>PII entity types found, e.g. ["email", "phone"]. Empty for non-redact decisions.
latency_pipeline_ms: f64Pipeline-only evaluation latency in milliseconds.
latency_total_ms: f64End-to-end request latency including upstream, in milliseconds.
failed_open_stages: Vec<String>Stages that errored on this request and failed open (on_error = "allow"): the request went on without their check. Empty when every
stage ran normally. Omitted from JSON when empty.
Implementations§
Source§impl AuditRecord
impl AuditRecord
Sourcepub fn from_decision(
req: &GuardrailRequest,
decision: &Decision,
latency_pipeline_ms: f64,
latency_total_ms: f64,
) -> Self
pub fn from_decision( req: &GuardrailRequest, decision: &Decision, latency_pipeline_ms: f64, latency_total_ms: f64, ) -> Self
Build an audit record from a request and its final decision.
§Examples
use guardrail_proxy::audit::AuditRecord;
use guardrail_core::{decision::{Decision, BlockCode}, test_helpers::clean_request};
let req = clean_request();
let record = AuditRecord::from_decision(&req, &Decision::Allow, 0.05, 120.0);
assert_eq!(record.decision, "allow");
assert!(record.code.is_none());PII entity types are read directly from Decision::Redact’s own
entities field — there is no separate parameter to thread through
(and therefore no way to pass a list that doesn’t match the actual
decision, which was a real bug in an earlier version of this API).
use guardrail_proxy::audit::AuditRecord;
use guardrail_core::{decision::Decision, test_helpers::clean_request};
let req = clean_request();
let decision = Decision::Redact {
reason: "PII redacted: Email".into(),
mutated: req.clone(),
entities: vec!["email".to_string()],
};
let record = AuditRecord::from_decision(&req, &decision, 0.02, 250.0);
assert_eq!(record.pii_entities_found, vec!["email"]);Sourcepub fn with_failed_open_stages(self, stages: Vec<String>) -> Self
pub fn with_failed_open_stages(self, stages: Vec<String>) -> Self
Record the stages that errored and failed open on this request.
Returns self for chaining.
§Examples
use guardrail_proxy::audit::AuditRecord;
use guardrail_core::{decision::Decision, test_helpers::clean_request};
let record = AuditRecord::from_decision(&clean_request(), &Decision::Allow, 0.1, 50.0)
.with_failed_open_stages(vec!["toxicity".to_string()]);
assert_eq!(record.failed_open_stages, vec!["toxicity"]);Sourcepub fn with_score(self, score: f32) -> Self
pub fn with_score(self, score: f32) -> Self
Set the ONNX classifier score on a record already constructed with
from_decision. Returns self for chaining.
§Examples
use guardrail_proxy::audit::AuditRecord;
use guardrail_core::{decision::{Decision, BlockCode}, test_helpers::clean_request};
let req = clean_request();
let record = AuditRecord::from_decision(
&req,
&Decision::Block { reason: "test".into(), code: BlockCode::PromptInjection },
0.8,
312.0,
)
.with_score(0.97)
.with_stage("onnx_injection");
assert_eq!(record.score, Some(0.97));
assert_eq!(record.stage.as_deref(), Some("onnx_injection"));Sourcepub fn with_stage(self, stage: impl Into<String>) -> Self
pub fn with_stage(self, stage: impl Into<String>) -> Self
Set the pipeline stage that produced the decision. Returns self for
chaining.
Sourcepub fn emit(&self)
pub fn emit(&self)
Emit this record as a structured tracing event at info level
under the guardrail::audit target.
The tracing event fields are designed to be captured by the NDJSON
audit-log writer in crate::audit_log and also appear in the
structured JSON application log when log_format = "json".
Trait Implementations§
Source§impl Clone for AuditRecord
impl Clone for AuditRecord
Source§impl Debug for AuditRecord
impl Debug for AuditRecord
Auto Trait Implementations§
impl Forget for AuditRecord
impl Freeze for AuditRecord
impl Move for AuditRecord
impl RefUnwindSafe for AuditRecord
impl Send for AuditRecord
impl Sync for AuditRecordwhere
RawVecInner: Sync,
impl Unpin for AuditRecord
impl UnsafeUnpin for AuditRecordwhere
*const u8 is !null: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
&'static str: UnsafeUnpin,
PhantomData<String>: UnsafeUnpin,
impl UnwindSafe for AuditRecord
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request