Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sns instrumentation #2157

Merged
merged 23 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions lib/instrumentation/modules/aws-sdk/sns.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@ const constants = require('../../../constants')

const TYPE = 'messaging'
const SUBTYPE = 'sns'
const ACTION = 'send'
const ACCESS_POINT = 'accesspoint'
const ACTION = 'publish'
const PHONE_NUMBER = '<PHONE_NUMBER>'

function getArnFromRequest (request) {
astorm marked this conversation as resolved.
Show resolved Hide resolved
let arn = request && request.params && request.params.TopicArn
if (!arn) {
arn = request && request.params && request.params.TargetArn
}
return arn
}

function getRegionFromRequest (request) {
return request && request.service &&
request.service.config && request.service.config.region
}

function getSpanNameFromRequest (request) {
const topicName = getDestinationNameFromRequest(request)
return `SNS PUBLISH ${topicName}`
return `SNS PUBLISH to ${topicName}`
}

function getMessageContextFromRequest (request) {
Expand Down Expand Up @@ -41,40 +48,39 @@ function getMessageDestinationContextFromRequest (request) {
service: {
resource: `${SUBTYPE}/${getDestinationNameFromRequest(request)}`,
type: TYPE,
name: SUBTYPE,
name: SUBTYPE
},
cloud: {
region: getRegionFromRequest(request)
}
}
}

function getArnFromRequest (request) {
let arn = request && request.params && request.params.TopicArn
if (!arn) {
arn = request && request.params && request.params.TargetArn
}
return arn
}

function getDestinationNameFromRequest (request) {
const phoneNumber = request && request.params && request.params.PhoneNumber
if (phoneNumber) {
return PHONE_NUMBER
}

const arn = getArnFromRequest(request)
const topicArn = request && request.params && request.params.TopicArn
const targetArn = request && request.params && request.params.TargetArn

if (!arn) {
return
if (topicArn) {
const parts = topicArn.split(':')
const topicName = parts.pop()
return topicName
}
const parts = arn.split(':')
const topicOrAccessPointName = parts.pop()
const maybeAccessPoint = parts.pop()
if (ACCESS_POINT === maybeAccessPoint) {
return `${ACCESS_POINT}:${topicOrAccessPointName}`

if (targetArn) {
const fullName = targetArn.split(':').pop()
if (fullName.indexOf('/') !== -1) {
astorm marked this conversation as resolved.
Show resolved Hide resolved
const parts = fullName.split('/')
parts.pop()
return parts.join('/')
} else {
return fullName
}
}
return topicOrAccessPointName
}

function shouldIgnoreRequest (request, agent) {
Expand Down
63 changes: 21 additions & 42 deletions test/instrumentation/modules/aws-sdk/sns.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,49 +64,27 @@ tape.test('AWS SNS: Unit Test Functions', function (test) {
operation: 'publish',
params: {
Message: 'this is my test, there are many lot like it but this one is mine',
TargetArn: 'arn:aws:sns:us-west-2:111111111111:topic-name'
TargetArn: 'arn:aws:sns:us-west-2:123456789012:endpoint/GCM/gcmpushapp/5e3e9847-3183-3f18-a7e8-671c3a57d4b3'
}
}), 'topic-name')

t.equals(getDestinationNameFromRequest({
operation: 'publish',
params: {
Message: 'this is my test, there are many lot like it but this one is mine',
TopicArn: 'arn:aws:sns:us-west-2:111111111111:accesspoint/withslashes'
}
}), 'accesspoint/withslashes')

t.equals(getDestinationNameFromRequest({
operation: 'publish',
params: {
Message: 'this is my test, there are many lot like it but this one is mine',
TargetArn: 'arn:aws:sns:us-west-2:111111111111:accesspoint/withslashes'
}
}), 'accesspoint/withslashes')

t.equals(getDestinationNameFromRequest({
operation: 'publish',
params: {
Message: 'this is my test, there are many lot like it but this one is mine',
TopicArn: 'arn:aws:sns:us-west-2:111111111111:accesspoint:withcolons'
}
}), 'accesspoint:withcolons')
}), 'endpoint/GCM/gcmpushapp')

// unlikely we'll receive a targetArn without /, but we should
// do something reasonable, just in case
t.equals(getDestinationNameFromRequest({
operation: 'publish',
params: {
Message: 'this is my test, there are many lot like it but this one is mine',
TargetArn: 'arn:aws:sns:us-west-2:111111111111:accesspoint:withcolons'
TargetArn: 'arn:aws:sns:us-west-2:123456789012:endpoint:GCM'
}
}), 'accesspoint:withcolons')
}), 'GCM')

