🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(otel): name CMA spans after agent name (#18754)
Nimardf7df892fe61
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.tsindex f31579ffd4..5c1ae62ac8 100644--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts@@ -420,7 +420,6 @@ export class OtelIngestionProcessor {const parentSpanId = span?.parentSpanId? this.parseId(span.parentSpanId): null;- const name = this.extractName(span.name, spanAttributes);const { startTimeISO, endTimeISO } =OtelIngestionProcessor.resolveSpanTimestamps({startTimeUnixNano: span.startTimeUnixNano,@@ -579,7 +578,12 @@ export class OtelIngestionProcessor {spanId,parentSpanId,- name,+ name: this.extractName(+ span.name,+ spanAttributes,+ observationType,+ scopeSpan.scope?.name,+ ),type: observationType,environment: this.extractEnvironment(spanAttributes,@@ -1280,7 +1284,12 @@ export class OtelIngestionProcessor {id: this.parseId(span.spanId?.data ?? span.spanId),traceId,parentObservationId,- name: this.extractName(span.name, attributes),+ name: this.extractName(+ span.name,+ attributes,+ mappedObservationType,+ instrumentationScopeName,+ ),startTime: startTimeISO,endTime: endTimeISO,environment: this.extractEnvironment(attributes, resourceAttributes),@@ -2550,6 +2559,8 @@ export class OtelIngestionProcessor {private extractName(spanName: string,attributes: Record<string, unknown>,+ observationType?: ObservationType,+ instrumentationScopeName?: string,): string {// GenAI tool name (standard OTel GenAI attribute)if ("gen_ai.tool.name" in attributes && attributes["gen_ai.tool.name"]) {@@ -2589,9 +2600,48 @@ export class OtelIngestionProcessor {return prefix + attributes[operationIdAttribute];}+ const claudeManagedAgentName = this.getClaudeManagedAgentName(+ spanName,+ attributes,+ observationType,+ instrumentationScopeName,+ );+ if (claudeManagedAgentName) {+ return claudeManagedAgentName;+ }+return spanName;}+ private getClaudeManagedAgentName(+ spanName: string,+ attributes: Record<string, unknown>,+ observationType?: ObservationType,+ instrumentationScopeName?: string,+ ): string | undefined {+ // Only replace the generic session-turn name in Claude Managed Agents (CMA) traces.+ // The Claude Platform scope alone is also used by regular API calls.+ const sessionId = attributes["anthropic.session.id"];+ const isClaudeManagedAgent =+ spanName === "anthropic.session.turn" &&+ observationType === ObservationType.AGENT &&+ instrumentationScopeName === "com.anthropic.claude-platform" &&+ attributes["gen_ai.operation.name"] === "invoke_agent" &&+ typeof sessionId === "string" &&+ sessionId.trim().length > 0;++ const agentName = attributes["gen_ai.agent.name"];+ if (+ isClaudeManagedAgent &&+ typeof agentName === "string" &&+ agentName.trim().length > 0+ ) {+ return agentName;+ }++ return undefined;+ }+private extractMetadata(attributes: Record<string, unknown>,domain: "trace" | "observation",diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.tsindex 7423417e92..93391b532e 100644--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts@@ -5780,6 +5780,129 @@ describe("OTel Resource Span Mapping", () => {).toBeUndefined();});+ it.each([+ {+ scenario: "names the agent",+ operation: "invoke_agent",+ expectedType: "AGENT",+ expectedName: "support-agent",+ },+ {+ scenario: "preserves custom agent span names",+ spanName: "custom-agent-run",+ operation: "invoke_agent",+ expectedType: "AGENT",+ expectedName: "custom-agent-run",+ },+ {+ scenario: "keeps generation names generic",+ operation: "chat",+ expectedType: "GENERATION",+ expectedName: "anthropic.model_request",+ },+ {+ scenario: "respects an explicit generation type",+ operation: "invoke_agent",+ explicitType: "generation",+ expectedType: "GENERATION",+ expectedName: "anthropic.session.turn",+ },+ {+ scenario: "does not rename agents from other instrumentation scopes",+ scopeName: "other-agent-sdk",+ operation: "invoke_agent",+ expectedType: "AGENT",+ expectedName: "anthropic.session.turn",+ },+ {+ scenario: "requires a managed session ID",+ sessionId: null,+ operation: "invoke_agent",+ expectedType: "AGENT",+ expectedName: "anthropic.session.turn",+ },+ {+ scenario: "requires an invoke_agent operation",+ spanName: "anthropic.session.turn",+ operation: "chat",+ explicitType: "agent",+ expectedType: "AGENT",+ expectedName: "anthropic.session.turn",+ },+ ])(+ "Claude Managed Agents naming: $scenario",+ async ({+ scopeName = "com.anthropic.claude-platform",+ sessionId = "session-example",+ operation,+ spanName = operation === "chat"+ ? "anthropic.model_request"+ : "anthropic.session.turn",+ explicitType,+ expectedType,+ expectedName,+ }) => {+ const attributes: Array<{ key: string; value: unknown }> = [+ { key: "gen_ai.operation.name", value: { stringValue: operation } },+ {+ key: "gen_ai.agent.name",+ value: { stringValue: "support-agent" },+ },+ ];+ if (sessionId !== null) {+ attributes.push({+ key: "anthropic.session.id",+ value: { stringValue: sessionId },+ });+ }+ if (explicitType) {+ attributes.push({+ key: "langfuse.observation.type",+ value: { stringValue: explicitType },+ });+ }+ const resourceSpan: ResourceSpan = {+ scopeSpans: [+ {+ scope: { name: scopeName },+ spans: [+ {+ ...defaultSpanProps,+ traceId: Buffer.from(defaultSpanProps.traceId.data),+ spanId: Buffer.from(defaultSpanProps.spanId.data),+ name: spanName,+ attributes,+ },+ ],+ },+ ],+ };++ const events = await convertOtelSpanToIngestionEvent(+ resourceSpan,+ new Set(),+ );+ const observation = events.find(+ (event) => event.type !== "trace-create",+ );+ const trace = events.find((event) => event.type === "trace-create");+ expect(observation).toMatchObject({+ type: `${expectedType.toLowerCase()}-create`,+ body: { name: expectedName },+ });+ expect(trace?.body.name).toBe(spanName);++ const eventInputs = createTestOtelProcessor().processToEvent([+ resourceSpan,+ ]);+ expect(eventInputs).toHaveLength(1);+ expect(eventInputs[0]).toMatchObject({+ type: expectedType,+ name: expectedName,+ });+ },+ );+it("should filter all input/output attribute patterns from metadata.attributes while preserving custom attributes", async () => {// This test verifies that extractInputAndOutput's filteredAttributes correctly removes// all known input/output attribute patterns from multiple frameworks