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§
- Agent
Loop Metrics Data - Usage of one agent loop cycle (
AgentLoopMetricsData). - Agent
Metrics - Read-only snapshot of an agent’s aggregated loop metrics (
AgentMetrics). - Agent
Trace - A node in the local timing tree of an invocation (
AgentTrace). - Baggage
KeyPredicate - Selects baggage keys for the baggage span processor (
BaggageKeyPredicate). - Invocation
Metrics Data - Metrics of one invocation (
InvocationMetricsData). - Meter
Config - Configuration for
setup_meter(MeterConfig). - Meter
Guard - The meter provider
setup_meterregistered. Dropping the guard flushes the provider;shutdownflushes and stops it. - Telemetry
Exporters - Which exporters a provider gets.
- Tool
Metrics Data - Per-tool execution metrics (
ToolMetricsData). - Tool
Usage ToolMetricsDatawith computed averages, as returned byAgentMetrics::tool_usage.- Tracer
Config - Configuration for
setup_tracer(TracerConfig). - Tracer
Guard - The tracer provider
setup_tracerregistered. Dropping the guard flushes the provider;shutdownflushes and stops it.
Enums§
- Attribute
Value - The value of a span attribute (OpenTelemetry
AttributeValue): a string, number or boolean, or a homogeneous array of them. - Baggage
Processor - 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§
- Baggage
KeyPredicates - The predicates of
BaggageProcessor::Predicates(BaggageKeyPredicates); a key is stamped when any of them selects it.