t.equals(getDestinationNameFromRequest({
operation: 'publish',
params: {
Message: 'this is my test, there are many lot like it but this one is mine',
TargetArn: 'arn:aws:sns:us-west-2:111111111111:accesspoint:withcolons'
TopicArn: 'arn:aws:sns:us-west-2:111111111111:foo/withslashes'
}
}), 'accesspoint:withcolons')
}), 'foo/withslashes')

t.equals(getDestinationNameFromRequest({
operation: 'publish',
Expand All @@ -123,35 +101,35 @@ tape.test('AWS SNS: Unit Test Functions', function (test) {
t.end()
})

test.test('getDestinationNameFromRequest tests', function (t) {
test.test('getSpanNameFromRequest tests', function (t) {
t.equals(getSpanNameFromRequest({
operation: 'publish',
params: {
Message: 'work test',
Subject: 'Admin',
PhoneNumber: '15555555555'
}
}), 'SNS PUBLISH <PHONE_NUMBER>')
}), 'SNS PUBLISH to <PHONE_NUMBER>')

t.equals(getSpanNameFromRequest({
operation: 'publish',
params: {
Message: 'this is my test, there are many lot like it but this one is mine',
TargetArn: 'arn:aws:sns:us-west-2:111111111111:accesspoint:withcolons'
TargetArn: 'arn:aws:sns:us-west-2:123456789012:endpoint/GCM/gcmpushapp/5e3e9847-3183-3f18-a7e8-671c3a57d4b3'
}
}), 'SNS PUBLISH accesspoint:withcolons')
}), 'SNS PUBLISH to endpoint/GCM/gcmpushapp')

t.equals(getSpanNameFromRequest({
operation: 'publish',
params: {
Message: 'this is my test, there are many lot like it but this one is mine',
TopicArn: 'arn:aws:sns:us-west-2:111111111111:foo:topic-name'
}
}), 'SNS PUBLISH topic-name')
}), 'SNS PUBLISH to topic-name')

t.equals(getSpanNameFromRequest(null), 'SNS PUBLISH undefined')
t.equals(getSpanNameFromRequest({}), 'SNS PUBLISH undefined')
t.equals(getSpanNameFromRequest({ params: {} }), 'SNS PUBLISH undefined')
t.equals(getSpanNameFromRequest(null), 'SNS PUBLISH to undefined')
t.equals(getSpanNameFromRequest({}), 'SNS PUBLISH to undefined')
t.equals(getSpanNameFromRequest({ params: {} }), 'SNS PUBLISH to undefined')
t.end()
})

Expand Down Expand Up @@ -179,7 +157,7 @@ tape.test('AWS SNS: Unit Test Functions', function (test) {
service: {
resource: 'sns/topic-name',
type: 'messaging',
name: 'sns',
name: 'sns'
},
cloud: { region: 'us-west-2' }
}
Expand All @@ -193,7 +171,7 @@ tape.test('AWS SNS: Unit Test Functions', function (test) {
service: {
resource: 'sns/undefined',
type: 'messaging',
name: 'sns',
name: 'sns'
},
cloud: { region: null }
}
Expand All @@ -207,7 +185,7 @@ tape.test('AWS SNS: Unit Test Functions', function (test) {
service: {
resource: 'sns/undefined',
type: 'messaging',
name: 'sns',
name: 'sns'
},
cloud: { region: undefined }
}
Expand All @@ -232,9 +210,10 @@ tape.test('AWS SNS: End to End Test', function (test) {
const port = listener.address().port
resetAgent(function (data) {
const span = data.spans.filter((span) => span.type === 'messaging').pop()
t.equals(span.name, 'SNS PUBLISH topic-name', 'span named correctly')
t.equals(span.name, 'SNS PUBLISH to topic-name', 'span named correctly')
t.equals(span.type, 'messaging', 'span type correctly set')
t.equals(span.subtype, 'sns', 'span subtype set correctly')
t.equals(span.action, 'publish', 'span action set correctly')
t.equals(span.context.message.queue.name, 'topic-name')
t.equals(span.context.destination.service.resource, 'sns/topic-name')
t.equals(span.context.destination.service.type, 'messaging')
Expand Down