Skip to main content

strands/telemetry/
mod.rs

1//! Telemetry: spans, metrics and the local [`AgentMetrics`] and [`AgentTrace`] an
2//! [`AgentResult`](crate::AgentResult) carries.
3//!
4//! The agent records `tracing` spans: `invoke_agent <name>` for an invocation,
5//! `execute_agent_loop_cycle` for each cycle, `chat` for each model call and `execute_tool <tool>`
6//! for each tool call, with `gen_ai.*` attributes as span fields. `OTEL_SEMCONV_STABILITY_OPT_IN`
7//! selects the conventions (`gen_ai_latest_experimental`, `gen_ai_tool_definitions`,
8//! `gen_ai_span_attributes_only`), and a Langfuse endpoint records message content as span
9//! attributes. Any `tracing` subscriber sees the spans.
10//!
11//! Span fields carry names, ids and token counts only, so a `fmt` subscriber's log lines stay
12//! small. Message content (prompts, messages, tool arguments and results, tool definitions) is
13//! recorded only on spans an OpenTelemetry layer exports.
14//!
15//! With the `otel` feature the spans also carry that content, their message events, custom
16//! [`trace_attributes`](crate::AgentConfig::trace_attributes) and exception records, the agent's
17//! [`baggage_attributes`](crate::AgentConfig::baggage_attributes) apply, and the meter records
18//! the `gen_ai.agent.*` counters and histograms through the global meter provider.
19//! [`setup_tracer`] and [`setup_meter`] build and register providers with OTLP HTTP and console
20//! exporters; [`TracerGuard::try_init_subscriber`] installs a subscriber that exports the spans,
21//! or add [`TracerGuard::layer`] to the application's own subscriber:
22//!
23//! ```no_run
24//! # #[cfg(feature = "otel")]
25//! # fn main() -> strands::Result<()> {
26//! use strands::telemetry::{MeterConfig, TelemetryExporters, TracerConfig, setup_meter, setup_tracer};
27//!
28//! let exporters = TelemetryExporters { otlp: true, console: false };
29//! let tracer = setup_tracer(TracerConfig { exporters, ..Default::default() })?;
30//! let meter = setup_meter(MeterConfig { exporters, ..Default::default() })?;
31//! tracer.try_init_subscriber()?;
32//! // ... run agents; the guards flush when dropped.
33//! # Ok(())
34//! # }
35//! # #[cfg(not(feature = "otel"))]
36//! # fn main() {}
37//! ```
38//!
39//! The spans are `INFO` spans with target `strands::telemetry`; a filter in front of the layer
40//! (an `EnvFilter` on the registry) must allow `strands::telemetry=info`, or give the layer a
41//! filter of its own.
42//!
43//! # Versions
44//!
45//! The `otel` feature exposes [`opentelemetry`], [`opentelemetry_sdk`], [`tracing_opentelemetry`]
46//! and [`tracing_subscriber`] types in the public API and re-exports those crates. A strands
47//! release that moves any of them to a new semver-incompatible version (a new `0.x` minor) is a
48//! breaking release of strands; use the re-exports rather than your own dependencies so the
49//! versions always match.
50//!
51// The `otel` items exist only with the feature; without it the links go to the feature list.
52#![cfg_attr(
53    feature = "otel",
54    doc = "[`setup_tracer`]: setup_tracer\n[`setup_meter`]: setup_meter\n\
55           [`TracerGuard::try_init_subscriber`]: TracerGuard::try_init_subscriber\n\
56           [`TracerGuard::layer`]: TracerGuard::layer\n[`opentelemetry`]: opentelemetry\n\
57           [`opentelemetry_sdk`]: opentelemetry_sdk\n[`tracing_opentelemetry`]: tracing_opentelemetry\n\
58           [`tracing_subscriber`]: tracing_subscriber"
59)]
60#![cfg_attr(
61    not(feature = "otel"),
62    doc = "[`setup_tracer`]: crate#features\n[`setup_meter`]: crate#features\n\
63           [`TracerGuard::try_init_subscriber`]: crate#features\n[`TracerGuard::layer`]: crate#features\n\
64           [`opentelemetry`]: crate#features\n[`opentelemetry_sdk`]: crate#features\n\
65           [`tracing_opentelemetry`]: crate#features\n[`tracing_subscriber`]: crate#features"
66)]
67
68mod baggage;
69#[cfg(feature = "otel")]
70mod config;
71mod meter;
72mod span;
73mod tracer;
74mod types;
75mod utils;
76
77#[cfg(test)]
78#[cfg(feature = "otel")]
79pub(crate) mod test_support;
80#[cfg(test)]
81mod tests;
82
83pub(crate) use baggage::{in_current_context, with_baggage};
84#[cfg(feature = "otel")]
85pub use config::{
86    BaggageKeyPredicate, BaggageKeyPredicates, BaggageProcessor, MeterConfig, MeterGuard, TelemetryExporters,
87    TracerConfig, TracerGuard, get_meter, get_tracer, setup_meter, setup_tracer,
88};
89pub use meter::{AgentLoopMetricsData, AgentMetrics, InvocationMetricsData, ToolMetricsData, ToolUsage};
90pub(crate) use meter::{Cycle, Meter};
91pub(crate) use span::InSpan;
92pub use tracer::AgentTrace;
93pub(crate) use tracer::{
94    AgentSpan, AgentSpanContext, EndAgentSpanOptions, EndMemoryAddSpanOptions, EndMemoryExtractSpanOptions,
95    EndMemoryInjectSpanOptions, EndMemorySearchSpanOptions, EndModelSpanOptions, EndToolCallSpanOptions, ModelSpan,
96    StartAgentSpanOptions, StartMemoryAddSpanOptions, StartMemoryExtractSpanOptions, StartMemoryInjectSpanOptions,
97    StartMemorySearchSpanOptions, StartModelInvokeSpanOptions, ToolSpan, Tracer,
98};
99pub use types::AttributeValue;
100
101/// The `opentelemetry` crate the SDK is built against; use it (not your own dependency) for
102/// contexts and values, so the versions match.
103#[cfg(feature = "otel")]
104pub use opentelemetry;
105/// The `opentelemetry_sdk` crate the SDK is built against, for custom providers.
106#[cfg(feature = "otel")]
107pub use opentelemetry_sdk;
108/// The `tracing-opentelemetry` crate the SDK is built against, for custom layers.
109#[cfg(feature = "otel")]
110pub use tracing_opentelemetry;
111/// The `tracing-subscriber` crate the SDK's layer is built for, to compose the subscriber the
112/// layer goes into.
113#[cfg(feature = "otel")]
114pub use tracing_subscriber;