From 330cc4b6ff8e3777b1dfa0fda7f5b4c5b20005af Mon Sep 17 00:00:00 2001 From: James Sumners Date: Thu, 23 May 2024 14:31:18 -0400 Subject: [PATCH] chore: Fixed aws-sdk-v3 bedrock tests (again) (#2212) --- .../bedrock-chat-completions.tap.js | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/test/versioned/aws-sdk-v3/bedrock-chat-completions.tap.js b/test/versioned/aws-sdk-v3/bedrock-chat-completions.tap.js index f2fb5a4427..fca8dd5b25 100644 --- a/test/versioned/aws-sdk-v3/bedrock-chat-completions.tap.js +++ b/test/versioned/aws-sdk-v3/bedrock-chat-completions.tap.js @@ -13,21 +13,6 @@ const createAiResponseServer = require('../../lib/aws-server-stubs/ai-server') const { FAKE_CREDENTIALS } = require('../../lib/aws-server-stubs') const { DESTINATIONS } = require('../../../lib/config/attribute-filter') -const pkgVersion = (function () { - try { - const { version } = require('@smithy/smithy-client/package.json') - return version - } catch { - try { - const { - version - } = require('./node_modules/@aws-sdk/client-bedrock-runtime/node_modules/@smithy/smithy-client/package.json') - return version - } catch {} - } - /* eslint-disable-next-line */ -}()) - const requests = { ai21: (prompt, modelId) => ({ body: JSON.stringify({ prompt, temperature: 0.5, maxTokens: 100 }), @@ -241,9 +226,10 @@ tap.afterEach(async (t) => { const command = new bedrock.InvokeModelCommand(input) helper.runInTransaction(agent, async (tx) => { await client.send(command) - const metrics = agent.metrics.getOrCreateMetric( - `Supportability/Nodejs/ML/Bedrock/${pkgVersion}` - ) + const metrics = getPrefixedMetric({ + agent, + metricPrefix: 'Supportability/Nodejs/ML/Bedrock' + }) t.equal(metrics.callCount > 0, true) tx.end() t.end() @@ -588,9 +574,10 @@ tap.test('should not instrument stream when disabled', (t) => { t.equal(events.length, 0, 'should not create Llm events when streaming is disabled') const attributes = tx.trace.attributes.get(DESTINATIONS.TRANS_EVENT) t.equal(attributes.llm, true, 'should assign llm attribute to transaction trace') - const metrics = agent.metrics.getOrCreateMetric( - `Supportability/Nodejs/ML/Bedrock/${pkgVersion}` - ) + const metrics = getPrefixedMetric({ + agent, + metricPrefix: 'Supportability/Nodejs/ML/Bedrock' + }) t.equal(metrics.callCount > 0, true, 'should set framework metric') const supportabilityMetrics = agent.metrics.getOrCreateMetric( `Supportability/Nodejs/ML/Streaming/Disabled` @@ -632,3 +619,12 @@ tap.test('should utilize tokenCountCallback when set', (t) => { t.end() }) }) + +function getPrefixedMetric({ agent, metricPrefix }) { + for (const [key, value] of Object.entries(agent.metrics._metrics.unscoped)) { + if (key.startsWith(metricPrefix) === false) { + continue + } + return value + } +}