# rig-otel
OpenTelemetry for [Rig](https://github.com/0xPlaygrounds/rig) agents in one call.
Rig already describes every agent run, model call and tool call as `tracing` spans that
follow the OpenTelemetry GenAI semantic conventions. `rig-otel` exports them, and adds
what a backend needs beyond the spans:
| | |
|---|---|
| **Traces** | OTLP over HTTP to any collector (Jaeger, Grafana Tempo, Honeycomb, Langfuse, Phoenix, AWS CloudWatch), from the standard `OTEL_*` variables. |
| **Metrics** | `gen_ai.client.operation.duration` and `gen_ai.client.token.usage`, with the semantic conventions' buckets, plus tool and model-request metrics (`rig.tool.*`, `rig.model.*`), read from Rig's spans: no agent code changes. |
| **Sessions** | `set_session` gives a span and everything under it a `session.id`, which travels as W3C baggage to the services it calls. |
| **Across processes** | `inject` and `extract` carry `traceparent` and `baggage` in HTTP headers, so an agent calling another (A2A, MCP) makes one trace. |
Dependencies for the first example:
```toml
[dependencies]
rig-otel = "0.47"
tracing = "0.1"
tracing-opentelemetry = "0.33"
http = "1"
```
```rust
use rig_agent::Agent;
use tracing::Instrument;
// At the start of main. Keep the guard until main returns: dropping it flushes.
fn setup() -> Result<rig_otel::Telemetry, rig_otel::InitError> {
// OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_SERVICE_NAME, OTEL_EXPORTER_OTLP_HEADERS... as usual.
rig_otel::init()
}
// A conversation: the agent run and every span under it carry session.id.
async fn converse(
agent: Agent,
session_id: String,
message: String,
) -> Result<String, Box<dyn std::error::Error>> {
let span = rig_otel::agent_span(agent.name().unwrap_or("agent"));
rig_otel::set_session(&span, session_id, None)?;
let answer = async { agent.prompt(message).await }.instrument(span).await?;
Ok(answer.to_string())
}
// Calling another service: it continues this trace and session.
fn call_out(request: &mut http::Request<()>) {
rig_otel::inject(request.headers_mut());
}
// Serving a call: continue the caller's trace.
fn serve_call(span: &tracing::Span, request: &http::Request<()>) -> Result<(), tracing_opentelemetry::SetParentError> {
use tracing_opentelemetry::OpenTelemetrySpanExt;
span.set_parent(rig_otel::extract(request.headers()))
}
```
Use `agent_span` for the outer span of a run, as above. It is named `invoke_agent <agent>`
and carries `gen_ai.operation.name = invoke_agent` and `gen_ai.agent.name`, which is what
GenAI backends look for. Rig creates its own `invoke_agent` span only when no span is
enabled around the run. Inside an enabled outer span, such as `info_span!("conversation")`,
Rig adopts that span as the run's, so no `invoke_agent` span is exported. With `agent_span`
the run's span is `invoke_agent`, but Rig does not record the run's prompt, answer or usage
on it.
`init()` exports to the endpoint in `OTEL_EXPORTER_OTLP_ENDPOINT`, or to the OTLP HTTP
default `http://localhost:4318` when that is unset. Starting without a collector does not
fail: exports run in the background and fail there. `force_flush()` returns the export
error. Dropping `Telemetry` ignores it, so call `force_flush()` where delivery matters.
Metrics go to the collector's metrics endpoint: a collector that stores traces only, such
as Jaeger, rejects them, while traces still arrive.
`Telemetry::builder()` sets the service name, the span filter, logs to stderr, or your
own span and metric exporters (in-memory ones in tests), and `build()` hands you the
subscriber to install yourself.
## No traces showing up?
- **Check the endpoint value.** `OTEL_EXPORTER_OTLP_ENDPOINT` (or the traces or metrics
variable) must be an `http` or `https` URL with a host. Anything else, such as
`not-a-url`, is reported at startup, and setup still succeeds without a collector.
- **Turn on logs.** Export failures are warnings, and they reach stderr only when
`RUST_LOG` is set: `RUST_LOG=warn` shows them, plus the OpenTelemetry SDK's own error
lines. Each warning names the signal (`traces` or `metrics`) and the endpoint. At most
one a minute is logged per exporter, with a count of the ones skipped.
- **Check the collector answers.** `curl -v <endpoint>/v1/traces` should get an HTTP
response of any status. "Connection refused" means nothing listens there. A timeout
means it is unreachable from this machine.
- **Check the signal.** A collector that takes traces only, such as Jaeger, rejects
metrics. Traces still arrive, so a `metrics` warning alone does not explain missing
traces.
- **Flush before exit.** Dropping `Telemetry` flushes, but ignores export errors. Call
`force_flush()` where delivery matters.
## Metrics
The rows for the GenAI names are the legacy ones, which Rig emits by default. To record the current semantic conventions' names instead, or both, see [Metric names and migration](#metric-names-and-migration).
| Name | Unit | Attributes | Source | Closest Strands name |
|------|------|------------|--------|----------------------|
| `gen_ai.client.operation.duration` (legacy, deprecated: [see below](#metric-names-and-migration)) | `s` | `gen_ai.operation.name`, `gen_ai.provider.name`, `gen_ai.request.model`, `gen_ai.tool.name`, `error.type` | Every GenAI span (`chat`, `execute_tool`, `invoke_agent`), duration of the span. OpenTelemetry GenAI semconv. | none |
| `gen_ai.client.token.usage` (legacy, deprecated: [see below](#metric-names-and-migration)) | `{token}` | the same, plus `gen_ai.token.type` (`input` or `output`) | Token usage recorded on a model-call span (`chat`, `embeddings` and the other modality operations). Not on `invoke_agent` or `execute_tool`: their totals repeat the model calls'. OpenTelemetry GenAI semconv. | none |
| `rig.token.usage` (deprecated in 0.48, removed in 0.49, together with the legacy names; see [below](#metric-names-and-migration)) | `{token}` | the same, plus `rig.token.type` (`cache_read`, `cache_creation` or `reasoning`) | Cached input and reasoning tokens reported on a `chat` span. The spec revision checked ([gen-ai-token-metrics.md](https://github.com/open-telemetry/semantic-conventions-genai/blob/main/docs/gen-ai/gen-ai-token-metrics.md), `main`, 2026-11-28) has no `gen_ai.token.type` value for them. They are subsets of the input and output counts (cached input is part of input, reasoning is part of output), so do not add them to `gen_ai.client.token.usage`. Not recorded when the provider reports none. | none |
| `rig.tool.call_count` | `{call}` | `gen_ai.agent.name`, `gen_ai.tool.name` | One per `execute_tool` span, so denied and skipped calls count too. | `strands.tool.call_count` |
| `rig.tool.success_count` | `{call}` | `gen_ai.agent.name`, `gen_ai.tool.name` | `execute_tool` spans with `gen_ai.tool.call.outcome` `success`. | `strands.tool.success_count` |
| `rig.tool.error_count` | `{call}` | `gen_ai.agent.name`, `gen_ai.tool.name`, `error.type` | `execute_tool` spans with outcome `error`. `error.type` is `gen_ai.tool.error.type`. | `strands.tool.error_count` |
| `rig.tool.duration` | `s` | `gen_ai.agent.name`, `gen_ai.tool.name`, `error.type` on failed calls | Duration of each `execute_tool` span. | `strands.tool.duration` |
| `rig.model.request_count` | `{request}` | `gen_ai.agent.name` | One per `chat` span: one model request attempt. Retries and turns the hooks reject count too. | `strands.event_loop.cycle_count`, but not the same count (see below) |
| `rig.model.request_duration` | `s` | `gen_ai.agent.name` | Duration of each `chat` span. | `strands.event_loop.cycle_duration`, but not the same duration (see below) |
The last column names the closest metric Strands Agents emits, for reference only. Nothing
here is wired to Strands or AgentCore dashboards: the `rig.*` names are the only ones this
crate emits. Strands has no semconv equivalent for tool calls, so `rig.tool.*` comes from
Rig's `execute_tool` spans.
`rig.model.*` counts model request attempts, not completed turns. A Strands event-loop
cycle also covers the tool calls that run after its model call, so the Strands cycle
count and duration differ from these. The agent name is the span's own
`gen_ai.agent.name`, or the nearest enclosing one. Attributes stay low cardinality: no
session, message or call ids.
## Metric names and migration
The OpenTelemetry GenAI semantic conventions renamed the GenAI duration and token metrics. Rig 0.47.x still emits the legacy names by default, so nothing changes until you opt in. Rig 0.48 makes the current names the default.
Choose the names with the `OTEL_SEMCONV_STABILITY_OPT_IN` variable, or with `TelemetryBuilder::metrics_semconv`, which wins over the variable. If the variable lists both `gen_ai_latest_experimental` and `gen_ai_latest_experimental/dup`, the duplicate mode wins.
| `OTEL_SEMCONV_STABILITY_OPT_IN` | `metrics_semconv` | GenAI names recorded |
|---|---|---|
| unset, or no `gen_ai` entry | `GenAiSemconv::Legacy` | legacy only (the default) |
| `gen_ai_latest_experimental` | `GenAiSemconv::Latest` | current only, plus the generic `gen_ai.client.operation.duration` for operations with no dedicated name (see below) |
| `gen_ai_latest_experimental/dup` | `GenAiSemconv::Duplicate` | legacy and current, to move dashboards without a gap |
The variable is a comma-separated list. Unknown entries are ignored. `rig.*` metrics are the same in every mode.
Old to new, for the GenAI metrics:
| Legacy name (`gen_ai.client.*`) | Operations | Current name |
|---|---|---|
| `operation.duration` | `chat`, `text_completion`, `generate_content` | `gen_ai.client.inference.duration` |
| `operation.duration` | `embeddings` | `gen_ai.client.embeddings.duration` |
| `operation.duration` | `invoke_agent` | `gen_ai.invoke_agent.duration` (one point per run) |
| `operation.duration` | `execute_tool` | `gen_ai.execute_tool.duration` (one point per tool call) |
| `operation.duration` | `retrieval` | none recorded. The semantic conventions give retrieval its own metric, `gen_ai.retrieval.duration`, which Rig does not emit yet |
| `operation.duration` | `rerank`, `transcription`, `image_generation`, `audio_generation` | `gen_ai.client.operation.duration` stays, as the semantic conventions keep it for operations with no dedicated metric. `Latest` records it with the current buckets; `Duplicate` records it with the legacy buckets |
| `token.usage`, `gen_ai.token.type` `input` | `chat`, `text_completion`, `generate_content` | `gen_ai.client.inference.operation.input_tokens` (per operation) and `gen_ai.client.inference.usage.input_tokens` (counter: use this for totals) |
| `token.usage`, `gen_ai.token.type` `output` | `chat`, `text_completion`, `generate_content` | `gen_ai.client.inference.operation.output_tokens` and `gen_ai.client.inference.usage.output_tokens` |
| `token.usage`, `gen_ai.token.type` `input` | `embeddings` | `gen_ai.client.embeddings.operation.input_tokens` |
| `token.usage`, `gen_ai.token.type` `output` | `embeddings` | removed |
| `token.usage` | `rerank`, `transcription`, `image_generation`, `audio_generation` | removed: the semconv has no name for them yet |
| `rig.token.usage`, `rig.token.type` `cache_read` | `chat` | `gen_ai.client.inference.usage.cache_read.input_tokens` |
| `rig.token.usage`, `rig.token.type` `cache_creation` | `chat` | `gen_ai.client.inference.usage.cache_write.input_tokens` |
| `rig.token.usage`, `rig.token.type` `reasoning` | `chat` | `gen_ai.client.inference.usage.reasoning.output_tokens` |
Other changes in the current names:
- The attribute `gen_ai.token.type` is gone. The usage counters carry `gen_ai.token.modality`, set to `unknown`, because Rig does not know the modality of the tokens. The reasoning counter has none.
- The cache and reasoning counters are subsets of the input and output counters. Do not add them to the totals.
- The usage counters record positive counts only. The per-operation token histograms record `0` when the provider reported no usage, failed calls included, as the semconv asks.
- The current duration names use the semconv's buckets: `0.01` to `60` seconds for the inference, embeddings and generic durations, and `0.1` to `86400` seconds for `gen_ai.invoke_agent.duration`. Quantiles move, so re-baseline alerts.
- `rig.token.usage` is unchanged in 0.47.x and in every mode. Its three replacements are the cache and reasoning counters above.
- The `gen_ai.client.inference.usage.*` names, the `gen_ai.token.modality` attribute and the `gen_ai.execute_tool.duration` attributes were checked against the OpenTelemetry GenAI semantic conventions (open-telemetry/semantic-conventions-genai, commit 81b5d438, 2026-11-25; metric docs in `docs/gen-ai/gen-ai-metrics.md`, `gen-ai-token-metrics.md`, `execute-tool.md`). The usage counters require `gen_ai.operation.name`, `gen_ai.provider.name` and `gen_ai.token.modality`, which Rig sets. The execute-tool duration requires `gen_ai.tool.name` and does not list `gen_ai.operation.name`, so it has none.
Timeline:
- 0.47.x: the current names are opt-in, by the variable or the builder option. The default is unchanged.
- 0.48: the current names become the default. The legacy names, and `rig.token.usage`, are deprecated; the legacy names stay available on request, through `GenAiSemconv::Legacy` and the variable, for one release.
- 0.49: the legacy names and `rig.token.usage` are removed.
Deprecation: the legacy GenAI names, and `rig.token.usage`, are deprecated in favour of the current ones in 0.48. The default still records them in 0.47.x, so this is a notice, not a warning in your build.
## AWS Bedrock AgentCore
Spans and metrics carry the instrumentation scope `opentelemetry.instrumentation.rig`,
and conversations carry `session.id`: the two things AgentCore Observability and
AgentCore Evaluations read spans by, so Rig agents show up there like agents of the
frameworks AWS instruments itself.
## Testing
`cargo test` runs a Rig agent on a scripted model with in-memory exporters: the exported
spans and their scope, the duration, token, tool and model-request metrics (one tool
call that succeeds, one that fails), a session reaching every span,
and a trace with its session crossing into another process through the headers.