Skip to main content

Module telemetry

Module telemetry 

Source
Expand description

Telemetry: spans, metrics and the local AgentMetrics and AgentTrace an AgentResult carries.

The agent records tracing spans: invoke_agent <name> for an invocation, execute_agent_loop_cycle for each cycle, chat for each model call and execute_tool <tool> for each tool call, with gen_ai.* attributes as span fields. OTEL_SEMCONV_STABILITY_OPT_IN selects the conventions (gen_ai_latest_experimental, gen_ai_tool_definitions, gen_ai_span_attributes_only), and a Langfuse endpoint records message content as span attributes. Any tracing subscriber sees the spans.

Span fields carry names, ids and token counts only, so a fmt subscriber’s log lines stay small. Message content (prompts, messages, tool arguments and results, tool definitions) is recorded only on spans an OpenTelemetry layer exports.

With the otel feature the spans also carry that content, their message events, custom trace_attributes and exception records, the agent’s baggage_attributes apply, and the meter records the gen_ai.agent.* counters and histograms through the global meter provider. setup_tracer and setup_meter build and register providers with OTLP HTTP and console exporters; TracerGuard::try_init_subscriber installs a subscriber that exports the spans, or add TracerGuard::layer to the application’s own subscriber:

use strands::telemetry::{MeterConfig, TelemetryExporters, TracerConfig, setup_meter, setup_tracer};

let exporters = TelemetryExporters { otlp: true, console: false };
let tracer = setup_tracer(TracerConfig { exporters, ..Default::default() })?;
let meter = setup_meter(MeterConfig { exporters, ..Default::default() })?;
tracer.try_init_subscriber()?;
// ... run agents; the guards flush when dropped.

The spans are INFO spans with target strands::telemetry; a filter in front of the layer (an EnvFilter on the registry) must allow strands::telemetry=info, or give the layer a filter of its own.

§Versions

The otel feature exposes opentelemetry, opentelemetry_sdk, tracing_opentelemetry and tracing_subscriber types in the public API and re-exports those crates. A strands release that moves any of them to a new semver-incompatible version (a new 0.x minor) is a breaking release of strands; use the re-exports rather than your own dependencies so the versions always match.

Re-exports§

pub use opentelemetry;
pub use opentelemetry_sdk;
pub use tracing_opentelemetry;
pub use tracing_subscriber;

Structs§

AgentLoopMetricsData
Usage of one agent loop cycle (AgentLoopMetricsData).
AgentMetrics
Read-only snapshot of an agent’s aggregated loop metrics (AgentMetrics).
AgentTrace
A node in the local timing tree of an invocation (AgentTrace).
BaggageKeyPredicate
Selects baggage keys for the baggage span processor (BaggageKeyPredicate).
InvocationMetricsData
Metrics of one invocation (InvocationMetricsData).
MeterConfig
Configuration for setup_meter (MeterConfig).
MeterGuard
The meter provider setup_meter registered. Dropping the guard flushes the provider; shutdown flushes and stops it.
TelemetryExporters
Which exporters a provider gets.
ToolMetricsData
Per-tool execution metrics (ToolMetricsData).
ToolUsage
ToolMetricsData with computed averages, as returned by AgentMetrics::tool_usage.
TracerConfig
Configuration for setup_tracer (TracerConfig).
TracerGuard
The tracer provider setup_tracer registered. Dropping the guard flushes the provider; shutdown flushes and stops it.

Enums§

AttributeValue
The value of a span attribute (OpenTelemetry AttributeValue): a string, number or boolean, or a homogeneous array of them.
BaggageProcessor
Which baggage entries the tracer stamps onto every span as attributes.

Functions§

get_meter
A meter from the global provider, scoped to the service name, for custom metrics; a no-op until a provider is registered.
get_tracer
A tracer from the global provider, scoped to the service name, for custom spans.
setup_meter
Sets up and registers the global meter provider. A second call logs a warning and returns a guard for the provider already registered; once a guard has been shut down, the next call registers a new provider.
setup_tracer
Sets up and registers the global tracer provider.

Type Aliases§

BaggageKeyPredicates
The predicates of BaggageProcessor::Predicates (BaggageKeyPredicates); a key is stamped when any of them selects it.