Skip to content

Commit 6cd7c1c

Browse files
committed
lint fixes
1 parent aeda8e6 commit 6cd7c1c

File tree

2 files changed

+129
-56
lines changed

2 files changed

+129
-56
lines changed

plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/bedrock-runtime.ts

Lines changed: 94 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
120120
requestBody.textGenerationConfig.temperature;
121121
}
122122
if (requestBody.textGenerationConfig?.topP !== undefined) {
123-
spanAttributes[ATTR_GEN_AI_REQUEST_TOP_P] = requestBody.textGenerationConfig.topP;
123+
spanAttributes[ATTR_GEN_AI_REQUEST_TOP_P] =
124+
requestBody.textGenerationConfig.topP;
124125
}
125126
if (requestBody.textGenerationConfig?.maxTokenCount !== undefined) {
126127
spanAttributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] =
@@ -132,47 +133,58 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
132133
}
133134
} else if (modelId.includes('amazon.nova')) {
134135
if (requestBody.inferenceConfig?.temperature !== undefined) {
135-
spanAttributes[ATTR_GEN_AI_REQUEST_TEMPERATURE] = requestBody.inferenceConfig.temperature;
136+
spanAttributes[ATTR_GEN_AI_REQUEST_TEMPERATURE] =
137+
requestBody.inferenceConfig.temperature;
136138
}
137139
if (requestBody.inferenceConfig?.top_p !== undefined) {
138-
spanAttributes[ATTR_GEN_AI_REQUEST_TOP_P] = requestBody.inferenceConfig.top_p;
140+
spanAttributes[ATTR_GEN_AI_REQUEST_TOP_P] =
141+
requestBody.inferenceConfig.top_p;
139142
}
140143
if (requestBody.inferenceConfig?.max_new_tokens !== undefined) {
141-
spanAttributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] = requestBody.inferenceConfig.max_new_tokens;
144+
spanAttributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] =
145+
requestBody.inferenceConfig.max_new_tokens;
142146
}
143147
if (requestBody.inferenceConfig?.stopSequences !== undefined) {
144-
spanAttributes[ATTR_GEN_AI_REQUEST_STOP_SEQUENCES] = requestBody.inferenceConfig.stopSequences;
148+
spanAttributes[ATTR_GEN_AI_REQUEST_STOP_SEQUENCES] =
149+
requestBody.inferenceConfig.stopSequences;
145150
}
146151
} else if (modelId.includes('anthropic.claude')) {
147152
if (requestBody.max_tokens !== undefined) {
148-
spanAttributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] = requestBody.max_tokens;
153+
spanAttributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] =
154+
requestBody.max_tokens;
149155
}
150156
if (requestBody.temperature !== undefined) {
151-
spanAttributes[ATTR_GEN_AI_REQUEST_TEMPERATURE] = requestBody.temperature;
157+
spanAttributes[ATTR_GEN_AI_REQUEST_TEMPERATURE] =
158+
requestBody.temperature;
152159
}
153160
if (requestBody.top_p !== undefined) {
154161
spanAttributes[ATTR_GEN_AI_REQUEST_TOP_P] = requestBody.top_p;
155162
}
156163
if (requestBody.stop_sequences !== undefined) {
157-
spanAttributes[ATTR_GEN_AI_REQUEST_STOP_SEQUENCES] = requestBody.stop_sequences;
164+
spanAttributes[ATTR_GEN_AI_REQUEST_STOP_SEQUENCES] =
165+
requestBody.stop_sequences;
158166
}
159167
} else if (modelId.includes('meta.llama')) {
160168
if (requestBody.max_gen_len !== undefined) {
161-
spanAttributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] = requestBody.max_gen_len;
169+
spanAttributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] =
170+
requestBody.max_gen_len;
162171
}
163172
if (requestBody.temperature !== undefined) {
164-
spanAttributes[ATTR_GEN_AI_REQUEST_TEMPERATURE] = requestBody.temperature;
173+
spanAttributes[ATTR_GEN_AI_REQUEST_TEMPERATURE] =
174+
requestBody.temperature;
165175
}
166176
if (requestBody.top_p !== undefined) {
167177
spanAttributes[ATTR_GEN_AI_REQUEST_TOP_P] = requestBody.top_p;
168178
}
169179
// request for meta llama models does not contain stop_sequences field
170180
} else if (modelId.includes('cohere.command-r')) {
171181
if (requestBody.max_tokens !== undefined) {
172-
spanAttributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] = requestBody.max_tokens;
182+
spanAttributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] =
183+
requestBody.max_tokens;
173184
}
174185
if (requestBody.temperature !== undefined) {
175-
spanAttributes[ATTR_GEN_AI_REQUEST_TEMPERATURE] = requestBody.temperature;
186+
spanAttributes[ATTR_GEN_AI_REQUEST_TEMPERATURE] =
187+
requestBody.temperature;
176188
}
177189
if (requestBody.p !== undefined) {
178190
spanAttributes[ATTR_GEN_AI_REQUEST_TOP_P] = requestBody.p;
@@ -181,17 +193,22 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
181193
// NOTE: We approximate the token count since this value is not directly available in the body
182194
// According to Bedrock docs they use (total_chars / 6) to approximate token count for pricing.
183195
// https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-prepare.html
184-
spanAttributes[ATTR_GEN_AI_USAGE_INPUT_TOKENS] = Math.ceil(requestBody.message.length / 6);
196+
spanAttributes[ATTR_GEN_AI_USAGE_INPUT_TOKENS] = Math.ceil(
197+
requestBody.message.length / 6
198+
);
185199
}
186200
if (requestBody.stop_sequences !== undefined) {
187-
spanAttributes[ATTR_GEN_AI_REQUEST_STOP_SEQUENCES] = requestBody.stop_sequences;
201+
spanAttributes[ATTR_GEN_AI_REQUEST_STOP_SEQUENCES] =
202+
requestBody.stop_sequences;
188203
}
189204
} else if (modelId.includes('cohere.command')) {
190205
if (requestBody.max_tokens !== undefined) {
191-
spanAttributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] = requestBody.max_tokens;
206+
spanAttributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] =
207+
requestBody.max_tokens;
192208
}
193209
if (requestBody.temperature !== undefined) {
194-
spanAttributes[ATTR_GEN_AI_REQUEST_TEMPERATURE] = requestBody.temperature;
210+
spanAttributes[ATTR_GEN_AI_REQUEST_TEMPERATURE] =
211+
requestBody.temperature;
195212
}
196213
if (requestBody.p !== undefined) {
197214
spanAttributes[ATTR_GEN_AI_REQUEST_TOP_P] = requestBody.p;
@@ -200,23 +217,30 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
200217
// NOTE: We approximate the token count since this value is not directly available in the body
201218
// According to Bedrock docs they use (total_chars / 6) to approximate token count for pricing.
202219
// https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-prepare.html
203-
spanAttributes[ATTR_GEN_AI_USAGE_INPUT_TOKENS] = Math.ceil(requestBody.prompt.length / 6);
220+
spanAttributes[ATTR_GEN_AI_USAGE_INPUT_TOKENS] = Math.ceil(
221+
requestBody.prompt.length / 6
222+
);
204223
}
205224
if (requestBody.stop_sequences !== undefined) {
206-
spanAttributes[ATTR_GEN_AI_REQUEST_STOP_SEQUENCES] = requestBody.stop_sequences;
225+
spanAttributes[ATTR_GEN_AI_REQUEST_STOP_SEQUENCES] =
226+
requestBody.stop_sequences;
207227
}
208228
} else if (modelId.includes('mistral')) {
209229
if (requestBody.prompt !== undefined) {
210230
// NOTE: We approximate the token count since this value is not directly available in the body
211231
// According to Bedrock docs they use (total_chars / 6) to approximate token count for pricing.
212232
// https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-prepare.html
213-
spanAttributes[ATTR_GEN_AI_USAGE_INPUT_TOKENS] = Math.ceil(requestBody.prompt.length / 6);
233+
spanAttributes[ATTR_GEN_AI_USAGE_INPUT_TOKENS] = Math.ceil(
234+
requestBody.prompt.length / 6
235+
);
214236
}
215237
if (requestBody.max_tokens !== undefined) {
216-
spanAttributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] = requestBody.max_tokens;
238+
spanAttributes[ATTR_GEN_AI_REQUEST_MAX_TOKENS] =
239+
requestBody.max_tokens;
217240
}
218241
if (requestBody.temperature !== undefined) {
219-
spanAttributes[ATTR_GEN_AI_REQUEST_TEMPERATURE] = requestBody.temperature;
242+
spanAttributes[ATTR_GEN_AI_REQUEST_TEMPERATURE] =
243+
requestBody.temperature;
220244
}
221245
if (requestBody.top_p !== undefined) {
222246
spanAttributes[ATTR_GEN_AI_REQUEST_TOP_P] = requestBody.top_p;
@@ -273,7 +297,7 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
273297
span.setAttribute(ATTR_GEN_AI_RESPONSE_FINISH_REASONS, [stopReason]);
274298
}
275299
}
276-
300+
277301
private responseHookInvokeModel(
278302
response: NormalizedResponse,
279303
span: Span,
@@ -286,10 +310,16 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
286310
const responseBody = JSON.parse(decodedResponseBody);
287311
if (currentModelId.includes('amazon.titan')) {
288312
if (responseBody.inputTextTokenCount !== undefined) {
289-
span.setAttribute(ATTR_GEN_AI_USAGE_INPUT_TOKENS, responseBody.inputTextTokenCount);
313+
span.setAttribute(
314+
ATTR_GEN_AI_USAGE_INPUT_TOKENS,
315+
responseBody.inputTextTokenCount
316+
);
290317
}
291318
if (responseBody.results?.[0]?.tokenCount !== undefined) {
292-
span.setAttribute(ATTR_GEN_AI_USAGE_OUTPUT_TOKENS, responseBody.results[0].tokenCount);
319+
span.setAttribute(
320+
ATTR_GEN_AI_USAGE_OUTPUT_TOKENS,
321+
responseBody.results[0].tokenCount
322+
);
293323
}
294324
if (responseBody.results?.[0]?.completionReason !== undefined) {
295325
span.setAttribute(ATTR_GEN_AI_RESPONSE_FINISH_REASONS, [
@@ -299,44 +329,73 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
299329
} else if (currentModelId.includes('amazon.nova')) {
300330
if (responseBody.usage !== undefined) {
301331
if (responseBody.usage.inputTokens !== undefined) {
302-
span.setAttribute(ATTR_GEN_AI_USAGE_INPUT_TOKENS, responseBody.usage.inputTokens);
332+
span.setAttribute(
333+
ATTR_GEN_AI_USAGE_INPUT_TOKENS,
334+
responseBody.usage.inputTokens
335+
);
303336
}
304337
if (responseBody.usage.outputTokens !== undefined) {
305-
span.setAttribute(ATTR_GEN_AI_USAGE_OUTPUT_TOKENS, responseBody.usage.outputTokens);
338+
span.setAttribute(
339+
ATTR_GEN_AI_USAGE_OUTPUT_TOKENS,
340+
responseBody.usage.outputTokens
341+
);
306342
}
307343
}
308344
if (responseBody.stopReason !== undefined) {
309-
span.setAttribute(ATTR_GEN_AI_RESPONSE_FINISH_REASONS, [responseBody.stopReason]);
345+
span.setAttribute(ATTR_GEN_AI_RESPONSE_FINISH_REASONS, [
346+
responseBody.stopReason,
347+
]);
310348
}
311349
} else if (currentModelId.includes('anthropic.claude')) {
312350
if (responseBody.usage?.input_tokens !== undefined) {
313-
span.setAttribute(ATTR_GEN_AI_USAGE_INPUT_TOKENS, responseBody.usage.input_tokens);
351+
span.setAttribute(
352+
ATTR_GEN_AI_USAGE_INPUT_TOKENS,
353+
responseBody.usage.input_tokens
354+
);
314355
}
315356
if (responseBody.usage?.output_tokens !== undefined) {
316-
span.setAttribute(ATTR_GEN_AI_USAGE_OUTPUT_TOKENS, responseBody.usage.output_tokens);
357+
span.setAttribute(
358+
ATTR_GEN_AI_USAGE_OUTPUT_TOKENS,
359+
responseBody.usage.output_tokens
360+
);
317361
}
318362
if (responseBody.stop_reason !== undefined) {
319-
span.setAttribute(ATTR_GEN_AI_RESPONSE_FINISH_REASONS, [responseBody.stop_reason]);
363+
span.setAttribute(ATTR_GEN_AI_RESPONSE_FINISH_REASONS, [
364+
responseBody.stop_reason,
365+
]);
320366
}
321367
} else if (currentModelId.includes('meta.llama')) {
322368
if (responseBody.prompt_token_count !== undefined) {
323-
span.setAttribute(ATTR_GEN_AI_USAGE_INPUT_TOKENS, responseBody.prompt_token_count);
369+
span.setAttribute(
370+
ATTR_GEN_AI_USAGE_INPUT_TOKENS,
371+
responseBody.prompt_token_count
372+
);
324373
}
325374
if (responseBody.generation_token_count !== undefined) {
326-
span.setAttribute(ATTR_GEN_AI_USAGE_OUTPUT_TOKENS, responseBody.generation_token_count);
375+
span.setAttribute(
376+
ATTR_GEN_AI_USAGE_OUTPUT_TOKENS,
377+
responseBody.generation_token_count
378+
);
327379
}
328380
if (responseBody.stop_reason !== undefined) {
329-
span.setAttribute(ATTR_GEN_AI_RESPONSE_FINISH_REASONS, [responseBody.stop_reason]);
381+
span.setAttribute(ATTR_GEN_AI_RESPONSE_FINISH_REASONS, [
382+
responseBody.stop_reason,
383+
]);
330384
}
331385
} else if (currentModelId.includes('cohere.command-r')) {
332386
if (responseBody.text !== undefined) {
333387
// NOTE: We approximate the token count since this value is not directly available in the body
334388
// According to Bedrock docs they use (total_chars / 6) to approximate token count for pricing.
335389
// https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-prepare.html
336-
span.setAttribute(ATTR_GEN_AI_USAGE_OUTPUT_TOKENS, Math.ceil(responseBody.text.length / 6));
390+
span.setAttribute(
391+
ATTR_GEN_AI_USAGE_OUTPUT_TOKENS,
392+
Math.ceil(responseBody.text.length / 6)
393+
);
337394
}
338395
if (responseBody.finish_reason !== undefined) {
339-
span.setAttribute(ATTR_GEN_AI_RESPONSE_FINISH_REASONS, [responseBody.finish_reason]);
396+
span.setAttribute(ATTR_GEN_AI_RESPONSE_FINISH_REASONS, [
397+
responseBody.finish_reason,
398+
]);
340399
}
341400
} else if (currentModelId.includes('cohere.command')) {
342401
if (responseBody.generations?.[0]?.text !== undefined) {

plugins/node/opentelemetry-instrumentation-aws-sdk/test/bedrock-runtime.test.ts

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,11 @@ describe('Bedrock', () => {
180180
);
181181

182182
const testSpans: ReadableSpan[] = getTestSpans();
183-
const invokeModelSpans: ReadableSpan[] = testSpans.filter((s: ReadableSpan) => {
184-
return s.name === 'BedrockRuntime.InvokeModel';
185-
});
183+
const invokeModelSpans: ReadableSpan[] = testSpans.filter(
184+
(s: ReadableSpan) => {
185+
return s.name === 'BedrockRuntime.InvokeModel';
186+
}
187+
);
186188
expect(invokeModelSpans.length).toBe(1);
187189
expect(invokeModelSpans[0].attributes).toMatchObject({
188190
[ATTR_GEN_AI_SYSTEM]: GEN_AI_SYSTEM_VALUE_AWS_BEDROCK,
@@ -219,9 +221,11 @@ describe('Bedrock', () => {
219221
);
220222

221223
const testSpans: ReadableSpan[] = getTestSpans();
222-
const invokeModelSpans: ReadableSpan[] = testSpans.filter((s: ReadableSpan) => {
223-
return s.name === 'BedrockRuntime.InvokeModel';
224-
});
224+
const invokeModelSpans: ReadableSpan[] = testSpans.filter(
225+
(s: ReadableSpan) => {
226+
return s.name === 'BedrockRuntime.InvokeModel';
227+
}
228+
);
225229
expect(invokeModelSpans.length).toBe(1);
226230
expect(invokeModelSpans[0].attributes).toMatchObject({
227231
[ATTR_GEN_AI_SYSTEM]: GEN_AI_SYSTEM_VALUE_AWS_BEDROCK,
@@ -267,9 +271,11 @@ describe('Bedrock', () => {
267271
);
268272

269273
const testSpans: ReadableSpan[] = getTestSpans();
270-
const invokeModelSpans: ReadableSpan[] = testSpans.filter((s: ReadableSpan) => {
271-
return s.name === 'BedrockRuntime.InvokeModel';
272-
});
274+
const invokeModelSpans: ReadableSpan[] = testSpans.filter(
275+
(s: ReadableSpan) => {
276+
return s.name === 'BedrockRuntime.InvokeModel';
277+
}
278+
);
273279
expect(invokeModelSpans.length).toBe(1);
274280
expect(invokeModelSpans[0].attributes).toMatchObject({
275281
[ATTR_GEN_AI_SYSTEM]: GEN_AI_SYSTEM_VALUE_AWS_BEDROCK,
@@ -304,9 +310,11 @@ describe('Bedrock', () => {
304310
);
305311

306312
const testSpans: ReadableSpan[] = getTestSpans();
307-
const invokeModelSpans: ReadableSpan[] = testSpans.filter((s: ReadableSpan) => {
308-
return s.name === 'BedrockRuntime.InvokeModel';
309-
});
313+
const invokeModelSpans: ReadableSpan[] = testSpans.filter(
314+
(s: ReadableSpan) => {
315+
return s.name === 'BedrockRuntime.InvokeModel';
316+
}
317+
);
310318
expect(invokeModelSpans.length).toBe(1);
311319
expect(invokeModelSpans[0].attributes).toMatchObject({
312320
[ATTR_GEN_AI_SYSTEM]: GEN_AI_SYSTEM_VALUE_AWS_BEDROCK,
@@ -341,9 +349,11 @@ describe('Bedrock', () => {
341349
);
342350

343351
const testSpans: ReadableSpan[] = getTestSpans();
344-
const invokeModelSpans: ReadableSpan[] = testSpans.filter((s: ReadableSpan) => {
345-
return s.name === 'BedrockRuntime.InvokeModel';
346-
});
352+
const invokeModelSpans: ReadableSpan[] = testSpans.filter(
353+
(s: ReadableSpan) => {
354+
return s.name === 'BedrockRuntime.InvokeModel';
355+
}
356+
);
347357
expect(invokeModelSpans.length).toBe(1);
348358
expect(invokeModelSpans[0].attributes).toMatchObject({
349359
[ATTR_GEN_AI_SYSTEM]: GEN_AI_SYSTEM_VALUE_AWS_BEDROCK,
@@ -377,9 +387,11 @@ describe('Bedrock', () => {
377387
);
378388

379389
const testSpans: ReadableSpan[] = getTestSpans();
380-
const invokeModelSpans: ReadableSpan[] = testSpans.filter((s: ReadableSpan) => {
381-
return s.name === 'BedrockRuntime.InvokeModel';
382-
});
390+
const invokeModelSpans: ReadableSpan[] = testSpans.filter(
391+
(s: ReadableSpan) => {
392+
return s.name === 'BedrockRuntime.InvokeModel';
393+
}
394+
);
383395
expect(invokeModelSpans.length).toBe(1);
384396
expect(invokeModelSpans[0].attributes).toMatchObject({
385397
[ATTR_GEN_AI_SYSTEM]: GEN_AI_SYSTEM_VALUE_AWS_BEDROCK,
@@ -413,9 +425,11 @@ describe('Bedrock', () => {
413425
);
414426

415427
const testSpans: ReadableSpan[] = getTestSpans();
416-
const invokeModelSpans: ReadableSpan[] = testSpans.filter((s: ReadableSpan) => {
417-
return s.name === 'BedrockRuntime.InvokeModel';
418-
});
428+
const invokeModelSpans: ReadableSpan[] = testSpans.filter(
429+
(s: ReadableSpan) => {
430+
return s.name === 'BedrockRuntime.InvokeModel';
431+
}
432+
);
419433
expect(invokeModelSpans.length).toBe(1);
420434
expect(invokeModelSpans[0].attributes).toMatchObject({
421435
[ATTR_GEN_AI_SYSTEM]: GEN_AI_SYSTEM_VALUE_AWS_BEDROCK,

0 commit comments

Comments
 (0)