Repository navigation
feat(observability): record Agents SDK spans through the OpenTelemetry API #2753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "agents": minor | ||
| --- | ||
|
|
||
| Agents SDK spans (agent lifecycle, `wrapAISDK`, Think) are now recorded through the OpenTelemetry API and register the Workers tracing bridge automatically; names and attributes are unchanged, and failures also set span status `ERROR`. See [Observability](https://github.com/cloudflare/agents/blob/main/docs/agents/observability.md#opentelemetry). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 0 additions & 31 deletions
31
packages/agents/src/node-tests/observability-bundle.test.ts
This file was deleted.
Oops, something went wrong.
176 changes: 176 additions & 0 deletions
176
packages/agents/src/node-tests/otel/agents-tracer.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| import { | ||
| context, | ||
| ProxyTracerProvider, | ||
| ROOT_CONTEXT, | ||
| trace | ||
| } from "@opentelemetry/api"; | ||
| import type { ContextManager, Span, Tracer } from "@opentelemetry/api"; | ||
| import { afterEach, beforeEach, expect, it, vi } from "vitest"; | ||
| import type { FakeTracing } from "./fake-tracing"; | ||
|
|
||
| const REGISTRY = Symbol.for("opentelemetry.js.api.1"); | ||
|
|
||
| function teardown() { | ||
| context.disable(); | ||
| trace.disable(); | ||
| } | ||
|
|
||
| /** A fresh copy of the Agents SDK tracer and the native tracing it sits on. */ | ||
| async function load(): Promise<{ | ||
| tracer: typeof import("../../observability/tracing/agents").tracer; | ||
| tracing: FakeTracing; | ||
| }> { | ||
| vi.resetModules(); | ||
| const { tracer } = await import("../../observability/tracing/agents"); | ||
| const { tracing } = await import("./cloudflare-workers"); | ||
| return { tracer, tracing }; | ||
| } | ||
|
|
||
| function registered(): boolean { | ||
| const registry = (globalThis as Record<symbol, { trace?: unknown }>)[ | ||
| REGISTRY | ||
| ]; | ||
| return registry?.trace !== undefined; | ||
| } | ||
|
|
||
| beforeEach(teardown); | ||
| afterEach(teardown); | ||
|
|
||
| it("registers the bridge on the first Agents SDK span", async () => { | ||
| const { tracer, tracing } = await load(); | ||
| expect(registered()).toBe(false); | ||
|
|
||
| tracing.invocation((root) => | ||
| tracer.withSpan("invoke_agent weather", {}, () => { | ||
| // Application OTel spans and platform spans nest under the agent span. | ||
| trace.getTracer("app").startActiveSpan("app span", (span) => { | ||
| tracing.platform("kv_get"); | ||
| span.end(); | ||
| }); | ||
| expect(root.children.map((span) => span.name)).toEqual([ | ||
| "invoke_agent weather" | ||
| ]); | ||
| }) | ||
| ); | ||
|
|
||
| expect(registered()).toBe(true); | ||
| const agent = tracing.recorded.find( | ||
| (span) => span.name === "invoke_agent weather" | ||
| ); | ||
| expect(agent?.ended).toBe(true); | ||
| expect(agent?.children.map((span) => span.name)).toEqual(["app span"]); | ||
| expect(agent?.children[0]?.children.map((span) => span.name)).toEqual([ | ||
| "kv_get" | ||
| ]); | ||
| }); | ||
|
|
||
| it("writes exactly the attributes it is given, and error status on failure", async () => { | ||
| const { tracer, tracing } = await load(); | ||
| const attributes = { | ||
| "gen_ai.operation.name": "invoke_agent", | ||
| "gen_ai.agent.name": "weather", | ||
| "instrumentation_scope.name": "agents", | ||
| "instrumentation_scope.version": "1", | ||
| "cloudflare.agents.session.name": undefined, | ||
| "gen_ai.usage.input_tokens": 3, | ||
| "cloudflare.agents.storage.grouped": true | ||
| }; | ||
|
|
||
| await tracing.invocation(async () => { | ||
| await expect( | ||
| tracer.withSpan("invoke_agent weather", attributes, async (span) => { | ||
| span.finish({ "gen_ai.usage.output_tokens": 1 }); | ||
| throw new TypeError("secret detail"); | ||
| }) | ||
| ).rejects.toThrow("secret detail"); | ||
| }); | ||
|
|
||
| const agent = tracing.recorded.find( | ||
| (span) => span.name === "invoke_agent weather" | ||
| ); | ||
| expect(agent?.attributes).toEqual({ | ||
| "gen_ai.operation.name": "invoke_agent", | ||
| "gen_ai.agent.name": "weather", | ||
| "instrumentation_scope.name": "agents", | ||
| "instrumentation_scope.version": "1", | ||
| "gen_ai.usage.input_tokens": 3, | ||
| "cloudflare.agents.storage.grouped": true, | ||
| "gen_ai.usage.output_tokens": 1 | ||
| }); | ||
| // finish() closed the span before the throw, so nothing else is written. | ||
| expect(agent?.status).toEqual({ code: "unset" }); | ||
| expect(agent?.exceptions).toEqual([]); | ||
|
|
||
| await tracing.invocation(async () => { | ||
| await expect( | ||
| tracer.withSpan("chat model", {}, async () => { | ||
| throw new TypeError("secret detail"); | ||
| }) | ||
| ).rejects.toThrow(); | ||
| }); | ||
| const chat = tracing.recorded.find((span) => span.name === "chat model"); | ||
| expect(chat?.attributes).toEqual({ "error.type": "TypeError" }); | ||
| expect(chat?.status).toEqual({ code: "error" }); | ||
| expect(chat?.exceptions).toEqual([]); | ||
| }); | ||
|
|
||
| it("records Agents SDK spans in a Durable Object constructor", async () => { | ||
| const { tracer, tracing } = await load(); | ||
|
|
||
| tracing.invocation((root) => | ||
| tracing.constructing(() => { | ||
| // The runtime reports no active span here. | ||
| expect(tracing.getActiveSpan()).toBeUndefined(); | ||
| tracer.withSpan("agent_initialization", {}, () => | ||
|
Comment on lines
+120
to
+124
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| tracer.withSpan("initialize_agent_storage", {}, () => undefined) | ||
| ); | ||
| expect(root.children.map((span) => span.name)).toEqual([ | ||
| "agent_initialization" | ||
| ]); | ||
| expect(root.children[0]?.children.map((span) => span.name)).toEqual([ | ||
| "initialize_agent_storage" | ||
| ]); | ||
| }) | ||
| ); | ||
| expect(registered()).toBe(true); | ||
| }); | ||
|
|
||
| it("steps aside for an application's own OpenTelemetry SDK", async () => { | ||
| const { tracer, tracing } = await load(); | ||
| const started: string[] = []; | ||
| const appTracer: Tracer = { | ||
| startSpan: () => | ||
| trace.wrapSpanContext({ traceId: "", spanId: "", traceFlags: 0 }), | ||
| startActiveSpan<F extends (span: Span) => unknown>( | ||
| name: string, | ||
| ...rest: unknown[] | ||
| ) { | ||
| started.push(name); | ||
| const fn = rest.at(-1) as F; | ||
| return fn( | ||
| trace.wrapSpanContext({ traceId: "", spanId: "", traceFlags: 0 }) | ||
| ) as ReturnType<F>; | ||
| } | ||
| }; | ||
| const provider = new ProxyTracerProvider(); | ||
| provider.setDelegate({ getTracer: () => appTracer }); | ||
| const manager: ContextManager = { | ||
| active: () => ROOT_CONTEXT, | ||
| with: (_context, fn, thisArg, ...args) => fn.apply(thisArg, args), | ||
| bind: (_context, target) => target, | ||
| enable() { | ||
| return this; | ||
| }, | ||
| disable() { | ||
| return this; | ||
| } | ||
| }; | ||
| expect(context.setGlobalContextManager(manager)).toBe(true); | ||
| expect(trace.setGlobalTracerProvider(provider)).toBe(true); | ||
|
|
||
| tracing.invocation((root) => { | ||
| tracer.withSpan("invoke_agent weather", {}, () => undefined); | ||
| expect(root.children).toEqual([]); | ||
| }); | ||
| expect(started).toEqual(["invoke_agent weather"]); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| /** `cloudflare:workers` for the node project: native tracing is the model in fake-tracing.ts. */ | ||
| import { createFakeTracing } from "./fake-tracing"; | ||
|
|
||
| export const tracing = createFakeTracing(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔍 Synchronize the published observability guide
The documentation rules require matching changes in cloudflare-docs. Confirm the updated OpenTelemetry instructions reach the published guide.
Was this helpful? React with 👍 or 👎 to provide feedback.