Skip to content

Commit 5890edb

Browse files
committed
pretty
1 parent d2921fc commit 5890edb

File tree

33 files changed

+358
-1093
lines changed

33 files changed

+358
-1093
lines changed

packages/ai-semantic-conventions/src/SemanticAttributes.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
1817
export const SpanAttributes = {
1918
// Attributes not yet in @opentelemetry/semantic-conventions
2019
GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS:

packages/instrumentation-anthropic/src/instrumentation.ts

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ export class AnthropicInstrumentation extends InstrumentationBase {
222222

223223
try {
224224
attributes[ATTR_GEN_AI_REQUEST_MODEL] = params.model;
225-
attributes[ATTR_GEN_AI_REQUEST_TEMPERATURE] =
226-
params.temperature;
225+
attributes[ATTR_GEN_AI_REQUEST_TEMPERATURE] = params.temperature;
227226
attributes[ATTR_GEN_AI_REQUEST_TOP_P] = params.top_p;
228227
attributes[SpanAttributes.LLM_TOP_K] = params.top_k;
229228

@@ -239,8 +238,7 @@ export class AnthropicInstrumentation extends InstrumentationBase {
239238
attributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] =
240239
params.max_tokens_to_sample;
241240
} else {
242-
attributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] =
243-
params.max_tokens;
241+
attributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] = params.max_tokens;
244242
}
245243

