feat(tracing): add governance trace enrichment for regulated industries
Description
Adds governance trace enrichment to NeMo Guardrails' OpenTelemetry tracing system.
Problem
NeMo Guardrails' current OTel traces capture operational metrics (LLM calls, token usage, latency, rail execution times) but lack governance-relevant metadata. Compliance teams in regulated industries (insurance, finance, healthcare) need to know: which policy was evaluated, what the decision was, why it was made, and whether any security threats were detected.
Solution
This PR adds three components as new, additive files only — no existing files are modified.
1. Governance Semantic Conventions (nemoguardrails/tracing/governance_conventions.py)
Custom OTel span attributes following the guardrails.governance.*, guardrails.security.*, guardrails.compliance.*, and guardrails.risk.* namespaces:
- Policy enforcement: decision (allow/deny/modify/escalate), rule_id, reason, category, position, confidence
- Security: threat_detected, threat_type, severity, detection_method, detection_confidence
- Escalation: triggered, reason, priority, queue
- Compliance: domain, audit_complete, rails_evaluated/passed/failed, failed_rail_names
- Risk: risk_score, violation_count, violation_severity
2. Governance Trace Enricher (nemoguardrails/tracing/governance_enricher.py)
A GovernanceTraceEnricher class with methods:
record_governance_decision()— enrich spans with policy decisionsrecord_security_threat()— add security alert events with severity trackingrecord_escalation()— record human-in-the-loop escalations with priorityrecord_pii_detection()— log PII detection events (entity types only, no PII values)finalise_compliance_summary()— add aggregate audit metrics and computed risk score to root span
Uses only the OTel API (not SDK), following the same library-best-practice pattern as the existing OpenTelemetryAdapter.
3. Tests and Example
- 56 unit tests with 100% pass rate covering all enrichment methods, helper functions, edge cases, and two end-to-end scenarios (jailbreak denial and PII anonymisation)
- End-to-end Jaeger integration example with three interaction scenarios (clean query, PII, jailbreak),
config.yaml, and a README with Grafana/Datadog query examples
Motivation
This enrichment enables organisations using NeMo Guardrails in regulated environments to:
- Monitor governance decisions in Datadog, Grafana, Jaeger, or any OTel-compatible backend
- Build compliance dashboards showing policy enforcement rates
- Alert on security threats detected during guardrails evaluation
- Maintain audit trails for regulatory requirements (NIST AI RMF, EU AI Act)
Type of Change
- [x] New feature (non-breaking change which adds functionality)
- [ ] Bug fix
- [ ] Documentation update
New Files
nemoguardrails/tracing/governance_conventions.py (269 lines)
nemoguardrails/tracing/governance_enricher.py (495 lines)
tests/tracing/test_governance_enrichment.py (771 lines)
examples/governance_tracing/README.md
examples/governance_tracing/config.yaml
examples/governance_tracing/governance_tracing_example.py
Checklist
- [x] Code follows Black formatting
- [x] New tests added and passing (56/56)
- [x] Example included demonstrating the feature
- [x] No existing files modified — purely additive
- [x] No existing tests broken
Codecov Report
:x: Patch coverage is 98.84393% with 2 lines in your changes missing coverage. Please review.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| nemoguardrails/tracing/governance_enricher.py | 98.19% | 2 Missing :warning: |
:loudspeaker: Thoughts on this report? Let us know!
Greptile Summary
This PR adds governance trace enrichment capabilities to NeMo Guardrails' OpenTelemetry tracing system, enabling compliance teams in regulated industries to monitor policy enforcement, security threats, and audit trails through standard observability tools.
Key additions:
- Semantic conventions (
governance_conventions.py) defining span attributes for policy decisions, security threats, escalations, compliance metrics, and risk scoring - Trace enricher (
governance_enricher.py) providing methods to record governance decisions, security threats, PII detection, and compliance summaries on OTel spans - Comprehensive test coverage (56 tests) with 100% pass rate including unit tests and end-to-end scenarios
- Complete example with Jaeger integration demonstrating three scenarios: clean query, PII detection, and jailbreak attempt
Implementation quality:
- Follows existing OpenTelemetry adapter pattern (uses API only, not SDK)
- Properly handles sensitive data (logs entity types but not actual PII values)
- All new files - no modifications to existing code
- Well-documented with clear examples and integration guidance
Minor issue found:
- Documentation inaccuracy in
_now_ns()function comment (says "monotonic wall clock" when it's just "wall clock")
Confidence Score: 5/5
- Safe to merge - purely additive feature with no impact on existing functionality
- All files are new additions with no modifications to existing code, comprehensive test coverage (56 tests passing), follows established patterns, and only contains a minor documentation issue
- No files require special attention - only a trivial documentation comment fix in
governance_enricher.py
Important Files Changed
| Filename | Overview |
|---|---|
| nemoguardrails/tracing/governance_conventions.py | Defines OpenTelemetry semantic conventions for governance metadata - clean constant definitions with proper namespace organization |
| nemoguardrails/tracing/governance_enricher.py | Core enricher implementation with governance decision tracking, security threat recording, and compliance summaries - minor doc issue in _now_ns() comment |
| tests/tracing/test_governance_enrichment.py | Comprehensive test suite with 56 tests covering all enrichment methods, helper functions, and two end-to-end scenarios |
| examples/governance_tracing/governance_tracing_example.py | Well-structured demo with three scenarios (clean query, PII detection, jailbreak) showing governance enrichment with Jaeger export |
Sequence Diagram
sequenceDiagram
participant App as Application
participant Tracer as OTel Tracer
participant Enricher as GovernanceTraceEnricher
participant Rails as NeMo Guardrails
participant Backend as OTel Backend
App->>Tracer: Configure TracerProvider & Exporter
App->>Enricher: Create enricher(domain="insurance")
App->>Tracer: start_as_current_span("interaction")
activate Tracer
App->>Rails: generate_async(messages)
activate Rails
Rails->>Rails: Evaluate rails (jailbreak, PII, etc)
Rails-->>App: Response or denial
deactivate Rails
Note over App,Enricher: Enrich span with governance metadata
App->>Enricher: record_security_threat(span, threat_type, severity)
Enricher->>Tracer: set_attribute(security.threat_detected)
Enricher->>Tracer: add_event(security.threat_detected)
App->>Enricher: record_governance_decision(span, decision, rule_id)
Enricher->>Tracer: set_attribute(governance.decision)
Enricher->>Tracer: add_event(governance.decision)
App->>Enricher: record_escalation(span, reason, priority)
Enricher->>Tracer: set_attribute(escalation.triggered)
Enricher->>Tracer: add_event(escalation.triggered)
App->>Enricher: finalise_compliance_summary(span, rails_evaluated)
Enricher->>Enricher: Compute risk score
Enricher->>Tracer: set_attribute(compliance.domain)
Enricher->>Tracer: set_attribute(risk.risk_score)
App->>Tracer: End span
deactivate Tracer
Tracer->>Backend: Export enriched trace
Backend->>Backend: Store for compliance audit/dashboards
Last reviewed commit: 1abfec7
Hi @tgasser-nv - would you mind reviewing when you get a chance?
Reading this PR after engaging on issue #1786 (audit/compliance export of guardrail decisions) — the two address the same regulated-industries gap from complementary angles. This PR enriches spans with governance attributes in the existing OTel pipeline; #1786 proposes per decision records via the adapter system. If both land, the audit-record approach could consume the enriched spans defined here rather than re deriving policy/decision/confidence signals — worth coordinating namespace and field shapes either way.
One question on the architecture: how does GovernanceTraceEnricher get invoked at runtime? From the description it sounds like it could be either (a) opt-in via configuration, with NeMo wiring up record_governance_decision() etc. at known rail-evaluation points (similar to how OpenTelemetryAdapter plugs in via TracingConfig), or (b) explicit caller-side enrichment that users invoke from their own code. The example folder suggests (a), but the PR body reads closer to (b). Could you confirm the intended use? The answer affects which integration points downstream tooling (audit adapters, compliance dashboards, red-team eval pipelines) would hook into.
Minor note: .claude/launch.json (23 lines) looks like an accidentally committed IDE config , probably worth removing in a cleanup commit.