Skip to content

Commit

Permalink
chore: Added entity relationship attributes to SQS segments
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Aug 2, 2024
1 parent b32f793 commit 657dc92
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
18 changes: 17 additions & 1 deletion lib/instrumentation/aws-sdk/v3/sqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,26 @@ function getSqsSpec(shim, original, name, args) {
callback: shim.LAST,
destinationName: grabLastUrlSegment(QueueUrl),
destinationType: shim.QUEUE,
opaque: true
opaque: true,
after({ segment }) {
const { region, accountId, queue } = urlComponents(QueueUrl)
segment.addAttribute('messaging.system', 'aws_sqs')
segment.addAttribute('cloud.region', region)
segment.addAttribute('cloud.account.id', accountId)
segment.addAttribute('messaging.destination.name', queue)
}
})
}

const urlReg = /\/\/sqs\.(?<region>[\w-]+)\.amazonaws\.com(:\d+)?\/(?<accountId>\d+)\/(?<queue>.+)$/
function urlComponents(queueUrl) {
const matches = urlReg.exec(queueUrl)
if (matches?.groups) {
return matches.groups
}
return { region: undefined, accountId: undefined, queue: undefined }
}

module.exports.sqsMiddlewareConfig = {
middleware: sqsMiddleware,
init(shim) {
Expand Down
23 changes: 22 additions & 1 deletion test/lib/aws-server-stubs/response-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
'use strict'

const http = require('http')
const dns = require('node:dns')
const semver = require('semver')
const { getAddTagsResponse } = require('./elasticache')
const { getAcceptExchangeResponse } = require('./redshift')
const { getSendEmailResponse } = require('./ses')
Expand All @@ -30,6 +32,25 @@ function createResponseServer() {
res.end()
})

const lookup = dns.lookup
dns.lookup = (...args) => {
const address = args[0]
if (address === 'sqs.us-east-1.amazonaws.com') {
if (semver.satisfies(process.version, '18')) {
return args.pop()(null, '127.0.0.1', 4)
}
// Node >= 20 changes the callback signature.
return args.pop()(null, [{ address: '127.0.0.1', family: 4 }])
}
lookup.apply(dns, args)
}

const close = server.close
server.close = () => {
close.call(server)
dns.lookup = lookup
}

patchDestroy(server)

return server
Expand All @@ -44,7 +65,7 @@ function handlePost(req, res) {

req.on('end', () => {
const isJson = !!req.headers['x-amz-target']
const endpoint = `http://localhost:${req.connection.localPort}`
const endpoint = `http://${req.headers.host}`
const parsed = parseBody(body, req.headers)

const getDataFunction = createGetDataFromAction(endpoint, parsed, isJson)
Expand Down
4 changes: 3 additions & 1 deletion test/lib/aws-server-stubs/response-server/sqs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ helpers.getCreateQueueResponse = function getCreateQueueResponse(
}

helpers.formatUrl = function formatUrl(endpoint, queueName) {
return `${endpoint}/queue/${queueName}`
// See https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-queue-message-identifiers.html#sqs-general-identifiers
// for details on the format of an SQS queue URL.
return `${endpoint}/1234567890/${queueName}`
}

helpers.getSendMessageResponse = function getSendMessageResponse(isJson, callback) {
Expand Down
11 changes: 10 additions & 1 deletion test/versioned/aws-sdk-v3/sqs.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tap.test('SQS API', (t) => {

t.context.sqs = new SQSClient({
credentials: FAKE_CREDENTIALS,
endpoint: `http://localhost:${server.address().port}`,
endpoint: `http://sqs.${AWS_REGION}.amazonaws.com:${server.address().port}`,
region: AWS_REGION
})

Expand Down Expand Up @@ -112,6 +112,15 @@ function finish({ t, transaction, queueName }) {
checkName(t, receiveMessage.name, 'Consume', queueName)
checkAttributes(t, receiveMessage, 'ReceiveMessageCommand')
t.equal(t.context.setLibrarySpy.callCount, 1, 'should only call setLibrary once and not per call')

// Verify that cloud entity relationship attributes are present:
for (const segment of segments) {
const attrs = segment.getAttributes()
t.equal(attrs['messaging.system'], 'aws_sqs')
t.equal(attrs['cloud.region'], 'us-east-1')
t.equal(attrs['cloud.account.id'], '1234567890')
t.equal(attrs['messaging.destination.name'], queueName)
}
}

function checkName(t, name, action, queueName) {
Expand Down

0 comments on commit 657dc92

Please sign in to comment.