Skip to content

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

fix oltp bugs, unused imports

Justin D'Souza3d60405afa42
diff --git a/packages/asymptote-observe-js/src/index.test.ts b/packages/asymptote-observe-js/src/index.test.ts
index 26b5b922..bff36449 100644
--- a/packages/asymptote-observe-js/src/index.test.ts
+++ b/packages/asymptote-observe-js/src/index.test.ts
@@ -4,7 +4,6 @@ import { createServer, type IncomingMessage } from "node:http";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import {
- ATTR_ASYMPTOTE_SDK_MODE,
ATTR_BEACON_EVENT_ACTION,
ATTR_BEACON_EVENT_CATEGORY,
ATTR_BEACON_HARNESS_NAME,
@@ -65,6 +64,22 @@ describe("resolveExporterConfig", () => {
expect(config.headers.authorization).toBeUndefined();
});
+ it("normalizes explicit OTLP traces endpoints to the Observe path", () => {
+ const config = resolveExporterConfig({ otlpEndpoint: "http://127.0.0.1:4318/v1/traces" });
+
+ expect(config.mode).toBe("otlp");
+ expect(config.observeUrl).toBe("http://127.0.0.1:4318/v1/observe");
+ });
+
+ it("normalizes generic OTLP endpoint env when it includes the traces path", () => {
+ process.env.OTEL_EXPORTER_OTLP_ENDPOINT = "http://127.0.0.1:4318/v1/traces/";
+
+ const config = resolveExporterConfig();
+
+ expect(config.mode).toBe("otlp");
+ expect(config.observeUrl).toBe("http://127.0.0.1:4318/v1/observe");
+ });
+
it("prefers Observe endpoint env over generic OTLP endpoint env", () => {
process.env.OTEL_EXPORTER_OTLP_ENDPOINT = "http://127.0.0.1:4318";
process.env.ASYMPTOTE_OBSERVE_ENDPOINT = "http://127.0.0.1:9999/v1/observe";
@@ -161,6 +176,26 @@ describe("Asymptote Observe SDK", () => {
expect(() => initialize({ disableDefaultExporter: true })).toThrow(/already initialized/);
});
+ it("does not wrap Claude Agent query twice when initialized again with instrumented modules", async () => {
+ const exporter = new InMemorySpanExporter();
+ const claudeAgentSDK = {
+ query: async (prompt: string) => ({ ok: true, prompt }),
+ };
+ initialize({
+ spanExporter: exporter,
+ disableDefaultExporter: true,
+ disableBatch: true,
+ instrumentModules: { claudeAgentSDK },
+ });
+
+ initialize({ instrumentModules: { claudeAgentSDK } });
+
+ await expect(claudeAgentSDK.query("explain tracing")).resolves.toEqual({ ok: true, prompt: "explain tracing" });
+ await flush();
+
+ expect(exporter.getFinishedSpans().filter(span => span.name === "claude_agent_sdk.query")).toHaveLength(1);
+ });
+
it("creates OpenLLMetry instrumentations", () => {
const instrumentations = initializeAsymptoteInstrumentations();
diff --git a/packages/asymptote-observe-js/src/index.ts b/packages/asymptote-observe-js/src/index.ts
index 6d0c6268..8599a88f 100644
--- a/packages/asymptote-observe-js/src/index.ts
+++ b/packages/asymptote-observe-js/src/index.ts
@@ -38,16 +38,17 @@ import {
ATTR_BEACON_HARNESS_NAME,
ATTR_BEACON_ORIGIN,
ATTR_BEACON_PROMPT_TEXT,
- ATTR_BEACON_SESSION_ID,
} from "./constants.js";
export * from "./constants.js";
const DEFAULT_HOSTED_BASE_URL = "https://api.asymptotelabs.ai";
const DEFAULT_OBSERVE_PATH = "/v1/observe";
+const DEFAULT_OTLP_TRACES_PATH = "/v1/traces";
const DEFAULT_TRACER_NAME = "asymptote-observe";
const DEFAULT_SERVICE_NAME = "asymptote-observe-app";
const SDK_VERSION = "0.0.0";
+const CLAUDE_AGENT_QUERY_WRAPPED = Symbol.for("@asymptote-labs/observe.claude-agent-query-wrapped");
export type ExportMode = "hosted" | "otlp" | "custom";
@@ -109,6 +110,10 @@ interface SDKState {
observeUrl?: string;
}
+type ClaudeAgentQueryFunction<T extends (...args: any[]) => any = (...args: any[]) => any> = T & {
+ [CLAUDE_AGENT_QUERY_WRAPPED]?: true;
+};
+
let state: SDKState | undefined;
export class AsymptoteObserve {
@@ -271,7 +276,10 @@ export function initializeAsymptoteInstrumentations(options: AsymptoteInstrument
}
export function wrapClaudeAgentQuery<T extends (...args: any[]) => any>(originalQuery: T): T {
- return function asymptoteClaudeAgentQuery(this: unknown, ...args: Parameters<T>): ReturnType<T> {
+ if ((originalQuery as ClaudeAgentQueryFunction<T>)[CLAUDE_AGENT_QUERY_WRAPPED]) {
+ return originalQuery;
+ }
+ const wrapped = function asymptoteClaudeAgentQuery(this: unknown, ...args: Parameters<T>): ReturnType<T> {
const tracer = getTracer();
const span = tracer.startSpan("claude_agent_sdk.query", {
kind: SpanKind.CLIENT,
@@ -292,7 +300,11 @@ export function wrapClaudeAgentQuery<T extends (...args: any[]) => any>(original
throw error;
}
});
- } as T;
+ } as ClaudeAgentQueryFunction<T>;
+ Object.defineProperty(wrapped, CLAUDE_AGENT_QUERY_WRAPPED, {
+ value: true,
+ });
+ return wrapped;
}
export function startActiveSpan<T>(name: string, fn: (span: Span) => T, options: SpanOptions = {}): T {
@@ -351,10 +363,17 @@ function makeSpanProcessor(exporter: SpanExporter, options: InitializeOptions):
}
function observeURL(endpoint: string): string {
- const trimmed = endpoint.replace(/\/+$/, "");
+ let end = endpoint.length;
+ while (end > 0 && endpoint.charCodeAt(end - 1) === 47) {
+ end -= 1;
+ }
+ const trimmed = endpoint.slice(0, end);
if (trimmed.endsWith(DEFAULT_OBSERVE_PATH)) {
return trimmed;
}
+ if (trimmed.endsWith(DEFAULT_OTLP_TRACES_PATH)) {
+ return `${trimmed.slice(0, -DEFAULT_OTLP_TRACES_PATH.length)}${DEFAULT_OBSERVE_PATH}`;
+ }
return `${trimmed}${DEFAULT_OBSERVE_PATH}`;
}