Automatically creates spans for NATS publish, subscribe, request-reply, and JetStream operations with distributed trace context propagation through message headers.
The instrumented library (nats-py) is an optional dependency. Install it separately if you haven't already:
pip install nats-py
Quick Start
import asyncio
import nats
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from otel_instrumentation_nats import NatsInstrumentor
async def main():
# Set up OpenTelemetry
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(provider)
# Instrument nats-py -- call this before using the NATS client
NatsInstrumentor().instrument()
nc = await nats.connect("nats://localhost:4222")
# This publish creates a PRODUCER span and injects
# trace context into the message headers automatically
await nc.publish("orders.new", b'{"item": "widget"}')
await nc.close()
asyncio.run(main())
What Gets Instrumented
Method
Span Kind
Span Name
Description
Client.publish()
PRODUCER
{subject} send
Fire-and-forget publish
Client.subscribe()
CONSUMER
{subject} receive
Per-message spans for both callback and next_msg() patterns
Trace context is automatically propagated through NATS message headers using the W3C TraceContext format. This means:
Publishers inject traceparent and tracestate into message headers
Subscribers extract trace context from incoming message headers and create child spans
This links producer and consumer spans into a single distributed trace, even across service boundaries.
Service A NATS Service B
───────── ──── ─────────
publish("orders.new") ──────► message with ──────► subscribe callback
│ traceparent header │
▼ ▼
[PRODUCER span] [CONSUMER span]
trace_id: abc123 trace_id: abc123
span_id: def456 parent_id: def456
Usage Patterns
Publish/Subscribe with Callback
NatsInstrumentor().instrument()
nc = await nats.connect()
async def on_message(msg):
# A CONSUMER span is active here, linked to the producer trace
print(f"Got {msg.data} on {msg.subject}")
await nc.subscribe("events.>", cb=on_message)
await nc.publish("events.click", b"data") # Creates a PRODUCER span
Subscribe with next_msg() (Pull Pattern)
sub = await nc.subscribe("tasks")
await nc.publish("tasks", b"do-something")
msg = await sub.next_msg(timeout=5.0) # Creates a CONSUMER span
Request-Reply
async def handler(msg):
await msg.respond(b"pong")
await nc.subscribe("ping", cb=handler)
# Creates a CLIENT span that encompasses the full request-reply cycle
response = await nc.request("ping", b"data", timeout=2.0)
JetStream
js = nc.jetstream()
await js.add_stream(name="ORDERS", subjects=["orders.>"])
# Creates a PRODUCER span with stream attributes
await js.publish("orders.new", b"order-data", stream="ORDERS")
# Subscribe callback receives CONSUMER spans linked to the producer
async def process_order(msg):
await msg.ack()
await js.subscribe("orders.new", cb=process_order, manual_ack=True)
Custom Tracer Provider
from opentelemetry.sdk.trace import TracerProvider
provider = TracerProvider()
# ... configure your provider with exporters, processors, etc.
NatsInstrumentor().instrument(tracer_provider=provider)
Uninstrumenting
instrumentor = NatsInstrumentor()
instrumentor.instrument()
# ... later, to restore original behavior:
instrumentor.uninstrument()