🪢 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
fix(worker): trace llm fetch failures (#14461)
Tobias Wochinger4ee4628c5f1c
diff --git a/packages/shared/src/server/llm/errors.ts b/packages/shared/src/server/llm/errors.tsindex 0bf9c856df..015f76c60c 100644--- a/packages/shared/src/server/llm/errors.ts+++ b/packages/shared/src/server/llm/errors.ts@@ -37,8 +37,9 @@ export class LLMCompletionError extends Error {message: string;responseStatusCode?: number;isRetryable?: boolean;+ cause?: unknown;}) {- super(params.message);+ super(params.message, { cause: params.cause });this.name = LLMCompletionErrorName;this.responseStatusCode = params.responseStatusCode ?? 500;diff --git a/packages/shared/src/server/llm/fetchLLMCompletion.ts b/packages/shared/src/server/llm/fetchLLMCompletion.tsindex 0dbe952eb3..34e1e8c954 100644--- a/packages/shared/src/server/llm/fetchLLMCompletion.ts+++ b/packages/shared/src/server/llm/fetchLLMCompletion.ts@@ -925,6 +925,7 @@ export async function fetchLLMCompletion(message,responseStatusCode,isRetryable,+ cause: e,});} finally {await processTracedEvents();diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex b2b3436ae3..1f9614895c 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -966,6 +966,9 @@ importers:'@opentelemetry/instrumentation-ioredis':specifier: ^0.67.0version: 0.67.0(@opentelemetry/[email protected])+ '@opentelemetry/instrumentation-undici':+ specifier: ^0.29.0+ version: 0.29.0(@opentelemetry/[email protected])'@opentelemetry/instrumentation-winston':specifier: ^0.63.0version: 0.63.0(@opentelemetry/[email protected])@@ -3156,6 +3159,12 @@ packages:peerDependencies:'@opentelemetry/api': ^1.3.0+ '@opentelemetry/[email protected]':+ resolution: {integrity: sha512-SnA+0XgGc595jtnwFVfWy7Vgfr5hle4D5YKIlm0U4z8aK9YoCZVUn1xAkVZ2evaJyykiDF50FBzr1XZ0uj8CPA==}+ engines: {node: ^18.19.0 || >=20.6.0}+ peerDependencies:+ '@opentelemetry/api': ^1.7.0+'@opentelemetry/[email protected]':resolution: {integrity: sha512-NFMHLYODph0rWGfT/QLv75hCsu1sxVAV79L8HduBCMo181jOTSRZHaqxfrvDtFOsvgYBaiUBa7Ga50w47wM3FA==}engines: {node: ^18.19.0 || >=20.6.0}@@ -14220,6 +14229,15 @@ snapshots:transitivePeerDependencies:- supports-color+ '@opentelemetry/[email protected](@opentelemetry/[email protected])':+ dependencies:+ '@opentelemetry/api': 1.9.1+ '@opentelemetry/core': 2.8.0(@opentelemetry/[email protected])+ '@opentelemetry/instrumentation': 0.219.0(@opentelemetry/[email protected])+ '@opentelemetry/semantic-conventions': 1.40.0+ transitivePeerDependencies:+ - supports-color+'@opentelemetry/[email protected](@opentelemetry/[email protected])':dependencies:'@opentelemetry/api': 1.9.1diff --git a/worker/package.json b/worker/package.jsonindex 162ac6ce38..00883b2348 100644--- a/worker/package.json+++ b/worker/package.json@@ -40,6 +40,7 @@"@opentelemetry/instrumentation-express": "^0.67.0","@opentelemetry/instrumentation-http": "^0.219.0","@opentelemetry/instrumentation-ioredis": "^0.67.0",+ "@opentelemetry/instrumentation-undici": "^0.29.0","@opentelemetry/instrumentation-winston": "^0.63.0","@opentelemetry/resource-detector-aws": "^2.19.0","@opentelemetry/resource-detector-container": "^0.8.10",diff --git a/worker/src/__tests__/fetchLLMCompletionErrors.test.ts b/worker/src/__tests__/fetchLLMCompletionErrors.test.tsindex 85d3358c8d..a253b31979 100644--- a/worker/src/__tests__/fetchLLMCompletionErrors.test.ts+++ b/worker/src/__tests__/fetchLLMCompletionErrors.test.ts@@ -43,8 +43,9 @@ class MockLLMCompletionError extends Error {message: string;responseStatusCode?: number;isRetryable?: boolean;+ cause?: unknown;}) {- super(params.message);+ super(params.message, { cause: params.cause });this.name = "LLMCompletionError";this.responseStatusCode = params.responseStatusCode ?? 500;this.isRetryable = params.isRetryable ?? false;@@ -131,8 +132,8 @@ describe("fetchLLMCompletion provider error classification", () => {anthropicInvokeMock.mockRejectedValue(wrappedError);- await expect(- fetchLLMCompletion({+ try {+ await fetchLLMCompletion({streaming: false,messages: [{@@ -151,12 +152,16 @@ describe("fetchLLMCompletion provider error classification", () => {llmConnection: {secretKey: encrypt("anthropic-api-key"),},- }),- ).rejects.toMatchObject({- name: "LLMCompletionError",- responseStatusCode: 401,- isRetryable: false,- });+ });+ expect.fail("Expected fetchLLMCompletion to throw");+ } catch (e) {+ expect(e).toMatchObject({+ name: "LLMCompletionError",+ responseStatusCode: 401,+ isRetryable: false,+ });+ expect((e as Error).cause).toBe(wrappedError);+ }});it("forwards Anthropic options and normalizes sampling params for Claude on Vertex", async () => {diff --git a/worker/src/instrumentation.ts b/worker/src/instrumentation.tsindex 00143cbb43..1e64ef76c5 100644--- a/worker/src/instrumentation.ts+++ b/worker/src/instrumentation.ts@@ -3,6 +3,7 @@ import { NodeSDK } from "@opentelemetry/sdk-node";import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";import { IORedisInstrumentation } from "@opentelemetry/instrumentation-ioredis";import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";+import { UndiciInstrumentation } from "@opentelemetry/instrumentation-undici";import { ExpressInstrumentation } from "@opentelemetry/instrumentation-express";import { PrismaInstrumentation } from "@prisma/instrumentation";import { WinstonInstrumentation } from "@opentelemetry/instrumentation-winston";@@ -23,6 +24,14 @@ dd.init({plugins: false,});+const getUndiciRequestUrl = (origin: string, path: string) => {+ try {+ return new URL(path, origin);+ } catch {+ return null;+ }+};+const sdk = new NodeSDK({resource: resourceFromAttributes({"service.name": env.OTEL_SERVICE_NAME,@@ -53,6 +62,29 @@ const sdk = new NodeSDK({span.updateName(`${req?.method} ${path}`);},}),+ new UndiciInstrumentation({+ requireParentforSpans: true,+ ignoreRequestHook: (request) => {+ const url = getUndiciRequestUrl(request.origin, request.path);+ return url?.hostname === "127.0.0.1" || url?.hostname === "localhost";+ },+ startSpanHook: (request) => {+ const url = getUndiciRequestUrl(request.origin, request.path);++ return url+ ? {+ "url.full": `${url.origin}${url.pathname}`,+ "url.query": "",+ }+ : {};+ },+ requestHook: (span, request) => {+ const url = getUndiciRequestUrl(request.origin, request.path);+ if (url) {+ span.updateName(`${request.method} ${url.pathname}`);+ }+ },+ }),new ExpressInstrumentation(),new PrismaInstrumentation({ignoreSpanTypes: [