246244
if (
@@ -258,8 +256,7 @@ export class AnthropicInstrumentation extends InstrumentationBase {
258256

259257
// If a system prompt is provided, it should always be first
260258
if ("system" in params && params.system !== undefined) {
261-
attributes[`${ATTR_GEN_AI_PROMPT}.0.role`] =
262-
"system";
259+
attributes[`${ATTR_GEN_AI_PROMPT}.0.role`] = "system";
263260
attributes[`${ATTR_GEN_AI_PROMPT}.0.content`] =
264261
typeof params.system === "string"
265262
? params.system
@@ -269,23 +266,19 @@ export class AnthropicInstrumentation extends InstrumentationBase {
269266

270267
params.messages.forEach((message, index) => {
271268
const currentIndex = index + promptIndex;
272-
attributes[
273-
`${ATTR_GEN_AI_PROMPT}.${currentIndex}.role`
274-
] = message.role;
269+
attributes[`${ATTR_GEN_AI_PROMPT}.${currentIndex}.role`] =
270+
message.role;
275271
if (typeof message.content === "string") {
276-
attributes[
277-
`${ATTR_GEN_AI_PROMPT}.${currentIndex}.content`
278-
] = (message.content as string) || "";
272+
attributes[`${ATTR_GEN_AI_PROMPT}.${currentIndex}.content`] =
273+
(message.content as string) || "";
279274
} else {
280-
attributes[
281-
`${ATTR_GEN_AI_PROMPT}.${currentIndex}.content`
282-
] = JSON.stringify(message.content);
275+
attributes[`${ATTR_GEN_AI_PROMPT}.${currentIndex}.content`] =
276+
JSON.stringify(message.content);
283277
}
284278
});
285279
} else {
286280
attributes[`${ATTR_GEN_AI_PROMPT}.0.role`] = "user";
287-
attributes[`${ATTR_GEN_AI_PROMPT}.0.content`] =
288-
params.prompt;
281+
attributes[`${ATTR_GEN_AI_PROMPT}.0.content`] = params.prompt;
289282
}
290283
}
291284
} catch (e) {
@@ -494,10 +487,7 @@ export class AnthropicInstrumentation extends InstrumentationBase {
494487
result: Completion;
495488
}) {
496489
try {
497-
span.setAttribute(
498-
ATTR_GEN_AI_RESPONSE_MODEL,
499-
result.model,
500-
);
490+
span.setAttribute(ATTR_GEN_AI_RESPONSE_MODEL, result.model);
501491
if (type === "chat" && result.usage) {
502492
span.setAttribute(
503493
SpanAttributes.LLM_USAGE_TOTAL_TOKENS,
@@ -522,19 +512,13 @@ export class AnthropicInstrumentation extends InstrumentationBase {
522512

523513
if (this._shouldSendPrompts()) {
524514
if (type === "chat") {
525-
span.setAttribute(
526-
`${ATTR_GEN_AI_COMPLETION}.0.role`,
527-
"assistant",
528-
);
515+
span.setAttribute(`${ATTR_GEN_AI_COMPLETION}.0.role`, "assistant");
529516
span.setAttribute(
530517
`${ATTR_GEN_AI_COMPLETION}.0.content`,
531518
JSON.stringify(result.content),
532519
);
533520
} else {
534-
span.setAttribute(
535-
`${ATTR_GEN_AI_COMPLETION}.0.role`,
536-
"assistant",
537-
);
521+
span.setAttribute(`${ATTR_GEN_AI_COMPLETION}.0.role`, "assistant");
538522
span.setAttribute(
539523
`${ATTR_GEN_AI_COMPLETION}.0.content`,
540524
result.completion,

packages/instrumentation-anthropic/test/instrumentation.test.ts

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,13 @@ describe("Test Anthropic instrumentation", async function () {
142142
chatSpan.attributes[`${ATTR_GEN_AI_COMPLETION}.0.content`],
143143
JSON.stringify(message.content),
144144
);
145-
assert.equal(
146-
chatSpan.attributes[`${ATTR_GEN_AI_USAGE_PROMPT_TOKENS}`],
147-
17,
148-
);
145+
assert.equal(chatSpan.attributes[`${ATTR_GEN_AI_USAGE_PROMPT_TOKENS}`], 17);
149146
assert.ok(
150-
+chatSpan.attributes[
151-
`${ATTR_GEN_AI_USAGE_COMPLETION_TOKENS}`
152-
]! > 0,
147+
+chatSpan.attributes[`${ATTR_GEN_AI_USAGE_COMPLETION_TOKENS}`]! > 0,
153148
);
154149
assert.equal(
155-
+chatSpan.attributes[
156-
`${ATTR_GEN_AI_USAGE_PROMPT_TOKENS}`
157-
]! +
158-
+chatSpan.attributes[
159-
`${ATTR_GEN_AI_USAGE_COMPLETION_TOKENS}`
160-
]!,
150+
+chatSpan.attributes[`${ATTR_GEN_AI_USAGE_PROMPT_TOKENS}`]! +
151+
+chatSpan.attributes[`${ATTR_GEN_AI_USAGE_COMPLETION_TOKENS}`]!,
161152
chatSpan.attributes[`${SpanAttributes.LLM_USAGE_TOTAL_TOKENS}`],
162153
);
163154
}).timeout(30000);
@@ -205,22 +196,13 @@ describe("Test Anthropic instrumentation", async function () {
205196
chatSpan.attributes[`${ATTR_GEN_AI_COMPLETION}.0.content`],
206197
JSON.stringify(message.content),
207198
);
208-
assert.equal(
209-
chatSpan.attributes[`${ATTR_GEN_AI_USAGE_PROMPT_TOKENS}`],
210-
17,
211-
);
199+
assert.equal(chatSpan.attributes[`${ATTR_GEN_AI_USAGE_PROMPT_TOKENS}`], 17);
212200
assert.ok(
213-
+chatSpan.attributes[
214-
`${ATTR_GEN_AI_USAGE_COMPLETION_TOKENS}`
215-
]! > 0,
201+
+chatSpan.attributes[`${ATTR_GEN_AI_USAGE_COMPLETION_TOKENS}`]! > 0,
216202
);
217203
assert.equal(
218-
+chatSpan.attributes[
219-
`${ATTR_GEN_AI_USAGE_PROMPT_TOKENS}`
220-
]! +
221-
+chatSpan.attributes[
222-
`${ATTR_GEN_AI_USAGE_COMPLETION_TOKENS}`
223-
]!,
204+
+chatSpan.attributes[`${ATTR_GEN_AI_USAGE_PROMPT_TOKENS}`]! +
205+
+chatSpan.attributes[`${ATTR_GEN_AI_USAGE_COMPLETION_TOKENS}`]!,
224206
chatSpan.attributes[`${SpanAttributes.LLM_USAGE_TOTAL_TOKENS}`],
225207
);
226208
}).timeout(30000);
@@ -248,10 +230,7 @@ describe("Test Anthropic instrumentation", async function () {
248230
span.attributes[`${ATTR_GEN_AI_PROMPT}.0.content`],
249231
"You are a helpful assistant",
250232
);
251-
assert.strictEqual(
252-
span.attributes[`${ATTR_GEN_AI_PROMPT}.1.role`],
253-
"user",
254-
);
233+
assert.strictEqual(span.attributes[`${ATTR_GEN_AI_PROMPT}.1.role`], "user");
255234
}).timeout(30000);
256235

257236
it("should set attributes in span for beta messages with thinking", async () => {
@@ -311,9 +290,7 @@ describe("Test Anthropic instrumentation", async function () {
311290

312291
// Check that we capture both thinking and regular content blocks
313292
const content = JSON.parse(
314-
chatSpan.attributes[
315-
`${ATTR_GEN_AI_COMPLETION}.0.content`
316-
] as string,
293+
chatSpan.attributes[`${ATTR_GEN_AI_COMPLETION}.0.content`] as string,
317294
);
318295
assert.ok(Array.isArray(content));
319296

@@ -340,9 +317,7 @@ describe("Test Anthropic instrumentation", async function () {
340317

341318
// Verify token usage includes thinking tokens
342319
const completionTokens =
343-
chatSpan.attributes[
344-
`${ATTR_GEN_AI_USAGE_COMPLETION_TOKENS}`
345-
];
320+
chatSpan.attributes[`${ATTR_GEN_AI_USAGE_COMPLETION_TOKENS}`];
346321
const promptTokens =
347322
chatSpan.attributes[`${ATTR_GEN_AI_USAGE_PROMPT_TOKENS}`];
348323
const totalTokens =

packages/instrumentation-bedrock/src/instrumentation.ts

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,7 @@ export class BedrockInstrumentation extends InstrumentationBase {
211211
: {};
212212

213213
if (ATTR_GEN_AI_SYSTEM in attributes) {
214-
const modelId = attributes[
215-
ATTR_GEN_AI_RESPONSE_MODEL
216-
] as string;
214+
const modelId = attributes[ATTR_GEN_AI_RESPONSE_MODEL] as string;
217215
const { modelVendor, model } = this._extractVendorAndModel(modelId);
218216

219217
span.setAttribute(ATTR_GEN_AI_RESPONSE_MODEL, model);
@@ -263,14 +261,11 @@ export class BedrockInstrumentation extends InstrumentationBase {
263261
if (this._shouldSendPrompts()) {
264262
// Update local value with attribute value that was set by _setResponseAttributes
265263
streamedContent +=
266-
responseAttributes[
267-
`${ATTR_GEN_AI_COMPLETION}.0.content`
268-
];
264+
responseAttributes[`${ATTR_GEN_AI_COMPLETION}.0.content`];
269265
// re-assign the new value to responseAttributes
270266
responseAttributes = {
271267
...responseAttributes,
272-
[`${ATTR_GEN_AI_COMPLETION}.0.content`]:
273-
streamedContent,
268+
[`${ATTR_GEN_AI_COMPLETION}.0.content`]: streamedContent,
274269
};
275270
}
276271

@@ -310,10 +305,8 @@ export class BedrockInstrumentation extends InstrumentationBase {
310305
case "ai21": {
311306
return {
312307
[ATTR_GEN_AI_REQUEST_TOP_P]: requestBody["topP"],
313-
[ATTR_GEN_AI_REQUEST_TEMPERATURE]:
314-
requestBody["temperature"],
315-
[ATTR_GEN_AI_REQUEST_MAX_TOKENS]:
316-
requestBody["maxTokens"],
308+
[ATTR_GEN_AI_REQUEST_TEMPERATURE]: requestBody["temperature"],
309+
[ATTR_GEN_AI_REQUEST_MAX_TOKENS]: requestBody["maxTokens"],
317310
[SpanAttributes.LLM_PRESENCE_PENALTY]:
318311
requestBody["presencePenalty"]["scale"],
319312
[SpanAttributes.LLM_FREQUENCY_PENALTY]:
@@ -323,8 +316,7 @@ export class BedrockInstrumentation extends InstrumentationBase {
323316
...(this._shouldSendPrompts()
324317
? {
325318
[`${ATTR_GEN_AI_PROMPT}.0.role`]: "user",
326-
[`${ATTR_GEN_AI_PROMPT}.0.content`]:
327-
requestBody["prompt"],
319+
[`${ATTR_GEN_AI_PROMPT}.0.content`]: requestBody["prompt"],
328320
}
329321
: {}),
330322
};
@@ -342,8 +334,7 @@ export class BedrockInstrumentation extends InstrumentationBase {
342334
...(this._shouldSendPrompts()
343335
? {
344336
[`${ATTR_GEN_AI_PROMPT}.0.role`]: "user",
345-
[`${ATTR_GEN_AI_PROMPT}.0.content`]:
346-
requestBody["inputText"],
337+
[`${ATTR_GEN_AI_PROMPT}.0.content`]: requestBody["inputText"],
347338
}
348339
: {}),
349340
};
@@ -352,8 +343,7 @@ export class BedrockInstrumentation extends InstrumentationBase {
352343
const baseAttributes = {
353344
[ATTR_GEN_AI_REQUEST_TOP_P]: requestBody["top_p"],
354345
[SpanAttributes.LLM_TOP_K]: requestBody["top_k"],
355-
[ATTR_GEN_AI_REQUEST_TEMPERATURE]:
356-
requestBody["temperature"],
346+
[ATTR_GEN_AI_REQUEST_TEMPERATURE]: requestBody["temperature"],
357347
[ATTR_GEN_AI_REQUEST_MAX_TOKENS]:
358348
requestBody["max_tokens_to_sample"] || requestBody["max_tokens"],
359349
};
@@ -366,12 +356,9 @@ export class BedrockInstrumentation extends InstrumentationBase {
366356
if (requestBody["messages"]) {
367357
const promptAttributes: Record<string, any> = {};
368358
requestBody["messages"].forEach((message: any, index: number) => {
369-
promptAttributes[
370-
`${ATTR_GEN_AI_PROMPT}.${index}.role`
371-
] = message.role;
372-
promptAttributes[
373-
`${ATTR_GEN_AI_PROMPT}.${index}.content`
374-
] =
359+
promptAttributes[`${ATTR_GEN_AI_PROMPT}.${index}.role`] =
360+
message.role;
361+
promptAttributes[`${ATTR_GEN_AI_PROMPT}.${index}.content`] =
375362
typeof message.content === "string"
376363
? message.content
377364
: JSON.stringify(message.content);
@@ -384,9 +371,7 @@ export class BedrockInstrumentation extends InstrumentationBase {
384371
return {
385372
...baseAttributes,
386373
[`${ATTR_GEN_AI_PROMPT}.0.role`]: "user",
387-
[`${ATTR_GEN_AI_PROMPT}.0.content`]: requestBody[
388-
"prompt"
389-
]
374+
[`${ATTR_GEN_AI_PROMPT}.0.content`]: requestBody["prompt"]
390375
// The format is removing when we are setting span attribute
391376
.replace("\n\nHuman:", "")
392377
.replace("\n\nAssistant:", ""),
@@ -399,10 +384,8 @@ export class BedrockInstrumentation extends InstrumentationBase {
399384
return {
400385
[ATTR_GEN_AI_REQUEST_TOP_P]: requestBody["p"],
401386
[SpanAttributes.LLM_TOP_K]: requestBody["k"],
402-
[ATTR_GEN_AI_REQUEST_TEMPERATURE]:
403-
requestBody["temperature"],
404-
[ATTR_GEN_AI_REQUEST_MAX_TOKENS]:
405-
requestBody["max_tokens"],
387+
[ATTR_GEN_AI_REQUEST_TEMPERATURE]: requestBody["temperature"],
388+
[ATTR_GEN_AI_REQUEST_MAX_TOKENS]: requestBody["max_tokens"],
406389

407390
// Prompt & Role
408391
...(this._shouldSendPrompts()
@@ -417,17 +400,14 @@ export class BedrockInstrumentation extends InstrumentationBase {
417400
case "meta": {
418401
return {
419402
[ATTR_GEN_AI_REQUEST_TOP_P]: requestBody["top_p"],
420-
[ATTR_GEN_AI_REQUEST_TEMPERATURE]:
421-
requestBody["temperature"],
422-
[ATTR_GEN_AI_REQUEST_MAX_TOKENS]:
423-
requestBody["max_gen_len"],
403+
[ATTR_GEN_AI_REQUEST_TEMPERATURE]: requestBody["temperature"],
404+
[ATTR_GEN_AI_REQUEST_MAX_TOKENS]: requestBody["max_gen_len"],
424405

425406
// Prompt & Role
426407
...(this._shouldSendPrompts()
427408
? {
428409
[`${ATTR_GEN_AI_PROMPT}.0.role`]: "user",
429-
[`${ATTR_GEN_AI_PROMPT}.0.content`]:
430-
requestBody["prompt"],
410+
[`${ATTR_GEN_AI_PROMPT}.0.content`]: requestBody["prompt"],
431411
}
432412
: {}),
433413
};
@@ -462,8 +442,7 @@ export class BedrockInstrumentation extends InstrumentationBase {
462442
? response["completionReason"]
463443
: response["results"][0]["completionReason"],
464444
[`${ATTR_GEN_AI_COMPLETION}.0.role`]: "assistant",
465-
[ATTR_GEN_AI_USAGE_PROMPT_TOKENS]:
466-
response["inputTextTokenCount"],
445+
[ATTR_GEN_AI_USAGE_PROMPT_TOKENS]: response["inputTextTokenCount"],
467446
[ATTR_GEN_AI_USAGE_COMPLETION_TOKENS]: isStream
468447
? response["totalOutputTextTokenCount"]
469448
: response["results"][0]["tokenCount"],
@@ -507,8 +486,7 @@ export class BedrockInstrumentation extends InstrumentationBase {
507486
if (response["completion"]) {
508487
return {
509488
...baseAttributes,
510-
[`${ATTR_GEN_AI_COMPLETION}.0.content`]:
511-
response["completion"],
489+
[`${ATTR_GEN_AI_COMPLETION}.0.content`]: response["completion"],
512490
};
513491
}
514492

@@ -532,10 +510,8 @@ export class BedrockInstrumentation extends InstrumentationBase {
532510
const billedUnits = response["meta"]["billed_units"];
533511
return {
534512
...baseAttributes,
535-
[ATTR_GEN_AI_USAGE_PROMPT_TOKENS]:
536-
billedUnits["input_tokens"],
537-
[ATTR_GEN_AI_USAGE_COMPLETION_TOKENS]:
538-
billedUnits["output_tokens"],
513+
[ATTR_GEN_AI_USAGE_PROMPT_TOKENS]: billedUnits["input_tokens"],
514+
[ATTR_GEN_AI_USAGE_COMPLETION_TOKENS]: billedUnits["output_tokens"],
539515
[SpanAttributes.LLM_USAGE_TOTAL_TOKENS]:
540516
(billedUnits["input_tokens"] || 0) +
541517
(billedUnits["output_tokens"] || 0),
@@ -549,16 +525,14 @@ export class BedrockInstrumentation extends InstrumentationBase {
549525
[`${ATTR_GEN_AI_COMPLETION}.0.finish_reason`]:
550526
response["stop_reason"],
551527
[`${ATTR_GEN_AI_COMPLETION}.0.role`]: "assistant",
552-
[ATTR_GEN_AI_USAGE_PROMPT_TOKENS]:
553-
response["prompt_token_count"],
528+
[ATTR_GEN_AI_USAGE_PROMPT_TOKENS]: response["prompt_token_count"],
554529
[ATTR_GEN_AI_USAGE_COMPLETION_TOKENS]:
555530
response["generation_token_count"],
556531
[SpanAttributes.LLM_USAGE_TOTAL_TOKENS]:
557532
response["prompt_token_count"] + response["generation_token_count"],
558533
...(this._shouldSendPrompts()
559534
? {
560-
[`${ATTR_GEN_AI_COMPLETION}.0.content`]:
561-
response["generation"],
535+
[`${ATTR_GEN_AI_COMPLETION}.0.content`]: response["generation"],
562536
}
563537
: {}),
564538
};

0 commit comments

Comments
 (0)