Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
refactor: Updated aws sdk instrumentation to construct specs at instr…
Browse files Browse the repository at this point in the history
…umentation (#259)
  • Loading branch information
bizob2828 authored Feb 28, 2024
1 parent 0ab4890 commit 94de420
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 30 deletions.
7 changes: 4 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ function grabLastUrlSegment(url = '/') {
/**
* Retrieves the db segment params from endpoint and command parameters
*
* @param {function} DatastoreParameters constructor of `shim.spec.DatastoreParameters`
* @param {Object} endpoint instance of ddb endpoint
* @param {Object} params parameters passed to a ddb command
* @returns {Object}
*/
function setDynamoParameters(endpoint, params) {
return {
function setDynamoParameters(DatastoreParameters, endpoint, params) {
return new DatastoreParameters({
host: endpoint && (endpoint.host || endpoint.hostname),
port_path_or_id: (endpoint && endpoint.port) || 443,
collection: (params && params.TableName) || UNKNOWN
}
})
}

module.exports = {
Expand Down
24 changes: 14 additions & 10 deletions lib/v2/dynamodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ function instrument(shim, AWS) {
function wrapMethod(shim, original, operationName, args) {
const params = args[0]

return {
return new shim.specs.OperationSpec({
name: operationName,
parameters: setDynamoParameters(this.endpoint, params),
parameters: setDynamoParameters(
shim.specs.params.DatastoreParameters,
this.endpoint,
params
),
callback: shim.LAST,
opaque: true
}
})
}
)
})
Expand All @@ -70,16 +74,16 @@ function instrument(shim, AWS) {
// the eventual cached endpoint to be hit is not known at this point.
const endpoint = this.service && this.service.endpoint

return {
return new shim.specs.OperationSpec({
name: dynamoOperation,
parameters: {
host: endpoint && endpoint.host,
port_path_or_id: endpoint && endpoint.port,
collection: (params && params.TableName) || 'Unknown'
},
parameters: new shim.specs.params.DatastoreParameters({
host: endpoint?.host,
port_path_or_id: endpoint?.port,
collection: params?.TableName || 'Unknown'
}),
callback: shim.LAST,
opaque: true
}
})
}
)
}
Expand Down
4 changes: 2 additions & 2 deletions lib/v2/sns.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ function instrument(shim, AWS) {
}

function wrapPublish(shim, original, name, args) {
return {
return new shim.specs.MessageSpec({
callback: shim.LAST,
destinationName: getDestinationName(args[0]),
destinationType: shim.TOPIC,
opaque: true
}
})
}

function getDestinationName({ TopicArn, TargetArn }) {
Expand Down
4 changes: 2 additions & 2 deletions lib/v2/sqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ function recordMessageApi(shim, original, name, args) {
const params = args[0]
const queueName = grabLastUrlSegment(params.QueueUrl)

return {
return new shim.specs.MessageSpec({
callback: shim.LAST,
destinationName: queueName,
destinationType: shim.QUEUE,
opaque: true
}
})
}
4 changes: 2 additions & 2 deletions lib/v3/bedrock.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function getBedrockSpec({ commandName }, shim, _original, _name, args) {
const bedrockCommand = new BedrockCommand(input)
const { modelType } = bedrockCommand

return {
return new shim.specs.RecorderSpec({
promise: true,
name: `Llm/${modelType}/Bedrock/${commandName}`,
// eslint-disable-next-line max-params
Expand Down Expand Up @@ -222,7 +222,7 @@ function getBedrockSpec({ commandName }, shim, _original, _name, args) {
addLlmMeta({ agent, segment })
}
}
}
})
}

function handleResponse({ shim, err, response, segment, bedrockCommand, modelType }) {
Expand Down
6 changes: 3 additions & 3 deletions lib/v3/dynamodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ const { setDynamoParameters } = require('../util')
*/
function getDynamoSpec(shim, original, name, args) {
const [{ input }] = args
return {
return new shim.specs.OperationSpec({
name: this.commandName,
parameters: setDynamoParameters(this.endpoint, input),
parameters: setDynamoParameters(shim.specs.params.DatastoreParameters, this.endpoint, input),
callback: shim.LAST,
opaque: true,
promise: true
}
})
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/v3/sns.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ function snsMiddleware(shim, config, next, context) {
*/
function getSnsSpec(shim, original, name, args) {
const [command] = args
return {
return new shim.specs.MessageSpec({
promise: true,
callback: shim.LAST,
destinationName: getDestinationName(command.input),
destinationType: shim.TOPIC,
opaque: true
}
})
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/v3/sqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ function sqsMiddleware(shim, config, next, context) {
function getSqsSpec(shim, original, name, args) {
const [command] = args
const { QueueUrl } = command.input
return {
return new shim.specs.MessageSpec({
callback: shim.LAST,
destinationName: grabLastUrlSegment(QueueUrl),
destinationType: shim.QUEUE,
opaque: true
}
})
}

module.exports.sqsMiddlewareConfig = {
Expand Down
16 changes: 13 additions & 3 deletions tests/unit/util.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
'use strict'
const tap = require('tap')
const { grabLastUrlSegment, setDynamoParameters } = require('../../lib/util')

function DatastoreParametersMock(params) {
this.host = params.host ?? null
this.port_path_or_id = params.port_path_or_id ?? null
this.database_name = params.database_name ?? null
this.collection = params.collection ?? null
}
tap.test('Utility Functions', (t) => {
t.ok(grabLastUrlSegment, 'imported function successfully')

Expand Down Expand Up @@ -41,11 +48,12 @@ tap.test('DB parameters', (t) => {
t.test('default values', (t) => {
const input = {}
const endpoint = {}
const result = setDynamoParameters(endpoint, input)
const result = setDynamoParameters(DatastoreParametersMock, endpoint, input)
t.same(
result,
{
host: undefined,
database_name: null,
port_path_or_id: 443,
collection: 'Unknown'
},
Expand All @@ -58,11 +66,12 @@ tap.test('DB parameters', (t) => {
t.test('host, port, collection', (t) => {
const input = { TableName: 'unit-test' }
const endpoint = { host: 'unit-test-host', port: '123' }
const result = setDynamoParameters(endpoint, input)
const result = setDynamoParameters(DatastoreParametersMock, endpoint, input)
t.same(
result,
{
host: endpoint.host,
database_name: null,
port_path_or_id: endpoint.port,
collection: input.TableName
},
Expand All @@ -75,11 +84,12 @@ tap.test('DB parameters', (t) => {
t.test('hostname, port, collection', (t) => {
const input = { TableName: 'unit-test' }
const endpoint = { hostname: 'unit-test-host', port: '123' }
const result = setDynamoParameters(endpoint, input)
const result = setDynamoParameters(DatastoreParametersMock, endpoint, input)
t.same(
result,
{
host: endpoint.hostname,
database_name: null,
port_path_or_id: endpoint.port,
collection: input.TableName
},
Expand Down
2 changes: 1 addition & 1 deletion third_party_manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lastUpdated": "Tue Feb 27 2024 15:25:14 GMT-0500 (Eastern Standard Time)",
"lastUpdated": "Tue Feb 27 2024 15:27:30 GMT-0500 (Eastern Standard Time)",
"projectName": "New Relic AWS-SDK Instrumentation",
"projectUrl": "https://github.com/newrelic/node-newrelic-aws-sdk",
"includeOptDeps": false,
Expand Down

0 comments on commit 94de420

Please sign in to comment.