OpenTelemetry Integration¶
OpenTelemetry tracing and metrics.
Prerequisites¶
Quick Example¶
from mamba_agents.observability import get_otel_integration
otel = get_otel_integration()
# Initialize
if otel.initialize():
print("OpenTelemetry initialized")
# Trace operations
with otel.trace_agent_run(prompt, model="gpt-4o") as span:
result = await agent.run(prompt)
span.set_attribute("tokens", result.usage().total_tokens)
Exporters¶
Jaeger¶
from opentelemetry.exporter.jaeger.thrift import JaegerExporter
from opentelemetry.sdk.trace.export import BatchSpanProcessor
otel.add_span_processor(BatchSpanProcessor(
JaegerExporter(agent_host_name="localhost", agent_port=6831)
))
OTLP¶
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import BatchSpanProcessor
otel.add_span_processor(BatchSpanProcessor(
OTLPSpanExporter(endpoint="https://otlp.example.com:4317")
))
Configuration¶
from mamba_agents.config import ObservabilityConfig
config = ObservabilityConfig(
enable_tracing=True,
service_name="my-agent",
)
API Reference¶
OTelIntegration
¶
OpenTelemetry integration for the agent.
Provides hooks for tracing agent operations when OpenTelemetry is available.
Initialize OpenTelemetry integration.
| PARAMETER | DESCRIPTION |
|---|---|
service_name
|
Service name for tracing.
TYPE:
|
Source code in src/mamba_agents/observability/otel.py
initialize
¶
Initialize OpenTelemetry if available.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if OpenTelemetry was initialized. |
Source code in src/mamba_agents/observability/otel.py
trace_agent_run
¶
trace_agent_run(
prompt: str, model: str | None = None
) -> Generator[SpanProtocol | NoOpSpan, None, None]
Create a span for an agent run.
| PARAMETER | DESCRIPTION |
|---|---|
prompt
|
The user prompt.
TYPE:
|
model
|
Model being used.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
SpanProtocol | NoOpSpan
|
Span for the agent run. |
Source code in src/mamba_agents/observability/otel.py
trace_tool_call
¶
trace_tool_call(
tool_name: str, args: dict[str, Any] | None = None
) -> Generator[SpanProtocol | NoOpSpan, None, None]
Create a span for a tool call.
| PARAMETER | DESCRIPTION |
|---|---|
tool_name
|
Name of the tool.
TYPE:
|
args
|
Tool arguments.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
SpanProtocol | NoOpSpan
|
Span for the tool call. |
Source code in src/mamba_agents/observability/otel.py
trace_model_request
¶
trace_model_request(
model: str, token_count: int | None = None
) -> Generator[SpanProtocol | NoOpSpan, None, None]
Create a span for a model request.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Model name.
TYPE:
|
token_count
|
Input token count.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
SpanProtocol | NoOpSpan
|
Span for the model request. |
Source code in src/mamba_agents/observability/otel.py
record_usage
¶
Record token usage as metrics.
| PARAMETER | DESCRIPTION |
|---|---|
input_tokens
|
Number of input tokens.
TYPE:
|
output_tokens
|
Number of output tokens.
TYPE:
|
model
|
Model used.
TYPE:
|
Source code in src/mamba_agents/observability/otel.py
get_otel_integration
¶
get_otel_integration() -> OTelIntegration
Get the global OpenTelemetry integration.
| RETURNS | DESCRIPTION |
|---|---|
OTelIntegration
|
OTelIntegration instance. |