litellm/litellm-rust/crates/traces/tests/otlp.rs
devin-ai-integration[bot] 62fb808d4b
feat: improve Lens dev seeding and live UI (#44468)
* feat: seed Lens dev with configurable load profiles

* feat: run Lens UI live through the dev launcher

* fix: verify Lens UI startup before seeding

* fix(ui): render run timestamps on one compact line

The agent runs table printed the long locale form with timezone, which
wrapped to two lines per row. Use a fixed-width 24h form with
milliseconds that matches the timeline axis, keep the long form in the
hover title, and show the timezone once in the column header

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* refactor(ui): use the root query client for the Lens demo

Drop the demo's nested QueryClient. Cache keys are already partitioned by
scope, and the root client now skips retries on 4xx ApiErrors, which covers
the demo's not-in-demo and read-only rejections and live 4xx alike.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: drop unused synthetic_spend reference from query help

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(traces): drop the deeplite fixtures and map every SDK to a logo

The deeplite captures predate the example repository and carried
synthetic spend rows, which leaked a fixture-only column into the
query help SQL and pinned tests to its shape. Replace them with the
SDK captures in the ClickHouse round trip and query API tests, and
derive the seed tenant lookup from the capture metadata

The runs table only knew the two Anthropic framework slugs. Register
the slugs the normalizer emits for LangChain, LangGraph, Deep Agents,
CrewAI, Google ADK, LlamaIndex, OpenAI Agents, Pydantic AI, Strands,
Vercel AI SDK, Codex, Cursor and Copilot, and fall back to the generic
agent glyph when a run has no known framework

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* refactor(ui): inject the Lens sample through data sources instead of demo checks

Components no longer ask whether they are in the demo. The traces source
carries live and handoff, navigation state comes from a URL or memory
route, and the preview action comes from context instead of onDemo props

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(ui): infinite scroll for the agent runs list

Replace the Load more button with a sentinel that fetches the next
cursor page as the list nears its end. Placeholder rows hold the tail
while more runs exist, and a failed page stops auto-loading until Retry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(ui): keep the whole Lens view in the URL

Lens navigation now lives entirely in query params through nuqs: the
sample session (demo=true, with a Demo data switch in the header), the
open run (trace, trace_ref), the selected step, view and detail section
(span, view, span_tab) and the list filters and range (q, agent, status,
hours). Any Lens view is a shareable link and the back button walks runs

RunView takes its selection injected: the drawer feeds it URL state and
the investigations evidence sheet keeps a local one, so a finding's
original run never writes step ids into the URL. Leaving the sample
session clears every Lens key except the tab so sample ids never point
at live data

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* style(traces): cargo fmt captures tests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Yujong Lee <yujong@berri.ai>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-04 01:03:11 +00:00

1430 lines
52 KiB
Rust

use litellm_traces::decode_otlp;
use litellm_traces::{AgentType, Integration, ObservationType, Shared};
use opentelemetry_proto::tonic::trace::v1::Span;
use rstest::rstest;
const FIXTURE: &[u8] = include_bytes!("fixtures/langsmith_deep_agent_export.json");
#[rstest]
#[case::root(include_bytes!("fixtures/query_root.json"), ObservationType::Agent, 0, 0)]
#[case::children(include_bytes!("fixtures/query_children.json"), ObservationType::Llm, 12, 6)]
#[case::alternate(include_bytes!("fixtures/query_alternate.json"), ObservationType::Agent, 0, 0)]
#[case::other_team(include_bytes!("fixtures/query_other_team.json"), ObservationType::Agent, 0, 0)]
fn query_fixtures_decode_and_normalize(
#[case] body: &[u8],
#[case] observation_type: ObservationType,
#[case] input_tokens: u32,
#[case] output_tokens: u32,
) {
let spans = decode_otlp(body, Some("application/json")).unwrap();
let first = &spans[0];
assert_eq!(first.normalized.observation_type, observation_type);
assert_eq!(first.normalized.input_tokens, input_tokens);
assert_eq!(first.normalized.output_tokens, output_tokens);
assert!(
spans
.iter()
.all(|span| span.resource_attributes["service.name"] == "fixture")
);
}
#[rstest]
#[case::json(FIXTURE, Some("application/json"))]
fn decodes_neutral_spans(#[case] body: &[u8], #[case] content_type: Option<&str>) {
let spans = decode_otlp(body, content_type).expect("valid OTLP export");
assert_eq!(spans.len(), 6);
assert_eq!(spans[0].trace_id, "4bad42b84e9de3ba46fc870185f8f023");
assert_eq!(spans[0].resource_attributes["service.name"], "agent-demo");
assert_eq!(spans[0].scope_name.as_ref(), "langsmith");
assert!(
spans
.iter()
.any(|span| span.attributes.contains_key("gen_ai.prompt"))
);
}
#[rstest]
fn accepts_trace_larger_than_eight_mib(mut span: opentelemetry_proto::tonic::trace::v1::Span) {
use prost::Message;
span.name = "x".repeat(9 * 1024 * 1024);
let body = request_with(span).encode_to_vec();
let decoded = decode_otlp(&body, None).expect("16 MiB default accepts a 9 MiB trace");
assert_eq!(decoded[0].name.len(), 9 * 1024 * 1024);
}
#[rstest]
fn rejects_invalid_payload() {
assert!(decode_otlp(b"not protobuf", None).is_err());
}
#[rstest]
fn decoder_does_not_enforce_the_http_body_limit() {
let body = format!("{{\"ignored\":\"{}\"}}", "x".repeat(16 * 1024 * 1024 + 1));
assert!(
decode_otlp(body.as_bytes(), Some("application/json"))
.unwrap()
.is_empty()
);
}
fn request_with(
span: opentelemetry_proto::tonic::trace::v1::Span,
) -> opentelemetry_proto::tonic::collector::trace::v1::ExportTraceServiceRequest {
use opentelemetry_proto::tonic::{
collector::trace::v1::ExportTraceServiceRequest,
trace::v1::{ResourceSpans, ScopeSpans},
};
ExportTraceServiceRequest {
resource_spans: vec![ResourceSpans {
scope_spans: vec![ScopeSpans {
spans: vec![span],
..Default::default()
}],
..Default::default()
}],
}
}
#[rstest::fixture]
fn span() -> opentelemetry_proto::tonic::trace::v1::Span {
opentelemetry_proto::tonic::trace::v1::Span {
trace_id: vec![1; 16],
span_id: vec![2; 8],
start_time_unix_nano: 1,
end_time_unix_nano: 2,
..Default::default()
}
}
#[rstest]
fn standard_json_and_protobuf_preserve_the_same_identifiers(
span: opentelemetry_proto::tonic::trace::v1::Span,
) {
use prost::Message;
let request = request_with(span);
let json = serde_json::to_vec(&request).unwrap();
let binary = request.encode_to_vec();
let json_spans = decode_otlp(&json, Some("application/json; charset=utf-8")).unwrap();
let binary_spans = decode_otlp(&binary, Some("application/x-protobuf")).unwrap();
assert_eq!(
serde_json::to_value(&json_spans).unwrap(),
serde_json::to_value(&binary_spans).unwrap()
);
assert_eq!(json_spans[0].trace_id, "01".repeat(16));
assert_eq!(json_spans[0].span_id, "02".repeat(8));
}
#[rstest]
#[case::json("APPLICATION/JSON; charset=utf-8", b"{}")]
#[case::protobuf("application/x-protobuf; charset=binary", b"")]
#[case::protobuf_alias("APPLICATION/PROTOBUF", b"")]
fn supported_content_types_select_the_decoder(#[case] content_type: &str, #[case] body: &[u8]) {
assert!(decode_otlp(body, Some(content_type)).is_ok());
}
#[rstest]
#[case::missing_content_type(None)]
#[case::unsupported_content_type(Some("text/plain"))]
fn content_type_defaults_to_protobuf_and_rejects_unknown_values(
#[case] content_type: Option<&str>,
) {
let result = decode_otlp(b"", content_type);
assert_eq!(result.is_ok(), content_type.is_none());
}
#[rstest]
#[case::short_trace(vec![1; 15], vec![2;8], 1, 2)]
#[case::zero_trace(vec![0; 16], vec![2;8], 1, 2)]
#[case::short_span(vec![1; 16], vec![2;7], 1, 2)]
#[case::timestamp_overflow(vec![1;16], vec![2;8], i64::MAX as u64 + 1, i64::MAX as u64 + 1)]
#[case::negative_duration(vec![1;16], vec![2;8], 3, 2)]
fn rejects_ids_and_timestamps_that_cannot_be_stored(
#[case] trace_id: Vec<u8>,
#[case] span_id: Vec<u8>,
#[case] start: u64,
#[case] end: u64,
) {
use prost::Message;
let span = opentelemetry_proto::tonic::trace::v1::Span {
trace_id,
span_id,
start_time_unix_nano: start,
end_time_unix_nano: end,
..Default::default()
};
assert!(matches!(
decode_otlp(&request_with(span).encode_to_vec(), None),
Err(litellm_traces::Error::InvalidPayload)
));
}
#[rstest]
fn resource_fanout_shares_one_allocation(span: opentelemetry_proto::tonic::trace::v1::Span) {
use opentelemetry_proto::tonic::{
common::v1::{AnyValue, KeyValue, any_value::Value},
resource::v1::Resource,
};
use prost::Message;
let mut request = request_with(span.clone());
request.resource_spans[0].resource = Some(Resource {
attributes: vec![KeyValue {
key: "shared".into(),
value: Some(AnyValue {
value: Some(Value::StringValue("x".repeat(16 * 1024))),
}),
..Default::default()
}],
..Default::default()
});
request.resource_spans[0].scope_spans[0].spans = vec![span; 1024];
let second_scope = request.resource_spans[0].scope_spans[0].clone();
request.resource_spans[0].scope_spans.push(second_scope);
request
.resource_spans
.push(request.resource_spans[0].clone());
let body = request.encode_to_vec();
let decoded = decode_otlp(&body, None).expect("shared resources do not expand with span count");
assert_eq!(decoded.len(), 4096);
assert!(decoded[..2048].iter().all(|span| {
Shared::shares_storage_with(&span.resource_attributes, &decoded[0].resource_attributes)
}));
assert!(!Shared::shares_storage_with(
&decoded[0].resource_attributes,
&decoded[2048].resource_attributes
));
assert_eq!(
*decoded[0].resource_attributes,
*decoded[2048].resource_attributes
);
}
#[rstest]
fn nested_values_are_serialized_once(span: opentelemetry_proto::tonic::trace::v1::Span) {
use opentelemetry_proto::tonic::common::v1::{
AnyValue, ArrayValue, KeyValue, any_value::Value,
};
use prost::Message;
let nested = (0..8).fold(
AnyValue {
value: Some(Value::StringValue("quoted \"value\"".into())),
},
|child, _| AnyValue {
value: Some(Value::ArrayValue(ArrayValue {
values: vec![child],
})),
},
);
let mut request = request_with(span);
request.resource_spans[0].scope_spans[0].spans[0].attributes = vec![KeyValue {
key: "nested".into(),
value: Some(nested),
..Default::default()
}];
let spans = decode_otlp(&request.encode_to_vec(), None).unwrap();
let expected = (0..8).fold(serde_json::json!("quoted \"value\""), |child, _| {
serde_json::json!([child])
});
assert_eq!(
serde_json::from_str::<serde_json::Value>(&spans[0].attributes["nested"]).unwrap(),
expected
);
assert!(spans[0].attributes["nested"].len() < 64);
}
#[rstest]
#[case::nesting(format!("{}0{}", "[".repeat(40), "]".repeat(40)).into_bytes())]
#[case::nodes(format!("[{}]", vec!["0"; 65537].join(",")).into_bytes())]
fn rejects_json_structure_before_building_a_tree(#[case] body: Vec<u8>) {
assert!(matches!(
decode_otlp(&body, Some("application/json")),
Err(litellm_traces::Error::TooLarge)
));
}
#[rstest]
#[case::depth(40, 1)]
#[case::nodes(0, 65537)]
fn protobuf_preflight_rejects_expansion_before_prost_allocates(
span: opentelemetry_proto::tonic::trace::v1::Span,
#[case] depth: usize,
#[case] count: usize,
) {
use opentelemetry_proto::tonic::common::v1::{
AnyValue, ArrayValue, KeyValue, any_value::Value,
};
use prost::Message;
let value = (0..depth).fold(
AnyValue {
value: Some(Value::BoolValue(true)),
},
|child, _| AnyValue {
value: Some(Value::ArrayValue(ArrayValue {
values: vec![child],
})),
},
);
let mut request = request_with(span);
request.resource_spans[0].scope_spans[0].spans[0].attributes = vec![KeyValue {
key: "deep".into(),
value: Some(value),
..Default::default()
}];
request.resource_spans = vec![request.resource_spans[0].clone(); count];
let body = request.encode_to_vec();
assert!(matches!(
decode_otlp(&body, None),
Err(litellm_traces::Error::TooLarge)
));
}
#[rstest]
fn scope_fanout_shares_name_and_version(span: opentelemetry_proto::tonic::trace::v1::Span) {
use opentelemetry_proto::tonic::common::v1::InstrumentationScope;
use prost::Message;
let mut request = request_with(span.clone());
request.resource_spans[0].scope_spans[0].scope = Some(InstrumentationScope {
name: "n".repeat(16 * 1024),
version: "v".repeat(16 * 1024),
..Default::default()
});
request.resource_spans[0].scope_spans[0].spans = vec![span; 1024];
let decoded = decode_otlp(&request.encode_to_vec(), None).unwrap();
assert!(
decoded
.iter()
.all(|span| Shared::shares_storage_with(&span.scope_name, &decoded[0].scope_name))
);
assert!(
decoded.iter().all(|span| Shared::shares_storage_with(
&span.scope_version,
&decoded[0].scope_version
))
);
assert_eq!(decoded[0].scope_name.len(), 16 * 1024);
assert_eq!(decoded[0].scope_version.len(), 16 * 1024);
}
#[rstest]
fn unique_attribute_expansion_still_respects_decoded_budget(
span: opentelemetry_proto::tonic::trace::v1::Span,
) {
use opentelemetry_proto::tonic::common::v1::{AnyValue, KeyValue, any_value::Value};
use prost::Message;
let mut request = request_with(span.clone());
request.resource_spans[0].scope_spans[0].spans = (0..1024)
.map(|index| {
let mut span = span.clone();
span.attributes = vec![KeyValue {
key: "unique".into(),
value: Some(AnyValue {
value: Some(Value::StringValue(format!(
"{index:04}{}",
"x".repeat(16_300)
))),
}),
..Default::default()
}];
span
})
.collect();
let body = request.encode_to_vec();
assert!(body.len() < 16 * 1024 * 1024);
assert!(matches!(
decode_otlp(&body, None),
Err(litellm_traces::Error::TooLarge)
));
}
#[rstest]
fn escaped_attribute_expansion_is_bounded_below_four_mib(
span: opentelemetry_proto::tonic::trace::v1::Span,
) {
use opentelemetry_proto::tonic::common::v1::{
AnyValue, ArrayValue, KeyValue, any_value::Value,
};
use prost::Message;
let mut request = request_with(span);
request.resource_spans[0].scope_spans[0].spans[0].attributes = vec![KeyValue {
key: "escaped".into(),
value: Some(AnyValue {
value: Some(Value::ArrayValue(ArrayValue {
values: vec![AnyValue {
value: Some(Value::StringValue("\0".repeat(3 * 1024 * 1024))),
}],
})),
}),
..Default::default()
}];
let body = request.encode_to_vec();
assert!(body.len() < 4 * 1024 * 1024);
assert!(matches!(
decode_otlp(&body, None),
Err(litellm_traces::Error::TooLarge)
));
}
#[rstest]
fn normalizes_langsmith_fixture() {
let spans = decode_otlp(FIXTURE, Some("application/json")).expect("valid OTLP export");
let llm = spans
.iter()
.find(|span| span.name == "ChatOpenAI")
.expect("LLM span");
assert_eq!(llm.normalized.observation_type, ObservationType::Llm);
assert_eq!(
llm.normalized.agent_name.as_deref().unwrap_or_default(),
"deep_research_agent"
);
assert_eq!(
llm.normalized.model.as_deref().unwrap_or_default(),
"claude-sonnet-4-5"
);
assert_eq!(
(llm.normalized.input_tokens, llm.normalized.output_tokens),
(3332, 467)
);
assert!(llm.normalized.calls.key_set().unwrap().contains(
&litellm_traces::CallKey::ProviderResponse(
"chatcmpl-4077bb36-9380-4a3b-9481-245700cef09a".to_owned()
)
));
let input: serde_json::Value =
serde_json::from_str(&llm.normalized.input).expect("message input");
assert_eq!(input[0]["role"], "system");
assert_eq!(input[1]["role"], "user");
let output: serde_json::Value =
serde_json::from_str(&llm.normalized.output).expect("message output");
assert_eq!(output["role"], "assistant");
assert!(output["tool_calls"][0]["name"].is_string());
assert!(output["tool_calls"][0]["id"].is_string());
assert_eq!(output["tool_calls"][0]["type"], "tool_call");
let root = spans
.iter()
.find(|span| span.name == "deep_research_agent")
.expect("root span");
assert_eq!(root.normalized.observation_type, ObservationType::Agent);
assert_eq!(
root.normalized.input,
"[{\"role\": \"user\", \"content\": \"Should we store OTEL agent spans in ClickHouse or Postgres at 50k spans/sec?\"}]"
);
let tool = spans
.iter()
.find(|span| span.name == "task")
.expect("tool span");
assert_eq!(tool.normalized.observation_type, ObservationType::Tool);
assert!(tool.normalized.output.starts_with("Based on my research"));
}
fn decode_normalization(
span: Span,
scope: &str,
attributes: &[(&str, &str)],
) -> Result<litellm_traces::DecodedSpan, litellm_traces::Error> {
decode_normalization_with_resources(span, scope, attributes, &[])
}
fn decode_normalization_with_resources(
span: Span,
scope: &str,
attributes: &[(&str, &str)],
resources: &[(&str, &str)],
) -> Result<litellm_traces::DecodedSpan, litellm_traces::Error> {
use opentelemetry_proto::tonic::{
collector::trace::v1::ExportTraceServiceRequest,
common::v1::{AnyValue, InstrumentationScope, KeyValue, any_value::Value},
resource::v1::Resource,
trace::v1::{ResourceSpans, ScopeSpans},
};
use prost::Message;
let request = ExportTraceServiceRequest {
resource_spans: vec![ResourceSpans {
resource: Some(Resource {
attributes: resources
.iter()
.map(|(key, value)| KeyValue {
key: (*key).to_owned(),
value: Some(AnyValue {
value: Some(Value::StringValue((*value).to_owned())),
}),
..Default::default()
})
.collect(),
..Default::default()
}),
scope_spans: vec![ScopeSpans {
scope: Some(InstrumentationScope {
name: scope.to_owned(),
..Default::default()
}),
spans: vec![Span {
attributes: attributes
.iter()
.map(|(key, value)| KeyValue {
key: (*key).to_owned(),
value: Some(AnyValue {
value: Some(Value::StringValue((*value).to_owned())),
}),
..Default::default()
})
.collect(),
..span
}],
..Default::default()
}],
..Default::default()
}],
};
decode_otlp(&request.encode_to_vec(), None)
.map(|spans| spans.into_iter().next().expect("one synthetic span"))
}
#[rstest]
#[case::agent("invoke_agent", false, ObservationType::Agent)]
#[case::chat("chat", false, ObservationType::Llm)]
#[case::completion("text_completion", false, ObservationType::Llm)]
#[case::content("generate_content", false, ObservationType::Llm)]
#[case::tool("execute_tool", false, ObservationType::Tool)]
#[case::embedding("embeddings", true, ObservationType::Embedding)]
#[case::retrieval("retrieval", false, ObservationType::Retriever)]
#[case::workflow("invoke_workflow", true, ObservationType::Chain)]
#[case::create_agent("create_agent", false, ObservationType::Framework)]
#[case::unknown_root("unknown", true, ObservationType::Agent)]
#[case::unknown_child("unknown", false, ObservationType::Chain)]
#[case::missing_root("", true, ObservationType::Agent)]
#[case::missing_child("", false, ObservationType::Chain)]
fn genai_operations_and_parentage_classify_spans(
span: Span,
#[case] operation: &str,
#[case] root: bool,
#[case] expected: ObservationType,
) {
let decoded = decode_normalization(
Span {
parent_span_id: if root { vec![] } else { vec![3; 8] },
..span
},
"",
&[("gen_ai.operation.name", operation)],
)
.unwrap();
assert_eq!(decoded.normalized.observation_type, expected);
}
#[rstest]
#[case::retriever("RETRIEVER", ObservationType::Retriever)]
#[case::embedding("EMBEDDING", ObservationType::Embedding)]
#[case::reranker("RERANKER", ObservationType::Reranker)]
#[case::guardrail("GUARDRAIL", ObservationType::Guardrail)]
#[case::evaluator("EVALUATOR", ObservationType::Evaluator)]
#[case::prompt("PROMPT", ObservationType::Prompt)]
#[case::decision("DECISION", ObservationType::Decision)]
fn openinference_preserves_operation_and_payload_at_any_depth(
span: Span,
#[case] kind: &str,
#[case] expected: ObservationType,
#[values(true, false)] root: bool,
) {
let input = r#"{"query":"hello"}"#;
let output = r#"[{"id":"doc-1","score":0.9}]"#;
let decoded = decode_normalization(
Span {
parent_span_id: if root { vec![] } else { vec![3; 8] },
..span
},
"openinference.instrumentation.example",
&[
("openinference.span.kind", kind),
("input.value", input),
("output.value", output),
],
)
.unwrap();
assert_eq!(decoded.normalized.observation_type, expected);
assert!(!decoded.normalized.wrapper_candidate);
assert_eq!(decoded.normalized.input, input);
assert_eq!(decoded.normalized.output, output);
assert_eq!(
serde_json::to_value(decoded.normalized.calls.kind()).unwrap(),
"unknown"
);
}
#[rstest]
#[case::claude("claude-code", Integration::ClaudeCode)]
#[case::codex("openai-codex", Integration::OpenaiCodex)]
#[case::deepagents("deepagents-code", Integration::DeepagentsCode)]
#[case::cursor("cursor", Integration::Cursor)]
#[case::pi("pi", Integration::Pi)]
#[case::opencode("opencode", Integration::Opencode)]
#[case::copilot("copilot", Integration::Copilot)]
#[case::extension("future-agent", Integration::Other("future-agent".to_owned()))]
fn coding_identity_is_independent_of_model_operation(
span: Span,
#[case] integration: &str,
#[case] expected: Integration,
) {
let decoded = decode_normalization(
span,
"langsmith",
&[
("langsmith.span.kind", "llm"),
("langsmith.metadata.ls_agent_type", "subagent"),
("langsmith.metadata.ls_integration", integration),
("langsmith.metadata.thread_id", "thread-1"),
("langsmith.metadata.ls_subagent_id", "agent-1"),
("langsmith.metadata.ls_subagent_type", "researcher"),
("langsmith.metadata.ls_model_name", "test-model"),
],
)
.unwrap();
assert_eq!(decoded.normalized.observation_type, ObservationType::Llm);
assert_eq!(
decoded
.normalized
.framework
.as_ref()
.map(ToString::to_string)
.unwrap_or_default(),
integration
);
assert_eq!(
decoded.normalized.model.as_deref().unwrap_or_default(),
"test-model"
);
assert_eq!(
decoded.normalized.agent_name.as_deref().unwrap_or_default(),
"researcher"
);
assert_eq!(
serde_json::to_value(decoded.normalized.calls.kind()).unwrap(),
"unknown"
);
assert!(
decoded
.normalized
.calls
.key_set()
.is_none_or(|keys| keys.is_empty())
);
let metadata = &decoded.normalized.agent_metadata;
assert_eq!(metadata.ls_integration, Some(expected));
assert_eq!(metadata.ls_agent_type, Some(AgentType::Subagent));
assert_eq!(metadata.thread_id.as_deref(), Some("thread-1"));
assert_eq!(metadata.ls_subagent_id.as_deref(), Some("agent-1"));
assert_eq!(
serde_json::to_value(metadata).unwrap()["ls_integration"],
integration
);
}
#[rstest]
#[case::subagent("subagent", "chain", ObservationType::Agent)]
#[case::root("root", "chain", ObservationType::Agent)]
#[case::middleware("middleware", "chain", ObservationType::Framework)]
#[case::compaction("compaction", "chain", ObservationType::Framework)]
#[case::compaction_model("compaction", "llm", ObservationType::Llm)]
#[case::middleware_tool("middleware", "tool", ObservationType::Tool)]
#[case::retrieval("root", "retriever", ObservationType::Retriever)]
fn agent_context_only_refines_container_roles(
span: Span,
#[case] agent_type: &str,
#[case] kind: &str,
#[case] expected: ObservationType,
) {
let decoded = decode_normalization(
span,
"langsmith",
&[
("langsmith.span.kind", kind),
("langsmith.metadata.ls_agent_type", agent_type),
],
)
.unwrap();
assert_eq!(decoded.normalized.observation_type, expected);
assert!(!decoded.normalized.wrapper_candidate);
}
#[rstest]
fn metadata_sources_merge_with_flattened_values_taking_precedence(span: Span) {
let decoded = decode_normalization(
span,
"langsmith",
&[
("langsmith.span.kind", "tool"),
("metadata", r#"{"ls_integration":"cursor","thread_id":"nested","ls_agent_type":42,"ls_agent_runtime":"runtime","ls_provider":"test-provider","repository_url":"repo","cwd":"directory","ls_agent_runtime_version":"version"}"#),
("thread_id", "direct"),
("langsmith.metadata.thread_id", "flattened"),
("langsmith.metadata.ls_tool_name", "shell"),
("langsmith.metadata.ls_agent_type", "unknown-context"),
],
).unwrap();
let metadata = &decoded.normalized.agent_metadata;
assert_eq!(metadata.thread_id.as_deref(), Some("flattened"));
assert_eq!(metadata.ls_agent_type, None);
assert_eq!(metadata.ls_agent_runtime.as_deref(), Some("runtime"));
assert_eq!(metadata.ls_provider.as_deref(), Some("test-provider"));
assert_eq!(metadata.git_repo_url.as_deref(), Some("repo"));
assert_eq!(metadata.working_directory.as_deref(), Some("directory"));
assert_eq!(metadata.ls_agent_version.as_deref(), Some("version"));
assert_eq!(decoded.name, "shell");
assert_eq!(decoded.normalized.observation_type, ObservationType::Tool);
assert_eq!(
decoded
.normalized
.framework
.as_ref()
.map(ToString::to_string)
.unwrap_or_default(),
"cursor"
);
}
#[rstest]
fn metadata_projection_respects_the_decoded_byte_budget(span: Span) {
let thread = "x".repeat(9 * 1024 * 1024);
assert!(matches!(
decode_normalization(span, "example", &[("thread_id", &thread)]),
Err(litellm_traces::Error::TooLarge)
));
}
#[rstest]
fn genai_retrieval_normalizes_query_and_documents(span: Span) {
let query = "trace storage";
let documents = r#"[{"id":"doc-1","score":0.9}]"#;
let decoded = decode_normalization(
span,
"example",
&[
("gen_ai.operation.name", "retrieval"),
("gen_ai.retrieval.query.text", query),
("gen_ai.retrieval.documents", documents),
],
)
.unwrap();
assert_eq!(
decoded.normalized.observation_type,
ObservationType::Retriever
);
assert_eq!(decoded.normalized.input, query);
assert_eq!(decoded.normalized.output, documents);
assert_eq!(decoded.normalized.input_preview, query);
}
#[rstest]
#[case::image(serde_json::json!({"type": "image_url", "image_url": {"url": "image"}}))]
#[case::unknown(serde_json::json!({"type": "unknown", "payload": "opaque"}))]
#[case::malformed(serde_json::json!({"type": "text", "text": 7}))]
#[case::scalar(serde_json::json!(7))]
fn genai_message_blocks_preserve_text_without_exposing_hidden_content(
span: Span,
#[case] unsupported: serde_json::Value,
#[values(
"reasoning",
"thinking",
"redacted_thinking",
"function_call",
"tool_use",
"tool_call"
)]
hidden_type: &str,
) {
let payload = serde_json::json!([{
"role": "user",
"content": [
{"type": "text", "text": "first"},
{"type": hidden_type, "text": "hidden", "thinking": "hidden", "input": "hidden"},
unsupported,
{"text": "second"},
],
}])
.to_string();
let decoded = decode_normalization(
span,
"",
&[
("gen_ai.input.messages", &payload),
("gen_ai.output.messages", &payload),
],
)
.unwrap();
let expected = serde_json::json!([{"role": "user", "content": "first\n\nsecond"}]);
assert_eq!(
serde_json::from_str::<serde_json::Value>(&decoded.normalized.input).unwrap(),
expected,
);
assert_eq!(
serde_json::from_str::<serde_json::Value>(&decoded.normalized.output).unwrap(),
expected,
);
}
#[rstest]
#[case::text(serde_json::json!("hello"), "hello")]
#[case::object(serde_json::json!({"count": 2}), r#"{"count": 2}"#)]
#[case::number(serde_json::json!(7), "7")]
#[case::empty_blocks(serde_json::json!([]), "")]
fn genai_message_content_preserves_text_and_non_array_fallbacks(
span: Span,
#[case] content: serde_json::Value,
#[case] expected: &str,
) {
let payload = serde_json::json!([{"role": "user", "content": content}]).to_string();
let decoded = decode_normalization(span, "", &[("gen_ai.input.messages", &payload)]).unwrap();
assert_eq!(
serde_json::from_str::<serde_json::Value>(&decoded.normalized.input).unwrap(),
serde_json::json!([{"role": "user", "content": expected}]),
);
}
#[rstest]
#[case::primary("request-model", "messages-in", "messages-out", ["request-model", "messages-in", "messages-out"])]
#[case::fallback("", "", "", ["response-model", "tool-in", "tool-out"])]
#[case::independent_fallback("request-model", "", "messages-out", ["request-model", "tool-in", "messages-out"])]
fn genai_fields_and_consumed_attributes_follow_the_same_fallback(
span: Span,
#[case] model: &str,
#[case] input: &str,
#[case] output: &str,
#[case] expected: [&str; 3],
) {
let decoded = decode_normalization(
span,
"",
&[
("gen_ai.request.model", model),
("gen_ai.response.model", "response-model"),
("gen_ai.input.messages", input),
("gen_ai.output.messages", output),
("gen_ai.tool.call.arguments", "tool-in"),
("gen_ai.tool.call.result", "tool-out"),
("gen_ai.agent.name", "test-agent"),
("gen_ai.response.id", "response-1"),
],
)
.unwrap();
let fields = &decoded.normalized;
assert_eq!(
[
fields.model.as_deref().unwrap_or_default(),
fields.input.as_str(),
fields.output.as_str()
],
expected
);
assert_eq!(fields.agent_name.as_deref(), Some("test-agent"));
assert!(
fields
.calls
.key_set()
.unwrap()
.contains(&litellm_traces::CallKey::ProviderResponse(
"response-1".to_owned()
))
);
assert_eq!(
fields.input,
decoded.attributes[decoded.consumed_attributes[0]]
);
assert_eq!(
fields.output,
decoded.attributes[decoded.consumed_attributes[1]]
);
}
#[rstest]
#[case::specific(&[("llm.token_count.prompt", "5"), ("llm.token_count.completion", "9")], 5, 9)]
#[case::fallback(&[], 17, 23)]
#[case::mixed(&[("llm.token_count.prompt", "5")], 5, 23)]
#[case::empty_specific(&[("llm.token_count.prompt", "")], 0, 23)]
fn openinference_fields_override_genai_and_usage_falls_back_per_field(
span: Span,
#[case] token_attributes: &[(&str, &str)],
#[case] input_tokens: u32,
#[case] output_tokens: u32,
) {
let attributes = [
("openinference.span.kind", "lLm"),
("gen_ai.operation.name", "execute_tool"),
("llm.model_name", "inference-model"),
("gen_ai.request.model", "other-model"),
("agent.name", "inference-agent"),
("input.value", "inference-input"),
("output.value", "inference-output"),
("gen_ai.input.messages", "other-input"),
("gen_ai.output.messages", "other-output"),
("gen_ai.usage.input_tokens", "17"),
("gen_ai.usage.output_tokens", "23"),
];
let combined = attributes
.iter()
.chain(token_attributes)
.copied()
.collect::<Vec<_>>();
let decoded = decode_normalization(span, "", &combined).unwrap();
let fields = &decoded.normalized;
assert_eq!(fields.observation_type, ObservationType::Llm);
assert_eq!(fields.model.as_deref(), Some("inference-model"));
assert_eq!(fields.agent_name.as_deref(), Some("inference-agent"));
assert_eq!(fields.input, "inference-input");
assert_eq!(fields.output, "inference-output");
assert_eq!(
(fields.input_tokens, fields.output_tokens),
(input_tokens, output_tokens)
);
assert_eq!(
*decoded.consumed_attributes,
["input.value", "output.value"]
);
}
#[rstest]
#[case::raw_response("LLM", r#"{"id":"chatcmpl-1","choices":[]}"#, &["provider_response:chatcmpl-1"], "complete")]
#[case::wrapped_response("LLM", r#"{"raw":{"id":"wrapped"}}"#, &["provider_response:wrapped"], "complete")]
#[case::top_level_wins("LLM", r#"{"id":"direct","raw":{"id":"wrapped"}}"#, &["provider_response:direct"], "complete")]
#[case::null_top_level_shadows_raw("LLM", r#"{"id":null,"raw":{"id":"wrapped"}}"#, &[], "unknown")]
#[case::invalid_top_level_shadows_raw("LLM", r#"{"id":7,"raw":{"id":"wrapped"}}"#, &[], "unknown")]
#[case::array_raw_is_not_a_response("LLM", r#"{"raw":["wrapped"]}"#, &[], "unknown")]
#[case::invalid_raw_keeps_top_level("LLM", r#"{"id":"direct","raw":7}"#, &["provider_response:direct"], "complete")]
#[case::langchain_llm_output("LLM", r#"{"llm_output":{"id":"chatcmpl-2"},"generations":[[{"message":{"kwargs":{"type":"ai","content":"hi"}}}]]}"#, &["provider_response:chatcmpl-2"], "complete")]
#[case::langchain_generation("LLM", r#"{"generations":[[{"message":{"kwargs":{"response_metadata":{"id":"chatcmpl-3"}}}}]]}"#, &["provider_response:chatcmpl-3"], "complete")]
#[case::langchain_batch("LLM", r#"{"generations":[[{"message":{"kwargs":{"response_metadata":{"id":"a"}}}}],[{"message":{"kwargs":{}}}]]}"#, &["provider_response:a"], "partial")]
#[case::malformed_candidate("LLM", r#"{"generations":[[{"message":{"kwargs":{"response_metadata":{"id":"a"}}}},null]]}"#, &["provider_response:a"], "partial")]
#[case::malformed_prompt("LLM", r#"{"generations":[null,[{"message":{"response_metadata":{"id":"a"}}}]]}"#, &["provider_response:a"], "partial")]
#[case::invalid_candidate_id("LLM", r#"{"generations":[[{"message":{"response_metadata":{"id":"a"}}},{"message":{"response_metadata":{"id":7}}}]]}"#, &["provider_response:a"], "partial")]
#[case::shared_candidate_id("LLM", r#"{"generations":[[{"message":{"response_metadata":{"id":"a"}}},{"message":{"response_metadata":{"id":"a"}}}]]}"#, &["provider_response:a"], "complete")]
#[case::conflicting_candidate_ids("LLM", r#"{"generations":[[{"message":{"response_metadata":{"id":"a"}}},{"message":{"response_metadata":{"id":"b"}}}]]}"#, &["provider_response:a", "provider_response:b"], "partial")]
#[case::multiple_prompt_ids("LLM", r#"{"generations":[[{"message":{"response_metadata":{"id":"a"}}}],[{"message":{"response_metadata":{"id":"b"}}}]]}"#, &["provider_response:a", "provider_response:b"], "complete")]
#[case::fallback_with_invalid_candidate("LLM", r#"{"llm_output":{"id":"a"},"generations":[[null]]}"#, &["provider_response:a"], "partial")]
#[case::empty_generations("LLM", r#"{"llm_output":{"id":"a"},"generations":[]}"#, &[], "unknown")]
#[case::non_llm("CHAIN", r#"{"id":"task-1"}"#, &[], "unknown")]
#[case::not_json("LLM", "plain text", &[], "unknown")]
#[case::non_string_id("LLM", r#"{"id":7}"#, &[], "unknown")]
fn openinference_llm_output_records_call_evidence(
span: Span,
#[case] kind: &str,
#[case] output: &str,
#[case] keys: &[&str],
#[case] evidence: &str,
) {
let decoded = decode_normalization(
span,
"",
&[("openinference.span.kind", kind), ("output.value", output)],
)
.unwrap();
let recorded: Vec<String> = decoded
.normalized
.calls
.key_set()
.into_iter()
.flatten()
.map(ToString::to_string)
.collect();
assert_eq!(recorded, keys);
assert_eq!(
serde_json::to_value(decoded.normalized.calls.kind()).unwrap(),
evidence
);
}
#[rstest]
#[case::crewai("openinference.instrumentation.crewai", "crewai")]
#[case::multi_word("openinference.instrumentation.claude_agent_sdk", "claude-agent-sdk")]
#[case::other_scope("other", "")]
fn openinference_scope_names_the_framework(
span: Span,
#[case] scope: &str,
#[case] framework: &str,
) {
let decoded =
decode_normalization(span, scope, &[("openinference.span.kind", "AGENT")]).unwrap();
assert_eq!(
decoded
.normalized
.framework
.as_ref()
.map(ToString::to_string)
.unwrap_or_default(),
framework
);
}
#[rstest]
#[case::scope("langsmith", &[], ObservationType::Agent)]
#[case::attribute("other", &[("langsmith.span.kind", "llm")], ObservationType::Llm)]
fn langsmith_dispatch_overrides_other_conventions(
span: Span,
#[case] scope: &str,
#[case] convention_attributes: &[(&str, &str)],
#[case] observation_type: ObservationType,
) {
let attributes = [
("openinference.span.kind", "TOOL"),
("gen_ai.operation.name", "execute_tool"),
("langsmith.metadata.lc_agent_name", "test-agent"),
(
"gen_ai.prompt",
r#"{"messages":[{"type":"human","content":"hello"}]}"#,
),
("gen_ai.completion", "{}"),
("input.value", "other-input"),
];
let combined = attributes
.iter()
.chain(convention_attributes)
.copied()
.collect::<Vec<_>>();
let decoded = decode_normalization(span, scope, &combined).unwrap();
assert_eq!(decoded.normalized.observation_type, observation_type);
assert_eq!(
decoded.normalized.agent_name.as_deref().unwrap_or_default(),
"test-agent"
);
assert_eq!(
serde_json::from_str::<serde_json::Value>(&decoded.normalized.input).unwrap(),
serde_json::json!([{"role": "user", "content": "hello"}]),
);
assert_eq!(
*decoded.consumed_attributes,
["gen_ai.prompt", "gen_ai.completion"]
);
}
#[rstest]
#[case::flat(r#"{"messages":[{"type":"human","content":"hello"}]}"#)]
#[case::nested(r#"{"messages":[[{"kwargs":{"type":"human","content":"hello"}}],[{"type":"human","content":"ignored batch"}]]}"#)]
fn langsmith_llm_messages_preserve_visible_content_and_tool_calls(
span: Span,
#[case] prompt: &str,
) {
let completion = r#"{
"generations": [[{"message": {"kwargs": {
"type": "ai",
"content": [
{"type": "text", "text": "first"},
{"type": "thinking", "thinking": "hidden"},
{"type": "tool_use", "id": "call-1"},
{"type": "image_url", "image_url": {"url": "image"}},
{"type": "text", "text": "second"}
],
"tool_calls": [{"name": "search", "args": {"query": "hello"}, "id": "call-1"}],
"response_metadata": {"id": "response-1"}
}}}]]
}"#;
let decoded = decode_normalization(
span,
"langsmith",
&[
("langsmith.span.kind", "llm"),
("gen_ai.prompt", prompt),
("gen_ai.completion", completion),
],
)
.unwrap();
let input: serde_json::Value = serde_json::from_str(&decoded.normalized.input).unwrap();
let output: serde_json::Value = serde_json::from_str(&decoded.normalized.output).unwrap();
assert_eq!(
input,
serde_json::json!([{"role": "user", "content": "hello"}])
);
assert_eq!(
output,
serde_json::json!({
"role": "assistant",
"content": "first\n\nsecond",
"tool_calls": [{"name": "search", "args": {"query": "hello"}, "id": "call-1"}],
})
);
assert!(decoded.normalized.calls.key_set().unwrap().contains(
&litellm_traces::CallKey::ProviderResponse("response-1".to_owned())
));
}
#[rstest]
#[case::string(r#""result""#, "result")]
#[case::wrapped(r#"{"output":{"content":"result"}}"#, "result")]
#[case::command(
r#"{"output":{"update":{"messages":[{"content":"ignored"},{"content":"result"}]}}}"#,
"result"
)]
#[case::object(r#"{"output":{"count":2}}"#, r#"{"count": 2}"#)]
#[case::null(r#"{"output":null}"#, "null")]
fn langsmith_tool_output_unwraps_supported_shapes(
span: Span,
#[case] completion: &str,
#[case] expected: &str,
) {
let decoded = decode_normalization(
span,
"langsmith",
&[
("langsmith.span.kind", "tool"),
("gen_ai.prompt", "raw-tool-input"),
("gen_ai.completion", completion),
],
)
.unwrap();
assert_eq!(decoded.normalized.observation_type, ObservationType::Tool);
assert_eq!(decoded.normalized.input, "raw-tool-input");
assert_eq!(decoded.normalized.output, expected);
}
const CLAUDE_AGENT_SDK_FIXTURE: &[u8] = include_bytes!("fixtures/claude_agent_sdk_export.json");
const CLAUDE_AGENT_SDK_DETAILED_FIXTURE: &[u8] =
include_bytes!("fixtures/claude_agent_sdk_detailed_export.json");
fn raw_spans(fixture: &[u8]) -> Vec<serde_json::Value> {
let export: serde_json::Value = serde_json::from_slice(fixture).expect("fixture JSON");
export["resourceSpans"][0]["scopeSpans"][0]["spans"]
.as_array()
.expect("spans")
.clone()
}
fn raw_attribute(span: &serde_json::Value, key: &str) -> Option<serde_json::Value> {
span["attributes"]
.as_array()
.expect("attributes")
.iter()
.find(|attribute| attribute["key"] == key)
.map(|attribute| attribute["value"].clone())
}
fn raw_string(span: &serde_json::Value, key: &str) -> String {
raw_attribute(span, key)
.and_then(|value| value["stringValue"].as_str().map(str::to_owned))
.unwrap_or_default()
}
fn raw_int(span: &serde_json::Value, key: &str) -> u64 {
raw_attribute(span, key).map_or(0, |value| match &value["intValue"] {
serde_json::Value::String(text) => text.parse().expect("integer"),
number => number.as_u64().expect("integer"),
})
}
fn raw_span<'a>(raw: &'a [serde_json::Value], span_id: &str) -> &'a serde_json::Value {
raw.iter()
.find(|span| {
span["spanId"]
.as_str()
.is_some_and(|id| id.eq_ignore_ascii_case(span_id))
})
.expect("raw span")
}
#[rstest]
#[case::default_telemetry(CLAUDE_AGENT_SDK_FIXTURE)]
#[case::detailed_telemetry(CLAUDE_AGENT_SDK_DETAILED_FIXTURE)]
fn normalizes_claude_agent_sdk_fixture(#[case] fixture: &[u8]) {
let spans = decode_otlp(fixture, Some("application/json")).expect("valid OTLP export");
let raw = raw_spans(fixture);
let types: std::collections::BTreeSet<_> = spans
.iter()
.map(|span| format!("{:?}", span.normalized.observation_type))
.collect();
assert_eq!(
types,
["Agent", "Framework", "Llm", "Tool"]
.into_iter()
.map(str::to_owned)
.collect()
);
let root = spans
.iter()
.find(|span| span.normalized.observation_type == ObservationType::Agent)
.expect("interaction root");
assert!(root.parent_span_id.is_empty());
let root_input: serde_json::Value =
serde_json::from_str(&root.normalized.input).expect("root input messages");
assert_eq!(root_input[0]["role"], "user");
assert_eq!(
root_input[0]["content"],
raw_string(raw_span(&raw, &root.span_id), "user_prompt")
);
assert!(root.consumed_attributes.contains(&"user_prompt"));
let tools: Vec<_> = spans
.iter()
.filter(|span| span.normalized.observation_type == ObservationType::Tool)
.collect();
assert_eq!(tools.len(), 2);
for tool in &tools {
assert_eq!(
tool.name,
raw_string(raw_span(&raw, &tool.span_id), "tool_name")
);
let input: serde_json::Value =
serde_json::from_str(&tool.normalized.input).expect("tool argument object");
assert!(input.is_object());
assert!(input.get("role").is_none());
let event = tool
.events
.iter()
.find(|event| event.name == "tool.output")
.expect("tool output event");
let expected_output = ["output", "content", "diff"]
.into_iter()
.filter_map(|key| event.attributes.get(key))
.find(|value| !value.is_empty())
.expect("event output");
assert_eq!(&tool.normalized.output, expected_output);
}
let bash = tools
.iter()
.find(|tool| tool.name == "Bash")
.expect("Bash tool");
assert_eq!(
serde_json::from_str::<serde_json::Value>(&bash.normalized.input).unwrap()["command"],
raw_string(raw_span(&raw, &bash.span_id), "full_command")
);
let llms: Vec<_> = spans
.iter()
.filter(|span| span.normalized.observation_type == ObservationType::Llm)
.collect();
assert!(!llms.is_empty());
for llm in &llms {
let raw_llm = raw_span(&raw, &llm.span_id);
let expected = raw_int(raw_llm, "input_tokens")
+ raw_int(raw_llm, "cache_read_tokens")
+ raw_int(raw_llm, "cache_creation_tokens");
assert_eq!(u64::from(llm.normalized.input_tokens), expected);
assert_eq!(
u64::from(llm.normalized.output_tokens),
raw_int(raw_llm, "output_tokens")
);
assert_eq!(
llm.normalized.model.as_deref().unwrap_or_default(),
raw_string(raw_llm, "model")
);
if raw_string(raw_llm, "query_source_safe") == "sdk" {
assert_eq!(
llm.normalized
.framework
.as_ref()
.map(ToString::to_string)
.unwrap_or_default(),
"claude-agent-sdk"
);
}
}
assert!(spans.iter().all(|span| {
span.normalized.agent_name.as_deref().unwrap_or_default()
== span.resource_attributes["service.name"].as_str()
}));
}
#[rstest]
fn claude_agent_sdk_detailed_fixture_keeps_full_tool_arguments_and_llm_messages() {
let spans = decode_otlp(CLAUDE_AGENT_SDK_DETAILED_FIXTURE, Some("application/json"))
.expect("valid OTLP export");
let raw = raw_spans(CLAUDE_AGENT_SDK_DETAILED_FIXTURE);
let bash = spans
.iter()
.find(|span| span.name == "Bash")
.expect("Bash tool");
let tool_input = raw_string(raw_span(&raw, &bash.span_id), "tool_input");
let (_, arguments) = tool_input.split_once('\n').expect("tool input header");
assert_eq!(
serde_json::from_str::<serde_json::Value>(&bash.normalized.input).unwrap(),
serde_json::from_str::<serde_json::Value>(arguments).unwrap()
);
assert!(bash.consumed_attributes.contains(&"tool_input"));
let answer = spans
.iter()
.find(|span| {
span.normalized.observation_type == ObservationType::Llm
&& span.attributes.get("query_source_safe").map(String::as_str) == Some("sdk")
&& !span.normalized.output.is_empty()
})
.expect("final SDK answer");
let raw_answer = raw_span(&raw, &answer.span_id);
let input: serde_json::Value =
serde_json::from_str(&answer.normalized.input).expect("llm input messages");
assert_eq!(input[0]["role"], "system");
assert_eq!(
input[0]["content"],
raw_string(raw_answer, "system_prompt_preview")
);
let output: serde_json::Value =
serde_json::from_str(&answer.normalized.output).expect("llm output message");
assert_eq!(output["role"], "assistant");
assert_eq!(
output["content"],
raw_string(raw_answer, "response.model_output")
);
let title = spans
.iter()
.find(|span| {
span.attributes.get("query_source_safe").map(String::as_str)
== Some("generate_session_title")
})
.expect("side query");
assert_eq!(
title
.normalized
.framework
.as_ref()
.map(ToString::to_string)
.unwrap_or_default(),
"claude-agent-sdk"
);
}
#[rstest]
fn claude_code_scope_takes_precedence_over_other_conventions(span: Span) {
let decoded = decode_normalization(
span,
"com.anthropic.claude_code.tracing",
&[
("span.type", "tool"),
("tool_name", "Grep"),
("openinference.span.kind", "LLM"),
("langsmith.span.kind", "LLM"),
],
)
.expect("valid span");
assert_eq!(decoded.normalized.observation_type, ObservationType::Tool);
assert_eq!(decoded.name, "Grep");
assert_eq!(
decoded
.normalized
.framework
.as_ref()
.map(ToString::to_string)
.unwrap_or_default(),
"claude-code"
);
assert_eq!(
decoded.normalized.agent_name.as_deref().unwrap_or_default(),
"claude-code"
);
}
#[rstest]
#[case::sdk_wrapper("openinference.instrumentation.claude_agent_sdk", &[("openinference.span.kind", "AGENT"), ("agent.name", "Agent")], &[("gen_ai.agent.name", "worker")], "worker")]
#[case::generic_fallback("custom", &[], &[("gen_ai.agent.name", "worker")], "worker")]
#[case::generic_explicit("custom", &[("gen_ai.agent.name", "explicit")], &[("gen_ai.agent.name", "worker")], "explicit")]
#[case::generic_service("custom", &[], &[("service.name", "worker")], "")]
#[case::hermes_default("hermes-otel-plugin", &[("gen_ai.agent.name", "hermes-agent")], &[("gen_ai.agent.name", "worker")], "worker")]
#[case::hermes_explicit("hermes-otel-plugin", &[("gen_ai.agent.name", "explicit")], &[("gen_ai.agent.name", "worker")], "explicit")]
#[case::claude_default("com.anthropic.claude_code.tracing", &[], &[("gen_ai.agent.name", "worker"), ("service.name", "service")], "worker")]
#[case::claude_service("com.anthropic.claude_code.tracing", &[], &[("service.name", "service")], "service")]
#[case::claude_empty_resource_name("com.anthropic.claude_code.tracing", &[], &[("gen_ai.agent.name", ""), ("service.name", "service")], "service")]
#[case::claude_subagent("com.anthropic.claude_code.tracing", &[("span.type", "llm_request"), ("query_source", "agent:custom:delegate")], &[("gen_ai.agent.name", "worker"), ("service.name", "service")], "delegate")]
fn resource_identity_preserves_explicit_names_and_sdk_fallbacks(
span: Span,
#[case] scope: &str,
#[case] attributes: &[(&str, &str)],
#[case] resources: &[(&str, &str)],
#[case] expected: &str,
) {
let decoded = decode_normalization_with_resources(span, scope, attributes, resources)
.expect("valid span");
assert_eq!(
decoded.normalized.agent_name.as_deref().unwrap_or_default(),
expected
);
}
#[rstest]
#[case::depth(litellm_traces::DecodeLimits { depth: 1, ..Default::default() })]
#[case::nodes(litellm_traces::DecodeLimits { nodes: 1, ..Default::default() })]
#[case::spans(litellm_traces::DecodeLimits { spans: 1, ..Default::default() })]
#[case::attributes(litellm_traces::DecodeLimits { attributes: 1, ..Default::default() })]
#[case::events(litellm_traces::DecodeLimits { events: 1, ..Default::default() })]
#[case::links(litellm_traces::DecodeLimits { links: 1, ..Default::default() })]
#[case::decoded_bytes(litellm_traces::DecodeLimits { decoded_span_bytes: 1, ..Default::default() })]
fn configurable_decode_limits_apply_to_both_wire_formats(
mut span: Span,
#[case] limits: litellm_traces::DecodeLimits,
) {
use opentelemetry_proto::tonic::{
common::v1::KeyValue,
trace::v1::span::{Event, Link},
};
use prost::Message;
span.attributes = vec![
KeyValue {
key: "a".into(),
..Default::default()
},
KeyValue {
key: "b".into(),
..Default::default()
},
];
span.events = vec![Event::default(), Event::default()];
span.links = vec![
Link {
trace_id: vec![1; 16],
span_id: vec![2; 8],
..Default::default()
};
2
];
let mut request = request_with(span.clone());
request.resource_spans[0].scope_spans[0].spans.push(span);
for (body, content_type) in [
(serde_json::to_vec(&request).unwrap(), "application/json"),
(request.encode_to_vec(), "application/x-protobuf"),
] {
assert!(matches!(
litellm_traces::decode_otlp_with_limits(&body, Some(content_type), limits),
Err(litellm_traces::Error::TooLarge)
));
assert_eq!(
litellm_traces::decode_otlp_with_limits(
&body,
Some(content_type),
litellm_traces::DecodeLimits::default()
)
.unwrap()
.len(),
2
);
}
}
#[test]
fn environment_decode_limits_are_used_and_invalid_values_fail() {
for value in ["2", "4", "0", "invalid"] {
let result = std::process::Command::new(std::env::current_exe().unwrap())
.args(["--exact", "environment_decode_limits_child"])
.env("LITELLM_TEST_DECODE_LIMIT", value)
.env("OTLP_MAX_SPANS", value)
.output()
.unwrap();
assert!(
result.status.success(),
"{}",
String::from_utf8_lossy(&result.stdout)
);
}
}
#[test]
fn environment_decode_limits_child() {
let Ok(value) = std::env::var("LITELLM_TEST_DECODE_LIMIT") else {
return;
};
let result = decode_otlp(
include_bytes!("fixtures/opentelemetry_simple.json"),
Some("application/json"),
);
match value.as_str() {
"2" => assert!(matches!(result, Err(litellm_traces::Error::TooLarge))),
"4" => assert_eq!(result.unwrap().len(), 3),
_ => assert!(matches!(
result,
Err(litellm_traces::Error::InvalidLimit("OTLP_MAX_SPANS"))
)),
}
}