Tracing & Observability¶
otel ¶
OpenTelemetry integration for Loom distributed tracing.
All public functions in this module are safe to call without
opentelemetry installed — they degrade to no-ops. This lets
production code instrument unconditionally while making OTel an
optional dependency.
Trace context propagation uses W3C traceparent format, injected into
NATS message dicts under the _trace_context key.
GenAI semantic conventions ~~~~~~~~~~~~~~~~~~~~~~~~~~
LLM call spans (llm.call) in worker/runner.py follow the emerging
OTel GenAI semantic conventions for attribute naming:
gen_ai.system— provider identifier (anthropic,ollama,openai)gen_ai.request.model/gen_ai.response.model— model namesgen_ai.usage.input_tokens/gen_ai.usage.output_tokens— token countsgen_ai.request.temperature/gen_ai.request.max_tokens— request params
When LOOM_TRACE_CONTENT=1, prompt and completion text are recorded as
span events (gen_ai.content.prompt, gen_ai.content.completion).
See: https://opentelemetry.io/docs/specs/semconv/gen-ai/
Legacy llm.* attributes are preserved for backward compatibility.
Setup::
from loom.tracing import init_tracing
init_tracing("loom-pipeline", endpoint="http://localhost:4317")
init_tracing ¶
Initialize OTel tracing with OTLP exporter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service_name
|
str
|
Service name reported to the collector. |
'loom'
|
endpoint
|
str | None
|
OTLP gRPC endpoint (e.g. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/loom/tracing/otel.py
get_tracer ¶
Get a tracer instance (real or no-op depending on OTel availability).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Instrumentation scope name (e.g. |
'loom'
|
Returns:
| Type | Description |
|---|---|
Any
|
An OTel |
Source code in src/loom/tracing/otel.py
inject_trace_context ¶
Inject current trace context into a message dict.
Adds a _trace_context key containing W3C propagation headers.
Safe to call without OTel installed (no-op).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
carrier
|
dict[str, Any]
|
Message dict (modified in-place). |
required |
Source code in src/loom/tracing/otel.py
extract_trace_context ¶
Extract trace context from a message dict.
Reads the _trace_context key and returns an OTel Context
that can be passed to tracer.start_as_current_span(context=...).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
carrier
|
dict[str, Any]
|
Message dict with optional |
required |
Returns:
| Type | Description |
|---|---|
Any
|
An OTel |