@traceloop/instrumentation-anthropic 0.10.0 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ var api = require('@opentelemetry/api');
5
5
  var instrumentation = require('@opentelemetry/instrumentation');
6
6
  var aiSemanticConventions = require('@traceloop/ai-semantic-conventions');
7
7
 
8
- var version = "0.10.0";
8
+ var version = "0.11.1";
9
9
 
10
10
  class AnthropicInstrumentation extends instrumentation.InstrumentationBase {
11
11
  constructor(config = {}) {
@@ -161,10 +161,14 @@ class AnthropicInstrumentation extends instrumentation.InstrumentationBase {
161
161
  break;
162
162
  case "content_block_delta":
163
163
  if (chunk.index < result.content.length) {
164
- result.content[chunk.index] = {
165
- type: "text",
166
- text: result.content[chunk.index].text + chunk.delta.text,
167
- };
164
+ const current = result.content[chunk.index];
165
+ if (current.type === "text" &&
166
+ chunk.delta.type === "text_delta") {
167
+ result.content[chunk.index] = {
168
+ type: "text",
169
+ text: current.text + chunk.delta.text,
170
+ };
171
+ }
168
172
  }
169
173
  }
170
174
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../src/instrumentation.ts"],"sourcesContent":["/*\n * Copyright Traceloop\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport {\n context,\n trace,\n Span,\n Attributes,\n SpanKind,\n SpanStatusCode,\n} from \"@opentelemetry/api\";\nimport {\n InstrumentationBase,\n InstrumentationModuleDefinition,\n InstrumentationNodeModuleDefinition,\n safeExecuteInTheMiddle,\n} from \"@opentelemetry/instrumentation\";\nimport {\n CONTEXT_KEY_ALLOW_TRACE_CONTENT,\n SpanAttributes,\n} from \"@traceloop/ai-semantic-conventions\";\nimport { AnthropicInstrumentationConfig } from \"./types\";\nimport { version } from \"../package.json\";\nimport type * as anthropic from \"@anthropic-ai/sdk\";\nimport type {\n CompletionCreateParamsNonStreaming,\n CompletionCreateParamsStreaming,\n Completion,\n} from \"@anthropic-ai/sdk/resources/completions\";\nimport type {\n MessageCreateParamsNonStreaming,\n MessageCreateParamsStreaming,\n Message,\n MessageStreamEvent,\n} from \"@anthropic-ai/sdk/resources/messages\";\nimport type { Stream } from \"@anthropic-ai/sdk/streaming\";\n\nexport class AnthropicInstrumentation extends InstrumentationBase {\n protected declare _config: AnthropicInstrumentationConfig;\n\n constructor(config: AnthropicInstrumentationConfig = {}) {\n super(\"@traceloop/instrumentation-anthropic\", version, config);\n }\n\n public override setConfig(config: AnthropicInstrumentationConfig = {}) {\n super.setConfig(config);\n }\n\n public manuallyInstrument(module: typeof anthropic) {\n this._diag.debug(`Patching @anthropic-ai/sdk manually`);\n\n this._wrap(\n module.Anthropic.Completions.prototype,\n \"create\",\n this.patchAnthropic(\"completion\"),\n );\n this._wrap(\n module.Anthropic.Messages.prototype,\n \"create\",\n this.patchAnthropic(\"chat\"),\n );\n }\n\n protected init(): InstrumentationModuleDefinition {\n const module = new InstrumentationNodeModuleDefinition(\n \"@anthropic-ai/sdk\",\n [\">=0.9.1\"],\n this.patch.bind(this),\n this.unpatch.bind(this),\n );\n return module;\n }\n\n private patch(moduleExports: typeof anthropic, moduleVersion?: string) {\n this._diag.debug(`Patching @anthropic-ai/sdk@${moduleVersion}`);\n\n this._wrap(\n moduleExports.Anthropic.Completions.prototype,\n \"create\",\n this.patchAnthropic(\"completion\"),\n );\n this._wrap(\n moduleExports.Anthropic.Messages.prototype,\n \"create\",\n this.patchAnthropic(\"chat\"),\n );\n return moduleExports;\n }\n\n private unpatch(\n moduleExports: typeof anthropic,\n moduleVersion?: string,\n ): void {\n this._diag.debug(`Unpatching @azure/openai@${moduleVersion}`);\n\n this._unwrap(moduleExports.Anthropic.Completions.prototype, \"create\");\n this._unwrap(moduleExports.Anthropic.Messages.prototype, \"create\");\n }\n\n private patchAnthropic(type: \"chat\" | \"completion\") {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const plugin = this;\n // eslint-disable-next-line @typescript-eslint/ban-types\n return (original: Function) => {\n return function method(this: any, ...args: unknown[]) {\n const span =\n type === \"chat\"\n ? plugin.startSpan({\n type,\n params: args[0] as MessageCreateParamsNonStreaming & {\n extraAttributes?: Record<string, any>;\n },\n })\n : plugin.startSpan({\n type,\n params: args[0] as CompletionCreateParamsNonStreaming & {\n extraAttributes?: Record<string, any>;\n },\n });\n\n const execContext = trace.setSpan(context.active(), span);\n const execPromise = safeExecuteInTheMiddle(\n () => {\n return context.with(execContext, () => {\n if ((args?.[0] as any)?.extraAttributes) {\n delete (args[0] as any).extraAttributes;\n }\n return original.apply(this, args);\n });\n },\n (e) => {\n if (e) {\n plugin._diag.error(\"Error in Anthropic instrumentation\", e);\n }\n },\n );\n\n if (\n (\n args[0] as\n | MessageCreateParamsStreaming\n | CompletionCreateParamsStreaming\n ).stream &&\n type === \"completion\" // For some reason, this causes an exception with chat, so disabled for now\n ) {\n return context.bind(\n execContext,\n plugin._streamingWrapPromise({\n span,\n type,\n promise: execPromise,\n }),\n );\n }\n\n const wrappedPromise = plugin._wrapPromise(type, span, execPromise);\n\n return context.bind(execContext, wrappedPromise as any);\n };\n };\n }\n\n private startSpan({\n type,\n params,\n }:\n | {\n type: \"chat\";\n params: MessageCreateParamsNonStreaming & {\n extraAttributes?: Record<string, any>;\n };\n }\n | {\n type: \"completion\";\n params: CompletionCreateParamsNonStreaming & {\n extraAttributes?: Record<string, any>;\n };\n }): Span {\n const attributes: Attributes = {\n [SpanAttributes.LLM_SYSTEM]: \"Anthropic\",\n [SpanAttributes.LLM_REQUEST_TYPE]: type,\n };\n\n try {\n attributes[SpanAttributes.LLM_REQUEST_MODEL] = params.model;\n attributes[SpanAttributes.LLM_REQUEST_TEMPERATURE] = params.temperature;\n attributes[SpanAttributes.LLM_REQUEST_TOP_P] = params.top_p;\n attributes[SpanAttributes.LLM_TOP_K] = params.top_k;\n\n if (type === \"completion\") {\n attributes[SpanAttributes.LLM_REQUEST_MAX_TOKENS] =\n params.max_tokens_to_sample;\n } else {\n attributes[SpanAttributes.LLM_REQUEST_MAX_TOKENS] = params.max_tokens;\n }\n\n if (\n params.extraAttributes !== undefined &&\n typeof params.extraAttributes === \"object\"\n ) {\n Object.keys(params.extraAttributes).forEach((key: string) => {\n attributes[key] = params.extraAttributes![key];\n });\n }\n\n if (this._shouldSendPrompts()) {\n if (type === \"chat\") {\n params.messages.forEach((message, index) => {\n attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.role`] =\n message.role;\n if (typeof message.content === \"string\") {\n attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.content`] =\n (message.content as string) || \"\";\n } else {\n attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.content`] =\n JSON.stringify(message.content);\n }\n });\n } else {\n attributes[`${SpanAttributes.LLM_PROMPTS}.0.role`] = \"user\";\n attributes[`${SpanAttributes.LLM_PROMPTS}.0.content`] = params.prompt;\n }\n }\n } catch (e) {\n this._diag.debug(e);\n this._config.exceptionLogger?.(e);\n }\n\n return this.tracer.startSpan(`anthropic.${type}`, {\n kind: SpanKind.CLIENT,\n attributes,\n });\n }\n\n private async *_streamingWrapPromise({\n span,\n type,\n promise,\n }:\n | {\n span: Span;\n type: \"chat\";\n promise: Promise<Stream<MessageStreamEvent>>;\n }\n | {\n span: Span;\n type: \"completion\";\n promise: Promise<Stream<Completion>>;\n }) {\n if (type === \"chat\") {\n const result: Message = {\n id: \"0\",\n type: \"message\",\n model: \"\",\n role: \"assistant\",\n stop_reason: null,\n stop_sequence: null,\n usage: { input_tokens: 0, output_tokens: 0 },\n content: [],\n };\n for await (const chunk of await promise) {\n yield chunk;\n\n try {\n switch (chunk.type) {\n case \"content_block_start\":\n if (result.content.length <= chunk.index) {\n result.content.push(chunk.content_block);\n }\n break;\n\n case \"content_block_delta\":\n if (chunk.index < result.content.length) {\n result.content[chunk.index] = {\n type: \"text\",\n text: result.content[chunk.index].text + chunk.delta.text,\n };\n }\n }\n } catch (e) {\n this._diag.debug(e);\n this._config.exceptionLogger?.(e);\n }\n }\n\n this._endSpan({ span, type, result });\n } else {\n const result: Completion = {\n id: \"0\",\n type: \"completion\",\n model: \"\",\n completion: \"\",\n stop_reason: null,\n };\n for await (const chunk of await promise) {\n yield chunk;\n\n try {\n result.id = chunk.id;\n result.model = chunk.model;\n\n if (chunk.stop_reason) {\n result.stop_reason = chunk.stop_reason;\n }\n if (chunk.model) {\n result.model = chunk.model;\n }\n if (chunk.completion) {\n result.completion += chunk.completion;\n }\n } catch (e) {\n this._diag.debug(e);\n this._config.exceptionLogger?.(e);\n }\n }\n\n this._endSpan({ span, type, result });\n }\n }\n\n private _wrapPromise<T>(\n type: \"chat\" | \"completion\",\n span: Span,\n promise: Promise<T>,\n ): Promise<T> {\n return promise\n .then((result) => {\n return new Promise<T>((resolve) => {\n if (type === \"chat\") {\n this._endSpan({\n type,\n span,\n result: result as Message,\n });\n } else {\n this._endSpan({\n type,\n span,\n result: result as Completion,\n });\n }\n\n resolve(result);\n });\n })\n .catch((error: Error) => {\n return new Promise<T>((_, reject) => {\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: error.message,\n });\n span.recordException(error);\n span.end();\n\n reject(error);\n });\n });\n }\n\n private _endSpan({\n span,\n type,\n result,\n }:\n | { span: Span; type: \"chat\"; result: Message }\n | {\n span: Span;\n type: \"completion\";\n result: Completion;\n }) {\n try {\n span.setAttribute(SpanAttributes.LLM_RESPONSE_MODEL, result.model);\n if (type === \"chat\" && result.usage) {\n span.setAttribute(\n SpanAttributes.LLM_USAGE_TOTAL_TOKENS,\n result.usage?.input_tokens + result.usage?.output_tokens,\n );\n span.setAttribute(\n SpanAttributes.LLM_USAGE_COMPLETION_TOKENS,\n result.usage?.output_tokens,\n );\n span.setAttribute(\n SpanAttributes.LLM_USAGE_PROMPT_TOKENS,\n result.usage?.input_tokens,\n );\n }\n\n result.stop_reason &&\n span.setAttribute(\n `${SpanAttributes.LLM_COMPLETIONS}.0.finish_reason`,\n result.stop_reason,\n );\n\n if (this._shouldSendPrompts()) {\n if (type === \"chat\") {\n span.setAttribute(\n `${SpanAttributes.LLM_COMPLETIONS}.0.role`,\n \"assistant\",\n );\n span.setAttribute(\n `${SpanAttributes.LLM_COMPLETIONS}.0.content`,\n JSON.stringify(result.content),\n );\n } else {\n span.setAttribute(\n `${SpanAttributes.LLM_COMPLETIONS}.0.role`,\n \"assistant\",\n );\n span.setAttribute(\n `${SpanAttributes.LLM_COMPLETIONS}.0.content`,\n result.completion,\n );\n }\n }\n } catch (e) {\n this._diag.debug(e);\n this._config.exceptionLogger?.(e);\n }\n\n span.end();\n }\n\n private _shouldSendPrompts() {\n const contextShouldSendPrompts = context\n .active()\n .getValue(CONTEXT_KEY_ALLOW_TRACE_CONTENT);\n\n if (contextShouldSendPrompts !== undefined) {\n return contextShouldSendPrompts;\n }\n\n return this._config.traceContent !== undefined\n ? this._config.traceContent\n : true;\n }\n}\n"],"names":["InstrumentationBase","InstrumentationNodeModuleDefinition","trace","context","safeExecuteInTheMiddle","SpanAttributes","SpanKind","__asyncValues","__await","SpanStatusCode","CONTEXT_KEY_ALLOW_TRACE_CONTENT"],"mappings":";;;;;;;;;AAiDM,MAAO,wBAAyB,SAAQA,mCAAmB,CAAA;AAG/D,IAAA,WAAA,CAAY,SAAyC,EAAE,EAAA;AACrD,QAAA,KAAK,CAAC,sCAAsC,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;KAChE;IAEe,SAAS,CAAC,SAAyC,EAAE,EAAA;AACnE,QAAA,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;KACzB;AAEM,IAAA,kBAAkB,CAAC,MAAwB,EAAA;A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ources":["../../src/instrumentation.ts"],"sourcesContent":["/*\n * Copyright Traceloop\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport {\n context,\n trace,\n Span,\n Attributes,\n SpanKind,\n SpanStatusCode,\n} from \"@opentelemetry/api\";\nimport {\n InstrumentationBase,\n InstrumentationModuleDefinition,\n InstrumentationNodeModuleDefinition,\n safeExecuteInTheMiddle,\n} from \"@opentelemetry/instrumentation\";\nimport {\n CONTEXT_KEY_ALLOW_TRACE_CONTENT,\n SpanAttributes,\n} from \"@traceloop/ai-semantic-conventions\";\nimport { AnthropicInstrumentationConfig } from \"./types\";\nimport { version } from \"../package.json\";\nimport type * as anthropic from \"@anthropic-ai/sdk\";\nimport type {\n CompletionCreateParamsNonStreaming,\n CompletionCreateParamsStreaming,\n Completion,\n} from \"@anthropic-ai/sdk/resources/completions\";\nimport type {\n MessageCreateParamsNonStreaming,\n MessageCreateParamsStreaming,\n Message,\n MessageStreamEvent,\n} from \"@anthropic-ai/sdk/resources/messages\";\nimport type { Stream } from \"@anthropic-ai/sdk/streaming\";\n\nexport class AnthropicInstrumentation extends InstrumentationBase {\n protected declare _config: AnthropicInstrumentationConfig;\n\n constructor(config: AnthropicInstrumentationConfig = {}) {\n super(\"@traceloop/instrumentation-anthropic\", version, config);\n }\n\n public override setConfig(config: AnthropicInstrumentationConfig = {}) {\n super.setConfig(config);\n }\n\n public manuallyInstrument(module: typeof anthropic) {\n this._diag.debug(`Patching @anthropic-ai/sdk manually`);\n\n this._wrap(\n module.Anthropic.Completions.prototype,\n \"create\",\n this.patchAnthropic(\"completion\"),\n );\n this._wrap(\n module.Anthropic.Messages.prototype,\n \"create\",\n this.patchAnthropic(\"chat\"),\n );\n }\n\n protected init(): InstrumentationModuleDefinition {\n const module = new InstrumentationNodeModuleDefinition(\n \"@anthropic-ai/sdk\",\n [\">=0.9.1\"],\n this.patch.bind(this),\n this.unpatch.bind(this),\n );\n return module;\n }\n\n private patch(moduleExports: typeof anthropic, moduleVersion?: string) {\n this._diag.debug(`Patching @anthropic-ai/sdk@${moduleVersion}`);\n\n this._wrap(\n moduleExports.Anthropic.Completions.prototype,\n \"create\",\n this.patchAnthropic(\"completion\"),\n );\n this._wrap(\n moduleExports.Anthropic.Messages.prototype,\n \"create\",\n this.patchAnthropic(\"chat\"),\n );\n return moduleExports;\n }\n\n private unpatch(\n moduleExports: typeof anthropic,\n moduleVersion?: string,\n ): void {\n this._diag.debug(`Unpatching @azure/openai@${moduleVersion}`);\n\n this._unwrap(moduleExports.Anthropic.Completions.prototype, \"create\");\n this._unwrap(moduleExports.Anthropic.Messages.prototype, \"create\");\n }\n\n private patchAnthropic(type: \"chat\" | \"completion\") {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const plugin = this;\n // eslint-disable-next-line @typescript-eslint/ban-types\n return (original: Function) => {\n return function method(this: any, ...args: unknown[]) {\n const span =\n type === \"chat\"\n ? plugin.startSpan({\n type,\n params: args[0] as MessageCreateParamsNonStreaming & {\n extraAttributes?: Record<string, any>;\n },\n })\n : plugin.startSpan({\n type,\n params: args[0] as CompletionCreateParamsNonStreaming & {\n extraAttributes?: Record<string, any>;\n },\n });\n\n const execContext = trace.setSpan(context.active(), span);\n const execPromise = safeExecuteInTheMiddle(\n () => {\n return context.with(execContext, () => {\n if ((args?.[0] as any)?.extraAttributes) {\n delete (args[0] as any).extraAttributes;\n }\n return original.apply(this, args);\n });\n },\n (e) => {\n if (e) {\n plugin._diag.error(\"Error in Anthropic instrumentation\", e);\n }\n },\n );\n\n if (\n (\n args[0] as\n | MessageCreateParamsStreaming\n | CompletionCreateParamsStreaming\n ).stream &&\n type === \"completion\" // For some reason, this causes an exception with chat, so disabled for now\n ) {\n return context.bind(\n execContext,\n plugin._streamingWrapPromise({\n span,\n type,\n promise: execPromise,\n }),\n );\n }\n\n const wrappedPromise = plugin._wrapPromise(type, span, execPromise);\n\n return context.bind(execContext, wrappedPromise as any);\n };\n };\n }\n\n private startSpan({\n type,\n params,\n }:\n | {\n type: \"chat\";\n params: MessageCreateParamsNonStreaming & {\n extraAttributes?: Record<string, any>;\n };\n }\n | {\n type: \"completion\";\n params: CompletionCreateParamsNonStreaming & {\n extraAttributes?: Record<string, any>;\n };\n }): Span {\n const attributes: Attributes = {\n [SpanAttributes.LLM_SYSTEM]: \"Anthropic\",\n [SpanAttributes.LLM_REQUEST_TYPE]: type,\n };\n\n try {\n attributes[SpanAttributes.LLM_REQUEST_MODEL] = params.model;\n attributes[SpanAttributes.LLM_REQUEST_TEMPERATURE] = params.temperature;\n attributes[SpanAttributes.LLM_REQUEST_TOP_P] = params.top_p;\n attributes[SpanAttributes.LLM_TOP_K] = params.top_k;\n\n if (type === \"completion\") {\n attributes[SpanAttributes.LLM_REQUEST_MAX_TOKENS] =\n params.max_tokens_to_sample;\n } else {\n attributes[SpanAttributes.LLM_REQUEST_MAX_TOKENS] = params.max_tokens;\n }\n\n if (\n params.extraAttributes !== undefined &&\n typeof params.extraAttributes === \"object\"\n ) {\n Object.keys(params.extraAttributes).forEach((key: string) => {\n attributes[key] = params.extraAttributes![key];\n });\n }\n\n if (this._shouldSendPrompts()) {\n if (type === \"chat\") {\n params.messages.forEach((message, index) => {\n attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.role`] =\n message.role;\n if (typeof message.content === \"string\") {\n attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.content`] =\n (message.content as string) || \"\";\n } else {\n attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.content`] =\n JSON.stringify(message.content);\n }\n });\n } else {\n attributes[`${SpanAttributes.LLM_PROMPTS}.0.role`] = \"user\";\n attributes[`${SpanAttributes.LLM_PROMPTS}.0.content`] = params.prompt;\n }\n }\n } catch (e) {\n this._diag.debug(e);\n this._config.exceptionLogger?.(e);\n }\n\n return this.tracer.startSpan(`anthropic.${type}`, {\n kind: SpanKind.CLIENT,\n attributes,\n });\n }\n\n private async *_streamingWrapPromise({\n span,\n type,\n promise,\n }:\n | {\n span: Span;\n type: \"chat\";\n promise: Promise<Stream<MessageStreamEvent>>;\n }\n | {\n span: Span;\n type: \"completion\";\n promise: Promise<Stream<Completion>>;\n }) {\n if (type === \"chat\") {\n const result: Message = {\n id: \"0\",\n type: \"message\",\n model: \"\",\n role: \"assistant\",\n stop_reason: null,\n stop_sequence: null,\n usage: { input_tokens: 0, output_tokens: 0 },\n content: [],\n };\n for await (const chunk of await promise) {\n yield chunk;\n\n try {\n switch (chunk.type) {\n case \"content_block_start\":\n if (result.content.length <= chunk.index) {\n result.content.push(chunk.content_block);\n }\n break;\n\n case \"content_block_delta\":\n if (chunk.index < result.content.length) {\n const current = result.content[chunk.index];\n if (\n current.type === \"text\" &&\n chunk.delta.type === \"text_delta\"\n ) {\n result.content[chunk.index] = {\n type: \"text\",\n text: current.text + chunk.delta.text,\n };\n }\n }\n }\n } catch (e) {\n this._diag.debug(e);\n this._config.exceptionLogger?.(e);\n }\n }\n\n this._endSpan({ span, type, result });\n } else {\n const result: Completion = {\n id: \"0\",\n type: \"completion\",\n model: \"\",\n completion: \"\",\n stop_reason: null,\n };\n for await (const chunk of await promise) {\n yield chunk;\n\n try {\n result.id = chunk.id;\n result.model = chunk.model;\n\n if (chunk.stop_reason) {\n result.stop_reason = chunk.stop_reason;\n }\n if (chunk.model) {\n result.model = chunk.model;\n }\n if (chunk.completion) {\n result.completion += chunk.completion;\n }\n } catch (e) {\n this._diag.debug(e);\n this._config.exceptionLogger?.(e);\n }\n }\n\n this._endSpan({ span, type, result });\n }\n }\n\n private _wrapPromise<T>(\n type: \"chat\" | \"completion\",\n span: Span,\n promise: Promise<T>,\n ): Promise<T> {\n return promise\n .then((result) => {\n return new Promise<T>((resolve) => {\n if (type === \"chat\") {\n this._endSpan({\n type,\n span,\n result: result as Message,\n });\n } else {\n this._endSpan({\n type,\n span,\n result: result as Completion,\n });\n }\n\n resolve(result);\n });\n })\n .catch((error: Error) => {\n return new Promise<T>((_, reject) => {\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: error.message,\n });\n span.recordException(error);\n span.end();\n\n reject(error);\n });\n });\n }\n\n private _endSpan({\n span,\n type,\n result,\n }:\n | { span: Span; type: \"chat\"; result: Message }\n | {\n span: Span;\n type: \"completion\";\n result: Completion;\n }) {\n try {\n span.setAttribute(SpanAttributes.LLM_RESPONSE_MODEL, result.model);\n if (type === \"chat\" && result.usage) {\n span.setAttribute(\n SpanAttributes.LLM_USAGE_TOTAL_TOKENS,\n result.usage?.input_tokens + result.usage?.output_tokens,\n );\n span.setAttribute(\n SpanAttributes.LLM_USAGE_COMPLETION_TOKENS,\n result.usage?.output_tokens,\n );\n span.setAttribute(\n SpanAttributes.LLM_USAGE_PROMPT_TOKENS,\n result.usage?.input_tokens,\n );\n }\n\n result.stop_reason &&\n span.setAttribute(\n `${SpanAttributes.LLM_COMPLETIONS}.0.finish_reason`,\n result.stop_reason,\n );\n\n if (this._shouldSendPrompts()) {\n if (type === \"chat\") {\n span.setAttribute(\n `${SpanAttributes.LLM_COMPLETIONS}.0.role`,\n \"assistant\",\n );\n span.setAttribute(\n `${SpanAttributes.LLM_COMPLETIONS}.0.content`,\n JSON.stringify(result.content),\n );\n } else {\n span.setAttribute(\n `${SpanAttributes.LLM_COMPLETIONS}.0.role`,\n \"assistant\",\n );\n span.setAttribute(\n `${SpanAttributes.LLM_COMPLETIONS}.0.content`,\n result.completion,\n );\n }\n }\n } catch (e) {\n this._diag.debug(e);\n this._config.exceptionLogger?.(e);\n }\n\n span.end();\n }\n\n private _shouldSendPrompts() {\n const contextShouldSendPrompts = context\n .active()\n .getValue(CONTEXT_KEY_ALLOW_TRACE_CONTENT);\n\n if (contextShouldSendPrompts !== undefined) {\n return contextShouldSendPrompts;\n }\n\n return this._config.traceContent !== undefined\n ? this._config.traceContent\n : true;\n }\n}\n"],"names":["InstrumentationBase","InstrumentationNodeModuleDefinition","trace","context","safeExecuteInTheMiddle","SpanAttributes","SpanKind","__asyncValues","__await","SpanStatusCode","CONTEXT_KEY_ALLOW_TRACE_CONTENT"],"mappings":";;;;;;;;;AAiDM,MAAO,wBAAyB,SAAQA,mCAAmB,CAAA;AAG/D,IAAA,WAAA,CAAY,SAAyC,EAAE,EAAA;AACrD,QAAA,KAAK,CAAC,sCAAsC,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;KAChE;IAEe,SAAS,CAAC,SAAyC,EAAE,EAAA;AACnE,QAAA,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;KACzB;AAEM,IAAA,kBAAkB,CAAC,MAAwB,EAAA;AAChD,QAAA,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA,mCAAA,CAAqC,CAAC,CAAC;QAExD,IAAI,CAAC,KAAK,CACR,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,SAAS,EACtC,QAAQ,EACR,IAAI,CAAC,cAAc,CAAC,YAAY,CAAC,CAClC,CAAC;QACF,IAAI,CAAC,KAAK,CACR,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,SAAS,EACnC,QAAQ,EACR,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,CAC5B,CAAC;KACH;IAES,IAAI,GAAA;AACZ,QAAA,MAAM,MAAM,GAAG,IAAIC,mDAAmC,CACpD,mBAAmB,EACnB,CAAC,SAAS,CAAC,EACX,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EACrB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CACxB,CAAC;AACF,QAAA,OAAO,MAAM,CAAC;KACf;IAEO,KAAK,CAAC,aAA+B,EAAE,aAAsB,EAAA;QACnE,IAAI,CAAC,KAAK,CAAC,KAAK,CAA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,CAAC,EAAE;AACV,YAAA,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACpB,CAAA,EAAA,GAAA,CAAA,EAAA,GAAA,IAAI,CAAC,OAAO,EAAC,eAAe,MAAA,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,IAAA,CAAA,EAAA,EAAG,CAAC,CAAC,CAAC;SACnC;QAED,IAAI,CAAC,GAAG,EAAE,CAAC;KACZ;IAEO,kBAAkB,GAAA;QACxB,MAAM,wBAAwB,GAAGF,WAAO;AACrC,aAAA,MAAM,EAAE;aACR,QAAQ,CAACO,qDAA+B,CAAC,CAAC;AAE7C,QAAA,IAAI,wBAAwB,KAAK,SAAS,EAAE;AAC1C,YAAA,OAAO,wBAAwB,CAAC;SACjC;AAED,QAAA,OAAO,IAAI,CAAC,OAAO,CAAC,YAAY,KAAK,SAAS;AAC5C,cAAE,IAAI,CAAC,OAAO,CAAC,YAAY;cACzB,IAAI,CAAC;KACV;AACF;;;;"}
package/dist/index.mjs ADDED
@@ -0,0 +1,307 @@
1
+ import { __asyncGenerator, __asyncValues, __await } from 'tslib';
2
+ import { trace, context, SpanKind, SpanStatusCode } from '@opentelemetry/api';
3
+ import { InstrumentationBase, InstrumentationNodeModuleDefinition, safeExecuteInTheMiddle } from '@opentelemetry/instrumentation';
4
+ import { SpanAttributes, CONTEXT_KEY_ALLOW_TRACE_CONTENT } from '@traceloop/ai-semantic-conventions';
5
+
6
+ var version = "0.11.1";
7
+
8
+ class AnthropicInstrumentation extends InstrumentationBase {
9
+ constructor(config = {}) {
10
+ super("@traceloop/instrumentation-anthropic", version, config);
11
+ }
12
+ setConfig(config = {}) {
13
+ super.setConfig(config);
14
+ }
15
+ manuallyInstrument(module) {
16
+ this._diag.debug(`Patching @anthropic-ai/sdk manually`);
17
+ this._wrap(module.Anthropic.Completions.prototype, "create", this.patchAnthropic("completion"));
18
+ this._wrap(module.Anthropic.Messages.prototype, "create", this.patchAnthropic("chat"));
19
+ }
20
+ init() {
21
+ const module = new InstrumentationNodeModuleDefinition("@anthropic-ai/sdk", [">=0.9.1"], this.patch.bind(this), this.unpatch.bind(this));
22
+ return module;
23
+ }
24
+ patch(moduleExports, moduleVersion) {
25
+ this._diag.debug(`Patching @anthropic-ai/sdk@${moduleVersion}`);
26
+ this._wrap(moduleExports.Anthropic.Completions.prototype, "create", this.patchAnthropic("completion"));
27
+ this._wrap(moduleExports.Anthropic.Messages.prototype, "create", this.patchAnthropic("chat"));
28
+ return moduleExports;
29
+ }
30
+ unpatch(moduleExports, moduleVersion) {
31
+ this._diag.debug(`Unpatching @azure/openai@${moduleVersion}`);
32
+ this._unwrap(moduleExports.Anthropic.Completions.prototype, "create");
33
+ this._unwrap(moduleExports.Anthropic.Messages.prototype, "create");
34
+ }
35
+ patchAnthropic(type) {
36
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
37
+ const plugin = this;
38
+ // eslint-disable-next-line @typescript-eslint/ban-types
39
+ return (original) => {
40
+ return function method(...args) {
41
+ const span = type === "chat"
42
+ ? plugin.startSpan({
43
+ type,
44
+ params: args[0],
45
+ })
46
+ : plugin.startSpan({
47
+ type,
48
+ params: args[0],
49
+ });
50
+ const execContext = trace.setSpan(context.active(), span);
51
+ const execPromise = safeExecuteInTheMiddle(() => {
52
+ return context.with(execContext, () => {
53
+ var _a;
54
+ if ((_a = args === null || args === void 0 ? void 0 : args[0]) === null || _a === void 0 ? void 0 : _a.extraAttributes) {
55
+ delete args[0].extraAttributes;
56
+ }
57
+ return original.apply(this, args);
58
+ });
59
+ }, (e) => {
60
+ if (e) {
61
+ plugin._diag.error("Error in Anthropic instrumentation", e);
62
+ }
63
+ });
64
+ if (args[0].stream &&
65
+ type === "completion" // For some reason, this causes an exception with chat, so disabled for now
66
+ ) {
67
+ return context.bind(execContext, plugin._streamingWrapPromise({
68
+ span,
69
+ type,
70
+ promise: execPromise,
71
+ }));
72
+ }
73
+ const wrappedPromise = plugin._wrapPromise(type, span, execPromise);
74
+ return context.bind(execContext, wrappedPromise);
75
+ };
76
+ };
77
+ }
78
+ startSpan({ type, params, }) {
79
+ var _a, _b;
80
+ const attributes = {
81
+ [SpanAttributes.LLM_SYSTEM]: "Anthropic",
82
+ [SpanAttributes.LLM_REQUEST_TYPE]: type,
83
+ };
84
+ try {
85
+ attributes[SpanAttributes.LLM_REQUEST_MODEL] = params.model;
86
+ attributes[SpanAttributes.LLM_REQUEST_TEMPERATURE] = params.temperature;
87
+ attributes[SpanAttributes.LLM_REQUEST_TOP_P] = params.top_p;
88
+ attributes[SpanAttributes.LLM_TOP_K] = params.top_k;
89
+ if (type === "completion") {
90
+ attributes[SpanAttributes.LLM_REQUEST_MAX_TOKENS] =
91
+ params.max_tokens_to_sample;
92
+ }
93
+ else {
94
+ attributes[SpanAttributes.LLM_REQUEST_MAX_TOKENS] = params.max_tokens;
95
+ }
96
+ if (params.extraAttributes !== undefined &&
97
+ typeof params.extraAttributes === "object") {
98
+ Object.keys(params.extraAttributes).forEach((key) => {
99
+ attributes[key] = params.extraAttributes[key];
100
+ });
101
+ }
102
+ if (this._shouldSendPrompts()) {
103
+ if (type === "chat") {
104
+ params.messages.forEach((message, index) => {
105
+ attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.role`] =
106
+ message.role;
107
+ if (typeof message.content === "string") {
108
+ attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.content`] =
109
+ message.content || "";
110
+ }
111
+ else {
112
+ attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.content`] =
113
+ JSON.stringify(message.content);
114
+ }
115
+ });
116
+ }
117
+ else {
118
+ attributes[`${SpanAttributes.LLM_PROMPTS}.0.role`] = "user";
119
+ attributes[`${SpanAttributes.LLM_PROMPTS}.0.content`] = params.prompt;
120
+ }
121
+ }
122
+ }
123
+ catch (e) {
124
+ this._diag.debug(e);
125
+ (_b = (_a = this._config).exceptionLogger) === null || _b === void 0 ? void 0 : _b.call(_a, e);
126
+ }
127
+ return this.tracer.startSpan(`anthropic.${type}`, {
128
+ kind: SpanKind.CLIENT,
129
+ attributes,
130
+ });
131
+ }
132
+ _streamingWrapPromise({ span, type, promise, }) {
133
+ var _a, _b, _c, _d;
134
+ return __asyncGenerator(this, arguments, function* _streamingWrapPromise_1() {
135
+ var _e, e_1, _f, _g, _h, e_2, _j, _k;
136
+ if (type === "chat") {
137
+ const result = {
138
+ id: "0",
139
+ type: "message",
140
+ model: "",
141
+ role: "assistant",
142
+ stop_reason: null,
143
+ stop_sequence: null,
144
+ usage: { input_tokens: 0, output_tokens: 0 },
145
+ content: [],
146
+ };
147
+ try {
148
+ for (var _l = true, _m = __asyncValues(yield __await(promise)), _o; _o = yield __await(_m.next()), _e = _o.done, !_e; _l = true) {
149
+ _g = _o.value;
150
+ _l = false;
151
+ const chunk = _g;
152
+ yield yield __await(chunk);
153
+ try {
154
+ switch (chunk.type) {
155
+ case "content_block_start":
156
+ if (result.content.length <= chunk.index) {
157
+ result.content.push(chunk.content_block);
158
+ }
159
+ break;
160
+ case "content_block_delta":
161
+ if (chunk.index < result.content.length) {
162
+ const current = result.content[chunk.index];
163
+ if (current.type === "text" &&
164
+ chunk.delta.type === "text_delta") {
165
+ result.content[chunk.index] = {
166
+ type: "text",
167
+ text: current.text + chunk.delta.text,
168
+ };
169
+ }
170
+ }
171
+ }
172
+ }
173
+ catch (e) {
174
+ this._diag.debug(e);
175
+ (_b = (_a = this._config).exceptionLogger) === null || _b === void 0 ? void 0 : _b.call(_a, e);
176
+ }
177
+ }
178
+ }
179
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
180
+ finally {
181
+ try {
182
+ if (!_l && !_e && (_f = _m.return)) yield __await(_f.call(_m));
183
+ }
184
+ finally { if (e_1) throw e_1.error; }
185
+ }
186
+ this._endSpan({ span, type, result });
187
+ }
188
+ else {
189
+ const result = {
190
+ id: "0",
191
+ type: "completion",
192
+ model: "",
193
+ completion: "",
194
+ stop_reason: null,
195
+ };
196
+ try {
197
+ for (var _p = true, _q = __asyncValues(yield __await(promise)), _r; _r = yield __await(_q.next()), _h = _r.done, !_h; _p = true) {
198
+ _k = _r.value;
199
+ _p = false;
200
+ const chunk = _k;
201
+ yield yield __await(chunk);
202
+ try {
203
+ result.id = chunk.id;
204
+ result.model = chunk.model;
205
+ if (chunk.stop_reason) {
206
+ result.stop_reason = chunk.stop_reason;
207
+ }
208
+ if (chunk.model) {
209
+ result.model = chunk.model;
210
+ }
211
+ if (chunk.completion) {
212
+ result.completion += chunk.completion;
213
+ }
214
+ }
215
+ catch (e) {
216
+ this._diag.debug(e);
217
+ (_d = (_c = this._config).exceptionLogger) === null || _d === void 0 ? void 0 : _d.call(_c, e);
218
+ }
219
+ }
220
+ }
221
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
222
+ finally {
223
+ try {
224
+ if (!_p && !_h && (_j = _q.return)) yield __await(_j.call(_q));
225
+ }
226
+ finally { if (e_2) throw e_2.error; }
227
+ }
228
+ this._endSpan({ span, type, result });
229
+ }
230
+ });
231
+ }
232
+ _wrapPromise(type, span, promise) {
233
+ return promise
234
+ .then((result) => {
235
+ return new Promise((resolve) => {
236
+ if (type === "chat") {
237
+ this._endSpan({
238
+ type,
239
+ span,
240
+ result: result,
241
+ });
242
+ }
243
+ else {
244
+ this._endSpan({
245
+ type,
246
+ span,
247
+ result: result,
248
+ });
249
+ }
250
+ resolve(result);
251
+ });
252
+ })
253
+ .catch((error) => {
254
+ return new Promise((_, reject) => {
255
+ span.setStatus({
256
+ code: SpanStatusCode.ERROR,
257
+ message: error.message,
258
+ });
259
+ span.recordException(error);
260
+ span.end();
261
+ reject(error);
262
+ });
263
+ });
264
+ }
265
+ _endSpan({ span, type, result, }) {
266
+ var _a, _b, _c, _d, _e, _f;
267
+ try {
268
+ span.setAttribute(SpanAttributes.LLM_RESPONSE_MODEL, result.model);
269
+ if (type === "chat" && result.usage) {
270
+ span.setAttribute(SpanAttributes.LLM_USAGE_TOTAL_TOKENS, ((_a = result.usage) === null || _a === void 0 ? void 0 : _a.input_tokens) + ((_b = result.usage) === null || _b === void 0 ? void 0 : _b.output_tokens));
271
+ span.setAttribute(SpanAttributes.LLM_USAGE_COMPLETION_TOKENS, (_c = result.usage) === null || _c === void 0 ? void 0 : _c.output_tokens);
272
+ span.setAttribute(SpanAttributes.LLM_USAGE_PROMPT_TOKENS, (_d = result.usage) === null || _d === void 0 ? void 0 : _d.input_tokens);
273
+ }
274
+ result.stop_reason &&
275
+ span.setAttribute(`${SpanAttributes.LLM_COMPLETIONS}.0.finish_reason`, result.stop_reason);
276
+ if (this._shouldSendPrompts()) {
277
+ if (type === "chat") {
278
+ span.setAttribute(`${SpanAttributes.LLM_COMPLETIONS}.0.role`, "assistant");
279
+ span.setAttribute(`${SpanAttributes.LLM_COMPLETIONS}.0.content`, JSON.stringify(result.content));
280
+ }
281
+ else {
282
+ span.setAttribute(`${SpanAttributes.LLM_COMPLETIONS}.0.role`, "assistant");
283
+ span.setAttribute(`${SpanAttributes.LLM_COMPLETIONS}.0.content`, result.completion);
284
+ }
285
+ }
286
+ }
287
+ catch (e) {
288
+ this._diag.debug(e);
289
+ (_f = (_e = this._config).exceptionLogger) === null || _f === void 0 ? void 0 : _f.call(_e, e);
290
+ }
291
+ span.end();
292
+ }
293
+ _shouldSendPrompts() {
294
+ const contextShouldSendPrompts = context
295
+ .active()
296
+ .getValue(CONTEXT_KEY_ALLOW_TRACE_CONTENT);
297
+ if (contextShouldSendPrompts !== undefined) {
298
+ return contextShouldSendPrompts;
299
+ }
300
+ return this._config.traceContent !== undefined
301
+ ? this._config.traceContent
302
+ : true;
303
+ }
304
+ }
305
+
306
+ export { AnthropicInstrumentation };
307
+ //# sourceMappingURL=index.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@traceloop/instrumentation-anthropic",
3
- "version": "0.10.0",
3
+ "version": "0.11.1",
4
4
  "description": "Anthropic Instrumentaion",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -26,6 +26,7 @@
26
26
  },
27
27
  "files": [
28
28
  "dist/**/*.js",
29
+ "dist/**/*.mjs",
29
30
  "dist/**/*.js.map",
30
31
  "dist/**/*.d.ts",
31
32
  "doc",
@@ -37,14 +38,14 @@
37
38
  "access": "public"
38
39
  },
39
40
  "dependencies": {
40
- "@opentelemetry/core": "^1.25.0",
41
- "@opentelemetry/instrumentation": "^0.52.0",
42
- "@opentelemetry/semantic-conventions": "^1.25.0",
43
- "@traceloop/ai-semantic-conventions": "^0.10.0",
41
+ "@opentelemetry/core": "^1.26.0",
42
+ "@opentelemetry/instrumentation": "^0.53.0",
43
+ "@opentelemetry/semantic-conventions": "^1.26.0",
44
+ "@traceloop/ai-semantic-conventions": "^0.11.0",
44
45
  "tslib": "^2.3.0"
45
46
  },
46
47
  "devDependencies": {
47
- "@anthropic-ai/sdk": "^0.20.1",
48
+ "@anthropic-ai/sdk": "^0.27.1",
48
49
  "@pollyjs/adapter-node-http": "^6.0.6",
49
50
  "@pollyjs/core": "^6.0.6",
50
51
  "@pollyjs/persister-fs": "^6.0.6",
@@ -52,5 +53,5 @@
52
53
  "ts-mocha": "^10.0.0"
53
54
  },
54
55
  "homepage": "https://github.com/traceloop/openllmetry-js/tree/main/packages/instrumentation-anthropic",
55
- "gitHead": "6ef34e7221122f10e20f433a63b3ce471b2732ba"
56
+ "gitHead": "f9ff60deceae7d798c54f9c62158c43201158db6"
56
57
  }