The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
fix(sdk): bind AI instrumentations to the configured provider (#832)
Rudy Mizrahi Celekli99308f5c7fdd
diff --git a/docs/sdk/lifecycle.mdx b/docs/sdk/lifecycle.mdxindex 067de831..7bba20cc 100644--- a/docs/sdk/lifecycle.mdx+++ b/docs/sdk/lifecycle.mdx@@ -61,6 +61,8 @@ If the SDK is already initialized, a later `Observe.initialize({ instrumentModulThat call keeps the active configuration. Repeating the same options, as each integration guide's setup does, is fine. If its other options differ from the active ones, they are ignored and a warning names them; shut down first to change exporter or resource settings.+After shutdown and reinitialization, both provider-instrumented model calls and custom observed steps use the exporter and resource attributes configured by the new initialization.+## Short-lived example```typescript title="Short-lived job example" linesdiff --git a/packages/asymptote-sdk-js/src/index.ts b/packages/asymptote-sdk-js/src/index.tsindex a1673cac..f0e0c73d 100644--- a/packages/asymptote-sdk-js/src/index.ts+++ b/packages/asymptote-sdk-js/src/index.ts@@ -218,7 +218,7 @@ export function initialize(options: InitializeOptions = {}): void {});provider.register();if (instrumentations.length > 0) {- registerInstrumentations({ instrumentations });+ registerInstrumentations({ instrumentations, tracerProvider: provider });}state = { instrumentations, instrumentationsDisabled: !!options.disableInstrumentations, provider, mode: resolved.mode, observeUrl: resolved.observeUrl, options };diff --git a/packages/asymptote-sdk-js/src/provider-routing.test.ts b/packages/asymptote-sdk-js/src/provider-routing.test.tsnew file mode 100644index 00000000..a2ba103e--- /dev/null+++ b/packages/asymptote-sdk-js/src/provider-routing.test.ts@@ -0,0 +1,37 @@+import * as Anthropic from "@anthropic-ai/sdk";+import { InMemorySpanExporter } from "@opentelemetry/sdk-trace-base";+import { afterEach, expect, it } from "vitest";+import { initialize, observe, flush, shutdown } from "./index.js";++const resources = [Anthropic.Anthropic.Completions, Anthropic.Anthropic.Messages, Anthropic.Anthropic.Beta.Messages];+const originals = resources.map(resource => Object.getOwnPropertyDescriptor(resource.prototype, "create")!);++afterEach(async () => {+ await shutdown();+ resources.forEach((resource, index) => { Object.defineProperty(resource.prototype, "create", originals[index]); });+});++it("routes real provider instrumentation to the new exporter after reinitialization", async () => {+ let fetches = 0;+ const client = new Anthropic.Anthropic({+ apiKey: "synthetic-fixture-only", baseURL: "http://127.0.0.1:1",+ fetch: async () => {+ fetches++;+ return new Response(JSON.stringify({ id: "msg_fixture", type: "message", role: "assistant", model: "synthetic-model", content: [{ type: "text", text: "fixture" }], stop_reason: "end_turn", stop_sequence: null, usage: { input_tokens: 1, output_tokens: 2 } }), { status: 200, headers: { "content-type": "application/json" } });+ },+ });+ const invoke = () => client.messages.create({ model: "synthetic-model", max_tokens: 8, messages: [{ role: "user", content: "offline fixture" }] });+ for (const serviceName of ["first-provider", "second-provider"]) {+ const exporter = new InMemorySpanExporter();+ initialize({ serviceName, spanExporter: exporter, disableDefaultExporter: true, disableBatch: true, instrumentationOptions: { openAI: false }, instrumentModules: { Anthropic } });+ observe({ name: "manual-control" }, () => "unchanged")();+ expect((await invoke()).content).toEqual([{ type: "text", text: "fixture" }]);+ await flush();+ const spans = exporter.getFinishedSpans();+ expect(spans.filter(span => span.name === "manual-control")).toHaveLength(1);+ expect(spans.filter(span => span.name !== "manual-control"), serviceName).toHaveLength(1);+ expect(spans.every(span => span.resource.attributes["service.name"] === serviceName)).toBe(true);+ await shutdown();+ }+ expect(fetches).toBe(2);+});