@traceloop/instrumentation-anthropic 0.11.0 → 0.11.6
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 +28 -23
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +28 -23
- package/package.json +11 -11
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.11.
|
|
8
|
+
var version = "0.11.6";
|
|
9
9
|
|
|
10
10
|
class AnthropicInstrumentation extends instrumentation.InstrumentationBase {
|
|
11
11
|
constructor(config = {}) {
|
|
@@ -37,7 +37,7 @@ class AnthropicInstrumentation extends instrumentation.InstrumentationBase {
|
|
|
37
37
|
patchAnthropic(type) {
|
|
38
38
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
39
39
|
const plugin = this;
|
|
40
|
-
// eslint-disable-next-line
|
|
40
|
+
// eslint-disable-next-line
|
|
41
41
|
return (original) => {
|
|
42
42
|
return function method(...args) {
|
|
43
43
|
const span = type === "chat"
|
|
@@ -131,10 +131,10 @@ class AnthropicInstrumentation extends instrumentation.InstrumentationBase {
|
|
|
131
131
|
attributes,
|
|
132
132
|
});
|
|
133
133
|
}
|
|
134
|
-
_streamingWrapPromise(
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
var
|
|
134
|
+
_streamingWrapPromise(_a) {
|
|
135
|
+
return tslib.__asyncGenerator(this, arguments, function* _streamingWrapPromise_1({ span, type, promise, }) {
|
|
136
|
+
var _b, e_1, _c, _d, _e, e_2, _f, _g;
|
|
137
|
+
var _h, _j, _k, _l;
|
|
138
138
|
if (type === "chat") {
|
|
139
139
|
const result = {
|
|
140
140
|
id: "0",
|
|
@@ -147,10 +147,10 @@ class AnthropicInstrumentation extends instrumentation.InstrumentationBase {
|
|
|
147
147
|
content: [],
|
|
148
148
|
};
|
|
149
149
|
try {
|
|
150
|
-
for (var
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const chunk =
|
|
150
|
+
for (var _m = true, _o = tslib.__asyncValues(yield tslib.__await(promise)), _p; _p = yield tslib.__await(_o.next()), _b = _p.done, !_b; _m = true) {
|
|
151
|
+
_d = _p.value;
|
|
152
|
+
_m = false;
|
|
153
|
+
const chunk = _d;
|
|
154
154
|
yield yield tslib.__await(chunk);
|
|
155
155
|
try {
|
|
156
156
|
switch (chunk.type) {
|
|
@@ -161,23 +161,27 @@ 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
|
-
|
|
166
|
-
|
|
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
|
}
|
|
171
175
|
catch (e) {
|
|
172
176
|
this._diag.debug(e);
|
|
173
|
-
(
|
|
177
|
+
(_j = (_h = this._config).exceptionLogger) === null || _j === void 0 ? void 0 : _j.call(_h, e);
|
|
174
178
|
}
|
|
175
179
|
}
|
|
176
180
|
}
|
|
177
181
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
178
182
|
finally {
|
|
179
183
|
try {
|
|
180
|
-
if (!
|
|
184
|
+
if (!_m && !_b && (_c = _o.return)) yield tslib.__await(_c.call(_o));
|
|
181
185
|
}
|
|
182
186
|
finally { if (e_1) throw e_1.error; }
|
|
183
187
|
}
|
|
@@ -192,10 +196,10 @@ class AnthropicInstrumentation extends instrumentation.InstrumentationBase {
|
|
|
192
196
|
stop_reason: null,
|
|
193
197
|
};
|
|
194
198
|
try {
|
|
195
|
-
for (var
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
const chunk =
|
|
199
|
+
for (var _q = true, _r = tslib.__asyncValues(yield tslib.__await(promise)), _s; _s = yield tslib.__await(_r.next()), _e = _s.done, !_e; _q = true) {
|
|
200
|
+
_g = _s.value;
|
|
201
|
+
_q = false;
|
|
202
|
+
const chunk = _g;
|
|
199
203
|
yield yield tslib.__await(chunk);
|
|
200
204
|
try {
|
|
201
205
|
result.id = chunk.id;
|
|
@@ -212,14 +216,14 @@ class AnthropicInstrumentation extends instrumentation.InstrumentationBase {
|
|
|
212
216
|
}
|
|
213
217
|
catch (e) {
|
|
214
218
|
this._diag.debug(e);
|
|
215
|
-
(
|
|
219
|
+
(_l = (_k = this._config).exceptionLogger) === null || _l === void 0 ? void 0 : _l.call(_k, e);
|
|
216
220
|
}
|
|
217
221
|
}
|
|
218
222
|
}
|
|
219
223
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
220
224
|
finally {
|
|
221
225
|
try {
|
|
222
|
-
if (!
|
|
226
|
+
if (!_q && !_e && (_f = _r.return)) yield tslib.__await(_f.call(_r));
|
|
223
227
|
}
|
|
224
228
|
finally { if (e_2) throw e_2.error; }
|
|
225
229
|
}
|
|
@@ -269,8 +273,9 @@ class AnthropicInstrumentation extends instrumentation.InstrumentationBase {
|
|
|
269
273
|
span.setAttribute(aiSemanticConventions.SpanAttributes.LLM_USAGE_COMPLETION_TOKENS, (_c = result.usage) === null || _c === void 0 ? void 0 : _c.output_tokens);
|
|
270
274
|
span.setAttribute(aiSemanticConventions.SpanAttributes.LLM_USAGE_PROMPT_TOKENS, (_d = result.usage) === null || _d === void 0 ? void 0 : _d.input_tokens);
|
|
271
275
|
}
|
|
272
|
-
result.stop_reason
|
|
276
|
+
if (result.stop_reason) {
|
|
273
277
|
span.setAttribute(`${aiSemanticConventions.SpanAttributes.LLM_COMPLETIONS}.0.finish_reason`, result.stop_reason);
|
|
278
|
+
}
|
|
274
279
|
if (this._shouldSendPrompts()) {
|
|
275
280
|
if (type === "chat") {
|
|
276
281
|
span.setAttribute(`${aiSemanticConventions.SpanAttributes.LLM_COMPLETIONS}.0.role`, "assistant");
|
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;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,CA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AU,CAClB,CAAC;iBACH;aACF;SACF;QAAC,OAAO,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;;;;"}
|
|
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 declare protected _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\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 if (result.stop_reason) {\n span.setAttribute(\n `${SpanAttributes.LLM_COMPLETIONS}.0.finish_reason`,\n result.stop_reason,\n );\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","__asyncGenerator","__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;;IAGhD,SAAS,CAAC,SAAyC,EAAE,EAAA;AACnE,QAAA,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC;;AAGlB,IAAA,kBAAkB,CAAC,M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AAC,eAAe,CAAA,UAAA,CAAY,EAC7C,MAAM,CAAC,UAAU,CAClB;;;;QAGL,OAAO,CAAC,EAAE;AACV,YAAA,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;YACnB,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;;QAGnC,IAAI,CAAC,GAAG,EAAE;;IAGJ,kBAAkB,GAAA;QACxB,MAAM,wBAAwB,GAAGF;AAC9B,aAAA,MAAM;aACN,QAAQ,CAACQ,qDAA+B,CAAC;AAE5C,QAAA,IAAI,wBAAwB,KAAK,SAAS,EAAE;AAC1C,YAAA,OAAO,wBAAwB;;AAGjC,QAAA,OAAO,IAAI,CAAC,OAAO,CAAC,YAAY,KAAK;AACnC,cAAE,IAAI,CAAC,OAAO,CAAC;cACb,IAAI;;AAEX;;;;"}
|
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { trace, context, SpanKind, SpanStatusCode } from '@opentelemetry/api';
|
|
|
3
3
|
import { InstrumentationBase, InstrumentationNodeModuleDefinition, safeExecuteInTheMiddle } from '@opentelemetry/instrumentation';
|
|
4
4
|
import { SpanAttributes, CONTEXT_KEY_ALLOW_TRACE_CONTENT } from '@traceloop/ai-semantic-conventions';
|
|
5
5
|
|
|
6
|
-
var version = "0.11.
|
|
6
|
+
var version = "0.11.6";
|
|
7
7
|
|
|
8
8
|
class AnthropicInstrumentation extends InstrumentationBase {
|
|
9
9
|
constructor(config = {}) {
|
|
@@ -35,7 +35,7 @@ class AnthropicInstrumentation extends InstrumentationBase {
|
|
|
35
35
|
patchAnthropic(type) {
|
|
36
36
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
37
37
|
const plugin = this;
|
|
38
|
-
// eslint-disable-next-line
|
|
38
|
+
// eslint-disable-next-line
|
|
39
39
|
return (original) => {
|
|
40
40
|
return function method(...args) {
|
|
41
41
|
const span = type === "chat"
|
|
@@ -129,10 +129,10 @@ class AnthropicInstrumentation extends InstrumentationBase {
|
|
|
129
129
|
attributes,
|
|
130
130
|
});
|
|
131
131
|
}
|
|
132
|
-
_streamingWrapPromise(
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
var
|
|
132
|
+
_streamingWrapPromise(_a) {
|
|
133
|
+
return __asyncGenerator(this, arguments, function* _streamingWrapPromise_1({ span, type, promise, }) {
|
|
134
|
+
var _b, e_1, _c, _d, _e, e_2, _f, _g;
|
|
135
|
+
var _h, _j, _k, _l;
|
|
136
136
|
if (type === "chat") {
|
|
137
137
|
const result = {
|
|
138
138
|
id: "0",
|
|
@@ -145,10 +145,10 @@ class AnthropicInstrumentation extends InstrumentationBase {
|
|
|
145
145
|
content: [],
|
|
146
146
|
};
|
|
147
147
|
try {
|
|
148
|
-
for (var
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
const chunk =
|
|
148
|
+
for (var _m = true, _o = __asyncValues(yield __await(promise)), _p; _p = yield __await(_o.next()), _b = _p.done, !_b; _m = true) {
|
|
149
|
+
_d = _p.value;
|
|
150
|
+
_m = false;
|
|
151
|
+
const chunk = _d;
|
|
152
152
|
yield yield __await(chunk);
|
|
153
153
|
try {
|
|
154
154
|
switch (chunk.type) {
|
|
@@ -159,23 +159,27 @@ class AnthropicInstrumentation extends InstrumentationBase {
|
|
|
159
159
|
break;
|
|
160
160
|
case "content_block_delta":
|
|
161
161
|
if (chunk.index < result.content.length) {
|
|
162
|
-
result.content[chunk.index]
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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
|
+
}
|
|
166
170
|
}
|
|
167
171
|
}
|
|
168
172
|
}
|
|
169
173
|
catch (e) {
|
|
170
174
|
this._diag.debug(e);
|
|
171
|
-
(
|
|
175
|
+
(_j = (_h = this._config).exceptionLogger) === null || _j === void 0 ? void 0 : _j.call(_h, e);
|
|
172
176
|
}
|
|
173
177
|
}
|
|
174
178
|
}
|
|
175
179
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
176
180
|
finally {
|
|
177
181
|
try {
|
|
178
|
-
if (!
|
|
182
|
+
if (!_m && !_b && (_c = _o.return)) yield __await(_c.call(_o));
|
|
179
183
|
}
|
|
180
184
|
finally { if (e_1) throw e_1.error; }
|
|
181
185
|
}
|
|
@@ -190,10 +194,10 @@ class AnthropicInstrumentation extends InstrumentationBase {
|
|
|
190
194
|
stop_reason: null,
|
|
191
195
|
};
|
|
192
196
|
try {
|
|
193
|
-
for (var
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const chunk =
|
|
197
|
+
for (var _q = true, _r = __asyncValues(yield __await(promise)), _s; _s = yield __await(_r.next()), _e = _s.done, !_e; _q = true) {
|
|
198
|
+
_g = _s.value;
|
|
199
|
+
_q = false;
|
|
200
|
+
const chunk = _g;
|
|
197
201
|
yield yield __await(chunk);
|
|
198
202
|
try {
|
|
199
203
|
result.id = chunk.id;
|
|
@@ -210,14 +214,14 @@ class AnthropicInstrumentation extends InstrumentationBase {
|
|
|
210
214
|
}
|
|
211
215
|
catch (e) {
|
|
212
216
|
this._diag.debug(e);
|
|
213
|
-
(
|
|
217
|
+
(_l = (_k = this._config).exceptionLogger) === null || _l === void 0 ? void 0 : _l.call(_k, e);
|
|
214
218
|
}
|
|
215
219
|
}
|
|
216
220
|
}
|
|
217
221
|
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
218
222
|
finally {
|
|
219
223
|
try {
|
|
220
|
-
if (!
|
|
224
|
+
if (!_q && !_e && (_f = _r.return)) yield __await(_f.call(_r));
|
|
221
225
|
}
|
|
222
226
|
finally { if (e_2) throw e_2.error; }
|
|
223
227
|
}
|
|
@@ -267,8 +271,9 @@ class AnthropicInstrumentation extends InstrumentationBase {
|
|
|
267
271
|
span.setAttribute(SpanAttributes.LLM_USAGE_COMPLETION_TOKENS, (_c = result.usage) === null || _c === void 0 ? void 0 : _c.output_tokens);
|
|
268
272
|
span.setAttribute(SpanAttributes.LLM_USAGE_PROMPT_TOKENS, (_d = result.usage) === null || _d === void 0 ? void 0 : _d.input_tokens);
|
|
269
273
|
}
|
|
270
|
-
result.stop_reason
|
|
274
|
+
if (result.stop_reason) {
|
|
271
275
|
span.setAttribute(`${SpanAttributes.LLM_COMPLETIONS}.0.finish_reason`, result.stop_reason);
|
|
276
|
+
}
|
|
272
277
|
if (this._shouldSendPrompts()) {
|
|
273
278
|
if (type === "chat") {
|
|
274
279
|
span.setAttribute(`${SpanAttributes.LLM_COMPLETIONS}.0.role`, "assistant");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@traceloop/instrumentation-anthropic",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.6",
|
|
4
4
|
"description": "Anthropic Instrumentaion",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
"repository": "traceloop/openllmetry-js",
|
|
9
9
|
"scripts": {
|
|
10
10
|
"build": "rollup -c",
|
|
11
|
-
"lint": "eslint .
|
|
12
|
-
"lint:fix": "eslint . --
|
|
11
|
+
"lint": "eslint .",
|
|
12
|
+
"lint:fix": "eslint . --fix",
|
|
13
13
|
"test": "ts-mocha -p tsconfig.json 'test/**/*.test.ts'"
|
|
14
14
|
},
|
|
15
15
|
"keywords": [
|
|
@@ -38,20 +38,20 @@
|
|
|
38
38
|
"access": "public"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@opentelemetry/core": "^1.
|
|
42
|
-
"@opentelemetry/instrumentation": "^0.
|
|
43
|
-
"@opentelemetry/semantic-conventions": "^1.
|
|
44
|
-
"@traceloop/ai-semantic-conventions": "^0.11.
|
|
45
|
-
"tslib": "^2.
|
|
41
|
+
"@opentelemetry/core": "^1.29.0",
|
|
42
|
+
"@opentelemetry/instrumentation": "^0.56.0",
|
|
43
|
+
"@opentelemetry/semantic-conventions": "^1.28.0",
|
|
44
|
+
"@traceloop/ai-semantic-conventions": "^0.11.6",
|
|
45
|
+
"tslib": "^2.8.1"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@anthropic-ai/sdk": "^0.
|
|
48
|
+
"@anthropic-ai/sdk": "^0.32.1",
|
|
49
49
|
"@pollyjs/adapter-node-http": "^6.0.6",
|
|
50
50
|
"@pollyjs/core": "^6.0.6",
|
|
51
51
|
"@pollyjs/persister-fs": "^6.0.6",
|
|
52
|
-
"@types/mocha": "^10.0.
|
|
52
|
+
"@types/mocha": "^10.0.10",
|
|
53
53
|
"ts-mocha": "^10.0.0"
|
|
54
54
|
},
|
|
55
55
|
"homepage": "https://github.com/traceloop/openllmetry-js/tree/main/packages/instrumentation-anthropic",
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "6196816b38d5438b2d71bdf5ea2275ac7cb6e14b"
|
|
57
57
|
}
|