Skip to main content

AuditRecord

Struct AuditRecord 

Source
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: String

ISO 8601 timestamp with millisecond precision and Z suffix.

§request_id: String

The unique request ID (UUID v4).

§decision: &'static str

Final 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: String

The model the request was destined for.

§provider: String

The provider (lowercase).

§message_count: usize

Number 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: f64

Pipeline-only evaluation latency in milliseconds.

§latency_total_ms: f64

End-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

Source

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"]);
Source

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"]);
Source

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"));
Source

pub fn with_stage(self, stage: impl Into<String>) -> Self

Set the pipeline stage that produced the decision. Returns self for chaining.

Source

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AuditRecord

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Serialize for AuditRecord

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Forget for AuditRecord

§

impl Freeze for AuditRecord

§

impl Move for AuditRecord

§

impl RefUnwindSafe for AuditRecord

§

impl Send for AuditRecord
where RawVecInner: Send, &'static str: Send,

§

impl Sync for AuditRecord
where RawVecInner: Sync,

§

impl Unpin for AuditRecord
where *const u8 is !null: Unpin, &'static str: Unpin,

§

impl UnsafeUnpin for AuditRecord

§

impl UnwindSafe for AuditRecord
where RawVecInner: UnwindSafe, &'static str: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more