OpenTelemetry Integration

View as Markdown

The NVIDIA NeMo Guardrails library follows OpenTelemetry best practices. The library uses only the API, and the host application configures the SDK. The following sections explain how to install and configure the OpenTelemetry SDK.

Installation

Choose one of the following options for installing the library with tracing support, the OpenTelemetry SDK, and the OpenTelemetry Protocol (OTLP) exporter.

  • For basic tracing support in the NeMo Guardrails library:

    $pip install nemoguardrails[tracing]
  • For development with the OpenTelemetry SDK:

    $pip install nemoguardrails[tracing] opentelemetry-sdk
  • For production with the OpenTelemetry SDK and the OpenTelemetry Protocol (OTLP) exporter:

    $pip install nemoguardrails[tracing] opentelemetry-sdk opentelemetry-exporter-otlp

Configuration Examples

The following examples show how to configure the library with the OpenTelemetry SDK for development and production use cases.

Console Output (Development)

1from opentelemetry import trace
2from opentelemetry.sdk.trace import TracerProvider
3from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
4from opentelemetry.sdk.resources import Resource
5
6# Configure OpenTelemetry before NeMo Guardrails
7resource = Resource.create({"service.name": "my-guardrails-app"})
8tracer_provider = TracerProvider(resource=resource)
9trace.set_tracer_provider(tracer_provider)
10
11console_exporter = ConsoleSpanExporter()
12tracer_provider.add_span_processor(BatchSpanProcessor(console_exporter))
13
14# Configure NeMo Guardrails
15from nemoguardrails import LLMRails, RailsConfig
16
17config_yaml = """
18models:
19 - type: main
20 engine: openai
21 model: gpt-4o-mini
22
23tracing:
24 enabled: true
25 adapters:
26 - name: OpenTelemetry
27"""
28
29config = RailsConfig.from_content(yaml_content=config_yaml)
30
31rails = LLMRails(config)

OTLP Exporter (Production)

1from opentelemetry import trace
2from opentelemetry.sdk.trace import TracerProvider
3from opentelemetry.sdk.trace.export import BatchSpanProcessor
4from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
5from opentelemetry.sdk.resources import Resource
6
7resource = Resource.create({"service.name": "my-guardrails-app"})
8tracer_provider = TracerProvider(resource=resource)
9trace.set_tracer_provider(tracer_provider)
10
11otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True)
12tracer_provider.add_span_processor(BatchSpanProcessor(otlp_exporter))
13
14# Use with NeMo Guardrails as above

OpenTelemetry Ecosystem Compatibility

The library works with the entire OpenTelemetry ecosystem, including the following components.

ComponentExamples
ExportersJaeger, Zipkin, Prometheus, New Relic, Datadog, AWS X-Ray, and Google Cloud Trace.
CollectorsOpenTelemetry Collector and vendor-specific collectors.
BackendsAny system that accepts OpenTelemetry traces.

Refer to the OpenTelemetry Registry for the complete list.

Exporting Logs

To also forward guardrails Python log records into your OpenTelemetry backend with trace correlation, see OpenTelemetry Logs.