Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(sdk): warn when the module shortcut ignores changed options (#839)

Rudy Mizrahi Celekli0b0b3b9af10b
diff --git a/docs/sdk/lifecycle.mdx b/docs/sdk/lifecycle.mdx
index 2d428f46..71f587cd 100644
--- a/docs/sdk/lifecycle.mdx
+++ b/docs/sdk/lifecycle.mdx
@@ -59,6 +59,8 @@ Observe.initialize({
If the SDK is already initialized, a later `Observe.initialize({ instrumentModules })` call patches additional modules without replacing the provider.
+That 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.
+
## Short-lived example
```typescript title="Short-lived job example" lines
diff --git a/packages/asymptote-sdk-js/src/index.ts b/packages/asymptote-sdk-js/src/index.ts
index bb37bc78..917fcc30 100644
--- a/packages/asymptote-sdk-js/src/index.ts
+++ b/packages/asymptote-sdk-js/src/index.ts
@@ -25,6 +25,7 @@ import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
+import { isDeepStrictEqual } from "node:util";
import {
AnthropicInstrumentation,
type AnthropicInstrumentationConfig,
@@ -113,6 +114,8 @@ interface SDKState {
provider: NodeTracerProvider;
mode: ExportMode;
observeUrl?: string;
+ /** The options the active configuration was built from, to tell a repeat from a conflict. */
+ options: InitializeOptions;
}
type ClaudeAgentQueryFunction<T extends (...args: any[]) => any = (...args: any[]) => any> = T & {
@@ -162,6 +165,13 @@ export class Observe {
export function initialize(options: InitializeOptions = {}): void {
if (state) {
if (options.instrumentModules) {
+ const ignored = changedOptions(state.options, options);
+ if (ignored.length > 0) {
+ console.warn(
+ `Asymptote Observe is already initialized; ignoring changed options: ${ignored.join(", ")}. ` +
+ "Call shutdown() before reinitializing with new options.",
+ );
+ }
patch(options.instrumentModules);
return;
}
@@ -210,7 +220,7 @@ export function initialize(options: InitializeOptions = {}): void {
if (instrumentations.length > 0) {
registerInstrumentations({ instrumentations });
}
- state = { instrumentations, instrumentationsDisabled: !!options.disableInstrumentations, provider, mode: resolved.mode, observeUrl: resolved.observeUrl };
+ state = { instrumentations, instrumentationsDisabled: !!options.disableInstrumentations, provider, mode: resolved.mode, observeUrl: resolved.observeUrl, options };
if (options.instrumentModules) {
try {
@@ -224,6 +234,24 @@ export function initialize(options: InitializeOptions = {}): void {
}
}
+// changedOptions names the options in next, other than instrumentModules, that differ from the
+// active configuration's. Repeating the same options, as the integration guides do in each module
+// they set up, is not a change. Plain objects (headers, resource attributes) compare by value;
+// exporters, processors and arrays of them compare by identity, since two distinct exporter
+// instances can look alike field by field.
+function changedOptions(active: InitializeOptions, next: InitializeOptions): string[] {
+ return (Object.keys(next) as (keyof InitializeOptions)[]).filter(name => {
+ if (name === "instrumentModules" || next[name] === undefined) return false;
+ const a = active[name];
+ const b = next[name];
+ return isPlainObject(a) && isPlainObject(b) ? !isDeepStrictEqual(a, b) : a !== b;
+ });
+}
+
+function isPlainObject(value: unknown): value is Record<string, unknown> {
+ return typeof value === "object" && value !== null && Object.getPrototypeOf(value) === Object.prototype;
+}
+
export function getTracer(name = DEFAULT_TRACER_NAME, version = SDK_VERSION): Tracer {
if (state) {
return state.provider.getTracer(name, version);
diff --git a/packages/asymptote-sdk-js/src/reinitialization-options.test.ts b/packages/asymptote-sdk-js/src/reinitialization-options.test.ts
new file mode 100644
index 00000000..c655f226
--- /dev/null
+++ b/packages/asymptote-sdk-js/src/reinitialization-options.test.ts
@@ -0,0 +1,39 @@
+import * as Anthropic from "@anthropic-ai/sdk";
+import { InMemorySpanExporter } from "@opentelemetry/sdk-trace-base";
+import { afterEach, beforeEach, expect, it, vi, type MockInstance } from "vitest";
+import { flush, initialize, observe, shutdown } from "./index.js";
+
+let warn: MockInstance<typeof console.warn>;
+beforeEach(() => { warn = vi.spyOn(console, "warn").mockImplementation(() => {}); });
+afterEach(async () => { warn.mockRestore(); await shutdown(); });
+
+// Each integration guide calls initialize({ apiKey, instrumentModules }); an app following two of
+// them repeats the same options and must neither fail nor warn.
+it("accepts a repeat of the active options with additional modules", () => {
+ const headers = { "x-team": "core" };
+ initialize({ apiKey: "key-1", headers, disableDefaultExporter: true, disableInstrumentations: true });
+ expect(() => initialize({ apiKey: "key-1", headers: { "x-team": "core" }, instrumentModules: { Anthropic } })).not.toThrow();
+ expect(warn).not.toHaveBeenCalled();
+});
+
+it("keeps the active configuration and names the options it ignores", async () => {
+ const original = new InMemorySpanExporter();
+ const replacement = new InMemorySpanExporter();
+ initialize({ apiKey: "key-1", serviceName: "original-service", spanExporter: original, disableBatch: true, disableInstrumentations: true });
+ initialize({ apiKey: "key-2", serviceName: "original-service", spanExporter: replacement, instrumentModules: { Anthropic } });
+ expect(warn).toHaveBeenCalledTimes(1);
+ const message = String(warn.mock.calls[0][0]);
+ expect(message).toContain("apiKey, spanExporter");
+ expect(message).not.toContain("serviceName");
+ expect(message).not.toContain("key-2");
+ observe({ name: "original-provider" }, () => 1)();
+ await flush();
+ expect(original.getFinishedSpans()).toHaveLength(1);
+ expect(replacement.getFinishedSpans()).toHaveLength(0);
+});
+
+it("does not treat an explicitly undefined option as a change", () => {
+ initialize({ disableDefaultExporter: true, disableInstrumentations: true });
+ initialize({ serviceName: undefined, instrumentModules: { Anthropic } });
+ expect(warn).not.toHaveBeenCalled();
+});