Skip to content

Commit

Permalink
fix(sns-publish-test-v3): add test for sns.publish for aws sdk v3 (op…
Browse files Browse the repository at this point in the history
…en-telemetry#1015)

* fix(sns-publish-test-v3): add test for sns.publish for aws sdk v3

* fix(add-header-to-payloads): fix lint errors

* fix(sns-publish-test-v3): fix lint problems

* fix(sns-publish-test-v3): fix lint problems

* fix(sns-publish-test-v3): fix sns version
  • Loading branch information
haddasbronfman committed May 10, 2022
1 parent 61ead95 commit 0293d89
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 12 deletions.
11 changes: 6 additions & 5 deletions plugins/node/opentelemetry-instrumentation-aws-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@
"@opentelemetry/propagation-utils": "^0.27.0"
},
"devDependencies": {
"@aws-sdk/client-dynamodb": "3.37.0",
"@aws-sdk/client-lambda": "3.37.0",
"@aws-sdk/client-s3": "3.37.0",
"@aws-sdk/client-sqs": "3.37.0",
"@aws-sdk/types": "3.37.0",
"@aws-sdk/client-dynamodb": "3.85.0",
"@aws-sdk/client-lambda": "3.85.0",
"@aws-sdk/client-s3": "3.85.0",
"@aws-sdk/client-sqs": "3.85.0",
"@aws-sdk/client-sns": "3.85.0",
"@aws-sdk/types": "3.78.0",
"@opentelemetry/api": "1.0.1",
"@opentelemetry/contrib-test-utils": "0.29.0",
"@opentelemetry/sdk-trace-base": "1.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0"?><PublishResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/"><PublishResult><MessageId>90d1987b-4853-54ad-a499-c2d89c4edf3a</MessageId></PublishResult><ResponseMetadata><RequestId>d81e4f4f-2d70-51f3-a040-15ecf96d5a64</RequestId></ResponseMetadata></PublishResponse>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import {
const instrumentation = registerInstrumentationTesting(
new AwsInstrumentation()
);
import * as AWS from 'aws-sdk';
import * as AWSv2 from 'aws-sdk';
import { SNS as SNSv3 } from '@aws-sdk/client-sns';
import * as fs from 'fs';
import * as nock from 'nock';

import { mockV2AwsSend } from './testing-utils';
import * as expect from 'expect';
Expand All @@ -41,9 +44,9 @@ const responseMockSuccess = {
const topicName = 'topic';
const fakeARN = `arn:aws:sns:region:000000000:${topicName}`;

describe('SNS', () => {
describe('SNS - v2', () => {
before(() => {
AWS.config.credentials = {
AWSv2.config.credentials = {
accessKeyId: 'test key id',
expired: false,
expireTime: new Date(),
Expand All @@ -60,7 +63,7 @@ describe('SNS', () => {

describe('publish', () => {
it('topic arn', async () => {
const sns = new AWS.SNS();
const sns = new AWSv2.SNS();

await sns
.publish({
Expand Down Expand Up @@ -91,7 +94,7 @@ describe('SNS', () => {
});

it('phone number', async () => {
const sns = new AWS.SNS();
const sns = new AWSv2.SNS();
const PhoneNumber = 'my phone number';
await sns
.publish({
Expand All @@ -111,7 +114,7 @@ describe('SNS', () => {
});

it('inject context propagation', async () => {
const sns = new AWS.SNS();
const sns = new AWSv2.SNS();
const hookSpy = sinon.spy(
(instrumentation['servicesExtensions'] as any)['services'].get('SNS'),
'requestPostSpanHook'
Expand All @@ -136,7 +139,7 @@ describe('SNS', () => {

describe('createTopic', () => {
it('basic createTopic creates a valid span', async () => {
const sns = new AWS.SNS();
const sns = new AWSv2.SNS();

const Name = 'my new topic';
await sns.createTopic({ Name }).promise();
Expand All @@ -160,3 +163,70 @@ describe('SNS', () => {
});
});
});

describe('SNS - v3', () => {
let sns: any;
beforeEach(() => {
sns = new SNSv3({
region: 'us-east-1',
credentials: {
accessKeyId: 'abcde',
secretAccessKey: 'abcde',
},
});

nock('https://sns.us-east-1.amazonaws.com/')
.post('/')
.reply(
200,
fs.readFileSync('./test/mock-responses/sns-publish.xml', 'utf8')
);
});

describe('publish', () => {
it('topic arn', async () => {
const topicV3Name = 'dummy-sns-v3-topic';
await sns.publish({
Message: 'sns message',
TopicArn: `arn:aws:sns:us-east-1:000000000:${topicV3Name}`,
});

const publishSpans = getTestSpans().filter(
(s: ReadableSpan) => s.name === `${topicV3Name} send`
);
expect(publishSpans.length).toBe(1);

const publishSpan = publishSpans[0];
expect(
publishSpan.attributes[SemanticAttributes.MESSAGING_DESTINATION_KIND]
).toBe(MessagingDestinationKindValues.TOPIC);
expect(
publishSpan.attributes[SemanticAttributes.MESSAGING_DESTINATION]
).toBe(topicV3Name);
expect(publishSpan.attributes[SemanticAttributes.RPC_METHOD]).toBe(
'Publish'
);
expect(publishSpan.attributes[SemanticAttributes.MESSAGING_SYSTEM]).toBe(
'aws.sns'
);
expect(publishSpan.kind).toBe(SpanKind.PRODUCER);
});

it('phone number', async () => {
const PhoneNumber = 'my phone number';
await sns.publish({
Message: 'sns message',
PhoneNumber,
});

const publishSpans = getTestSpans().filter(
(s: ReadableSpan) => s.name === 'phone_number send'
);
expect(publishSpans.length).toBe(1);
const publishSpan = publishSpans[0];
expect(
publishSpan.attributes[SemanticAttributes.MESSAGING_DESTINATION]
).toBe(PhoneNumber);
});
});
});

0 comments on commit 0293d89

Please sign in to comment.