@traceloop/instrumentation-anthropic 0.6.0 → 0.7.0

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.d.ts CHANGED
@@ -7,6 +7,10 @@ interface AnthropicInstrumentationConfig extends InstrumentationConfig {
7
7
  * @default true
8
8
  */
9
9
  traceContent?: boolean;
10
+ /**
11
+ * A custom logger to log any exceptions that happen during span creation.
12
+ */
13
+ exceptionLogger?: (e: Error) => void;
10
14
  }
11
15
 
12
16
  declare class AnthropicInstrumentation extends InstrumentationBase<any> {
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.6.0";
8
+ var version = "0.7.0";
9
9
 
10
10
  class AnthropicInstrumentation extends instrumentation.InstrumentationBase {
11
11
  constructor(config = {}) {
@@ -78,55 +78,63 @@ class AnthropicInstrumentation extends instrumentation.InstrumentationBase {
78
78
  };
79
79
  }
80
80
  startSpan({ type, params, }) {
81
+ var _a, _b;
81
82
  const attributes = {
82
83
  [aiSemanticConventions.SpanAttributes.LLM_VENDOR]: "Anthropic",
83
84
  [aiSemanticConventions.SpanAttributes.LLM_REQUEST_TYPE]: type,
84
85
  };
85
- attributes[aiSemanticConventions.SpanAttributes.LLM_REQUEST_MODEL] = params.model;
86
- attributes[aiSemanticConventions.SpanAttributes.LLM_TEMPERATURE] = params.temperature;
87
- attributes[aiSemanticConventions.SpanAttributes.LLM_TOP_P] = params.top_p;
88
- attributes[aiSemanticConventions.SpanAttributes.LLM_TOP_K] = params.top_k;
89
- if (type === "completion") {
90
- attributes[aiSemanticConventions.SpanAttributes.LLM_REQUEST_MAX_TOKENS] =
91
- params.max_tokens_to_sample;
92
- }
93
- else {
94
- attributes[aiSemanticConventions.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[`${aiSemanticConventions.SpanAttributes.LLM_PROMPTS}.${index}.role`] =
106
- message.role;
107
- if (typeof message.content === "string") {
108
- attributes[`${aiSemanticConventions.SpanAttributes.LLM_PROMPTS}.${index}.content`] =
109
- message.content || "";
110
- }
111
- else {
112
- attributes[`${aiSemanticConventions.SpanAttributes.LLM_PROMPTS}.${index}.content`] =
113
- JSON.stringify(message.content);
114
- }
115
- });
86
+ try {
87
+ attributes[aiSemanticConventions.SpanAttributes.LLM_REQUEST_MODEL] = params.model;
88
+ attributes[aiSemanticConventions.SpanAttributes.LLM_TEMPERATURE] = params.temperature;
89
+ attributes[aiSemanticConventions.SpanAttributes.LLM_TOP_P] = params.top_p;
90
+ attributes[aiSemanticConventions.SpanAttributes.LLM_TOP_K] = params.top_k;
91
+ if (type === "completion") {
92
+ attributes[aiSemanticConventions.SpanAttributes.LLM_REQUEST_MAX_TOKENS] =
93
+ params.max_tokens_to_sample;
116
94
  }
117
95
  else {
118
- attributes[`${aiSemanticConventions.SpanAttributes.LLM_PROMPTS}.0.role`] = "user";
119
- attributes[`${aiSemanticConventions.SpanAttributes.LLM_PROMPTS}.0.content`] = params.prompt;
96
+ attributes[aiSemanticConventions.SpanAttributes.LLM_REQUEST_MAX_TOKENS] = params.max_tokens;
97
+ }
98
+ if (params.extraAttributes !== undefined &&
99
+ typeof params.extraAttributes === "object") {
100
+ Object.keys(params.extraAttributes).forEach((key) => {
101
+ attributes[key] = params.extraAttributes[key];
102
+ });
103
+ }
104
+ if (this._shouldSendPrompts()) {
105
+ if (type === "chat") {
106
+ params.messages.forEach((message, index) => {
107
+ attributes[`${aiSemanticConventions.SpanAttributes.LLM_PROMPTS}.${index}.role`] =
108
+ message.role;
109
+ if (typeof message.content === "string") {
110
+ attributes[`${aiSemanticConventions.SpanAttributes.LLM_PROMPTS}.${index}.content`] =
111
+ message.content || "";
112
+ }
113
+ else {
114
+ attributes[`${aiSemanticConventions.SpanAttributes.LLM_PROMPTS}.${index}.content`] =
115
+ JSON.stringify(message.content);
116
+ }
117
+ });
118
+ }
119
+ else {
120
+ attributes[`${aiSemanticConventions.SpanAttributes.LLM_PROMPTS}.0.role`] = "user";
121
+ attributes[`${aiSemanticConventions.SpanAttributes.LLM_PROMPTS}.0.content`] = params.prompt;
122
+ }
120
123
  }
121
124
  }
125
+ catch (e) {
126
+ this._diag.warn(e);
127
+ (_b = (_a = this._config).exceptionLogger) === null || _b === void 0 ? void 0 : _b.call(_a, e);
128
+ }
122
129
  return this.tracer.startSpan(`anthropic.${type}`, {
123
130
  kind: api.SpanKind.CLIENT,
124
131
  attributes,
125
132
  });
126
133
  }
127
134
  _streamingWrapPromise({ span, type, promise, }) {
135
+ var _a, _b, _c, _d;
128
136
  return tslib.__asyncGenerator(this, arguments, function* _streamingWrapPromise_1() {
129
- var _a, e_1, _b, _c, _d, e_2, _e, _f;
137
+ var _e, e_1, _f, _g, _h, e_2, _j, _k;
130
138
  if (type === "chat") {
131
139
  const result = {
132
140
  id: "0",
@@ -139,31 +147,37 @@ class AnthropicInstrumentation extends instrumentation.InstrumentationBase {
139
147
  content: [],
140
148
  };
141
149
  try {
142
- for (var _g = true, _h = tslib.__asyncValues(yield tslib.__await(promise)), _j; _j = yield tslib.__await(_h.next()), _a = _j.done, !_a; _g = true) {
143
- _c = _j.value;
144
- _g = false;
145
- const chunk = _c;
150
+ for (var _l = true, _m = tslib.__asyncValues(yield tslib.__await(promise)), _o; _o = yield tslib.__await(_m.next()), _e = _o.done, !_e; _l = true) {
151
+ _g = _o.value;
152
+ _l = false;
153
+ const chunk = _g;
146
154
  yield yield tslib.__await(chunk);
147
- switch (chunk.type) {
148
- case "content_block_start":
149
- if (result.content.length <= chunk.index) {
150
- result.content.push(chunk.content_block);
151
- }
152
- break;
153
- case "content_block_delta":
154
- if (chunk.index < result.content.length) {
155
- result.content[chunk.index] = {
156
- type: "text",
157
- text: result.content[chunk.index].text + chunk.delta.text,
158
- };
159
- }
155
+ try {
156
+ switch (chunk.type) {
157
+ case "content_block_start":
158
+ if (result.content.length <= chunk.index) {
159
+ result.content.push(chunk.content_block);
160
+ }
161
+ break;
162
+ case "content_block_delta":
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
+ };
168
+ }
169
+ }
170
+ }
171
+ catch (e) {
172
+ this._diag.warn(e);
173
+ (_b = (_a = this._config).exceptionLogger) === null || _b === void 0 ? void 0 : _b.call(_a, e);
160
174
  }
161
175
  }
162
176
  }
163
177
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
164
178
  finally {
165
179
  try {
166
- if (!_g && !_a && (_b = _h.return)) yield tslib.__await(_b.call(_h));
180
+ if (!_l && !_e && (_f = _m.return)) yield tslib.__await(_f.call(_m));
167
181
  }
168
182
  finally { if (e_1) throw e_1.error; }
169
183
  }
@@ -178,28 +192,34 @@ class AnthropicInstrumentation extends instrumentation.InstrumentationBase {
178
192
  stop_reason: null,
179
193
  };
180
194
  try {
181
- for (var _k = true, _l = tslib.__asyncValues(yield tslib.__await(promise)), _m; _m = yield tslib.__await(_l.next()), _d = _m.done, !_d; _k = true) {
182
- _f = _m.value;
183
- _k = false;
184
- const chunk = _f;
195
+ for (var _p = true, _q = tslib.__asyncValues(yield tslib.__await(promise)), _r; _r = yield tslib.__await(_q.next()), _h = _r.done, !_h; _p = true) {
196
+ _k = _r.value;
197
+ _p = false;
198
+ const chunk = _k;
185
199
  yield yield tslib.__await(chunk);
186
- result.id = chunk.id;
187
- result.model = chunk.model;
188
- if (chunk.stop_reason) {
189
- result.stop_reason = chunk.stop_reason;
190
- }
191
- if (chunk.model) {
200
+ try {
201
+ result.id = chunk.id;
192
202
  result.model = chunk.model;
203
+ if (chunk.stop_reason) {
204
+ result.stop_reason = chunk.stop_reason;
205
+ }
206
+ if (chunk.model) {
207
+ result.model = chunk.model;
208
+ }
209
+ if (chunk.completion) {
210
+ result.completion += chunk.completion;
211
+ }
193
212
  }
194
- if (chunk.completion) {
195
- result.completion += chunk.completion;
213
+ catch (e) {
214
+ this._diag.warn(e);
215
+ (_d = (_c = this._config).exceptionLogger) === null || _d === void 0 ? void 0 : _d.call(_c, e);
196
216
  }
197
217
  }
198
218
  }
199
219
  catch (e_2_1) { e_2 = { error: e_2_1 }; }
200
220
  finally {
201
221
  try {
202
- if (!_k && !_d && (_e = _l.return)) yield tslib.__await(_e.call(_l));
222
+ if (!_p && !_h && (_j = _q.return)) yield tslib.__await(_j.call(_q));
203
223
  }
204
224
  finally { if (e_2) throw e_2.error; }
205
225
  }
@@ -241,25 +261,31 @@ class AnthropicInstrumentation extends instrumentation.InstrumentationBase {
241
261
  });
242
262
  }
243
263
  _endSpan({ span, type, result, }) {
244
- var _a, _b, _c, _d;
245
- span.setAttribute(aiSemanticConventions.SpanAttributes.LLM_RESPONSE_MODEL, result.model);
246
- if (type === "chat" && result.usage) {
247
- span.setAttribute(aiSemanticConventions.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));
248
- span.setAttribute(aiSemanticConventions.SpanAttributes.LLM_USAGE_COMPLETION_TOKENS, (_c = result.usage) === null || _c === void 0 ? void 0 : _c.output_tokens);
249
- span.setAttribute(aiSemanticConventions.SpanAttributes.LLM_USAGE_PROMPT_TOKENS, (_d = result.usage) === null || _d === void 0 ? void 0 : _d.input_tokens);
250
- }
251
- result.stop_reason &&
252
- span.setAttribute(`${aiSemanticConventions.SpanAttributes.LLM_COMPLETIONS}.0.finish_reason`, result.stop_reason);
253
- if (this._shouldSendPrompts()) {
254
- if (type === "chat") {
255
- span.setAttribute(`${aiSemanticConventions.SpanAttributes.LLM_COMPLETIONS}.0.role`, "assistant");
256
- span.setAttribute(`${aiSemanticConventions.SpanAttributes.LLM_COMPLETIONS}.0.content`, JSON.stringify(result.content));
264
+ var _a, _b, _c, _d, _e, _f;
265
+ try {
266
+ span.setAttribute(aiSemanticConventions.SpanAttributes.LLM_RESPONSE_MODEL, result.model);
267
+ if (type === "chat" && result.usage) {
268
+ span.setAttribute(aiSemanticConventions.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));
269
+ span.setAttribute(aiSemanticConventions.SpanAttributes.LLM_USAGE_COMPLETION_TOKENS, (_c = result.usage) === null || _c === void 0 ? void 0 : _c.output_tokens);
270
+ span.setAttribute(aiSemanticConventions.SpanAttributes.LLM_USAGE_PROMPT_TOKENS, (_d = result.usage) === null || _d === void 0 ? void 0 : _d.input_tokens);
257
271
  }
258
- else {
259
- span.setAttribute(`${aiSemanticConventions.SpanAttributes.LLM_COMPLETIONS}.0.role`, "assistant");
260
- span.setAttribute(`${aiSemanticConventions.SpanAttributes.LLM_COMPLETIONS}.0.content`, result.completion);
272
+ result.stop_reason &&
273
+ span.setAttribute(`${aiSemanticConventions.SpanAttributes.LLM_COMPLETIONS}.0.finish_reason`, result.stop_reason);
274
+ if (this._shouldSendPrompts()) {
275
+ if (type === "chat") {
276
+ span.setAttribute(`${aiSemanticConventions.SpanAttributes.LLM_COMPLETIONS}.0.role`, "assistant");
277
+ span.setAttribute(`${aiSemanticConventions.SpanAttributes.LLM_COMPLETIONS}.0.content`, JSON.stringify(result.content));
278
+ }
279
+ else {
280
+ span.setAttribute(`${aiSemanticConventions.SpanAttributes.LLM_COMPLETIONS}.0.role`, "assistant");
281
+ span.setAttribute(`${aiSemanticConventions.SpanAttributes.LLM_COMPLETIONS}.0.content`, result.completion);
282
+ }
261
283
  }
262
284
  }
285
+ catch (e) {
286
+ this._diag.warn(e);
287
+ (_f = (_e = this._config).exceptionLogger) === null || _f === void 0 ? void 0 : _f.call(_e, e);
288
+ }
263
289
  span.end();
264
290
  }
265
291
  _shouldSendPrompts() {
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<any> {\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<any> {\n const module = new InstrumentationNodeModuleDefinition<any>(\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_VENDOR]: \"Anthropic\",\n [SpanAttributes.LLM_REQUEST_TYPE]: type,\n };\n\n attributes[SpanAttributes.LLM_REQUEST_MODEL] = params.model;\n attributes[SpanAttributes.LLM_TEMPERATURE] = params.temperature;\n attributes[SpanAttributes.LLM_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\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 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 }\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 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 }\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 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\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,mCAAwB,CAAA;AAGpE,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,CAAC,CAA+B,4BAAA,EAAA,aAAa,CAAE,CAAA,CAAC,CAAC;QAEjE,IAAI,CAAC,KAAK,C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CAClB,CAAC;aACH;SACF;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<any> {\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<any> {\n const module = new InstrumentationNodeModuleDefinition<any>(\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_VENDOR]: \"Anthropic\",\n [SpanAttributes.LLM_REQUEST_TYPE]: type,\n };\n\n try {\n attributes[SpanAttributes.LLM_REQUEST_MODEL] = params.model;\n attributes[SpanAttributes.LLM_TEMPERATURE] = params.temperature;\n attributes[SpanAttributes.LLM_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.warn(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.warn(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.warn(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.warn(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,mCAAwB,CAAA;AAGpE,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,CAA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c,CAAC,eAAe,CAAS,OAAA,CAAA,EAC1C,WAAW,CACZ,CAAC;AACF,oBAAA,IAAI,CAAC,YAAY,CACf,CAAA,EAAGA,oCAAc,CAAC,eAAe,CAAA,UAAA,CAAY,EAC7C,MAAM,CAAC,UAAU,CAClB,CAAC;iBACH;aACF;SACF;QAAC,OAAO,CAAC,EAAE;AACV,YAAA,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,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,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;;;;"}
@@ -5,5 +5,9 @@ export interface AnthropicInstrumentationConfig extends InstrumentationConfig {
5
5
  * @default true
6
6
  */
7
7
  traceContent?: boolean;
8
+ /**
9
+ * A custom logger to log any exceptions that happen during span creation.
10
+ */
11
+ exceptionLogger?: (e: Error) => void;
8
12
  }
9
13
  //# sourceMappingURL=types.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@traceloop/instrumentation-anthropic",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Anthropic Instrumentaion",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -40,7 +40,7 @@
40
40
  "@opentelemetry/core": "^1.22.0",
41
41
  "@opentelemetry/instrumentation": "^0.49.0",
42
42
  "@opentelemetry/semantic-conventions": "^1.22.0",
43
- "@traceloop/ai-semantic-conventions": "^0.6.0"
43
+ "@traceloop/ai-semantic-conventions": "^0.7.0"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@anthropic-ai/sdk": "^0.20.1",
@@ -51,5 +51,5 @@
51
51
  "ts-mocha": "^10.0.0"
52
52
  },
53
53
  "homepage": "https://github.com/traceloop/openllmetry-js/tree/main/packages/instrumentation-anthropic",
54
- "gitHead": "9ed5378bfca1cb20854824fd39b526d566307166"
54
+ "gitHead": "c79421048bc0bd47df0c4f94e3a6deb98cb06381"
55
55
  }