Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@opentelemetry/core": "^2.0.0",
"@opentelemetry/instrumentation": "^0.202.0",
"@opentelemetry/propagation-utils": "^0.31.2",
"@opentelemetry/semantic-conventions": "^1.31.0"
"@opentelemetry/semantic-conventions": "^1.34.0"
},
"devDependencies": {
"@aws-sdk/client-bedrock-runtime": "^3.587.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,6 @@ export const GEN_AI_TOKEN_TYPE_VALUE_INPUT = 'input' as const;
* Enum value "output" for attribute {@link ATTR_GEN_AI_TOKEN_TYPE}.
*/
export const GEN_AI_TOKEN_TYPE_VALUE_OUTPUT = 'output' as const;

// Copied ATTR_AWS_SNS_TOPIC_ARN from '@opentelemetry/semantic-conventions/incubating'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: copying also the docs would give more info to other contributors if they happen to work with this instrumentation. It also will reflect the experiemental status of the attribute in the IDE

Suggested change
// Copied ATTR_AWS_SNS_TOPIC_ARN from '@opentelemetry/semantic-conventions/incubating'
/**
* Originally from '@opentelemetry/semantic-conventions/incubating'
* The ARN of the AWS SNS Topic. An Amazon SNS [topic](https://docs.aws.amazon.com/sns/latest/dg/sns-create-topic.html) is a logical access point that acts as a communication channel.
*
* @example arn:aws:sns:us-east-1:123456789012:mystack-mytopic-NZJ5JSMVGFIE
*
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
*/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, copying the docs is usually preferred.

export const ATTR_AWS_SNS_TOPIC_ARN = 'aws.sns.topic.arn' as const;
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
SEMATTRS_MESSAGING_DESTINATION_KIND,
SEMATTRS_MESSAGING_SYSTEM,
} from '@opentelemetry/semantic-conventions';
import { ATTR_AWS_SNS_TOPIC_ARN } from '../semconv';
import {
NormalizedRequest,
NormalizedResponse,
Expand Down Expand Up @@ -58,6 +59,11 @@ export class SnsServiceExtension implements ServiceExtension {
} send`;
}

const topicArn = request.commandInput?.TopicArn;
if (topicArn) {
spanAttributes[ATTR_AWS_SNS_TOPIC_ARN] = topicArn;
}

return {
isIncoming: false,
spanAttributes,
Expand All @@ -83,7 +89,12 @@ export class SnsServiceExtension implements ServiceExtension {
span: Span,
tracer: Tracer,
config: AwsSdkInstrumentationConfig
): void {}
): void {
const topicArn = response.data?.TopicArn;
if (topicArn) {
span.setAttribute(ATTR_AWS_SNS_TOPIC_ARN, topicArn);
}
}

extractDestinationName(
topicArn: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<CreateTopicResponse xmlns="https://sns.amazonaws.com/doc/2010-03-31/">
<CreateTopicResult>
<TopicArn>arn:aws:sns:us-east-1:123456789012:sns-topic-foo</TopicArn>
</CreateTopicResult>
<ResponseMetadata>
<RequestId>d74b8436-ae13-5ab4-a9ff-ce54dfea72a0</RequestId>
</ResponseMetadata>
</CreateTopicResponse>
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,30 @@ import {
SEMATTRS_MESSAGING_SYSTEM,
SEMATTRS_RPC_METHOD,
} from '@opentelemetry/semantic-conventions';
import { ATTR_AWS_SNS_TOPIC_ARN } from '@opentelemetry/semantic-conventions/incubating';
import { SpanKind } from '@opentelemetry/api';

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', () => {
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')
);
});

it('topic arn', async () => {
const topicV3Name = 'dummy-sns-v3-topic';
const topicV3ARN = `arn:aws:sns:us-east-1:000000000:${topicV3Name}`;
Expand All @@ -73,6 +75,7 @@ describe('SNS - v3', () => {
expect(publishSpan.attributes['messaging.destination.name']).toBe(
topicV3ARN
);
expect(publishSpan.attributes[ATTR_AWS_SNS_TOPIC_ARN]).toBe(topicV3ARN);
expect(publishSpan.attributes[SEMATTRS_RPC_METHOD]).toBe('Publish');
expect(publishSpan.attributes[SEMATTRS_MESSAGING_SYSTEM]).toBe('aws.sns');
expect(publishSpan.kind).toBe(SpanKind.PRODUCER);
Expand All @@ -93,6 +96,41 @@ describe('SNS - v3', () => {
expect(publishSpan.attributes[SEMATTRS_MESSAGING_DESTINATION]).toBe(
PhoneNumber
);
expect(publishSpan.attributes[ATTR_AWS_SNS_TOPIC_ARN]).toBeUndefined();
});
});

describe('Create Topic', () => {
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-create-topic.xml', 'utf8')
);
});

it('should create topic ARN and capture expected trace attributes', async () => {
const topicName = 'sns-topic-foo';
const topicArn = `arn:aws:sns:us-east-1:123456789012:${topicName}`;
await sns.createTopic({
Name: topicName,
});
const publishSpans = getTestSpans().filter(
(s: ReadableSpan) => s.name === 'SNS CreateTopic'
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - update name to createTopicSpans

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will fail the UT. I believe the span name comes from the auto-instrumentation library. The predicate only reads it, not set it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I meant the variable name publishSpans -> createTopicSpans, I assume the original variable name was from the other test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

expect(publishSpans.length).toBe(1);
const publishSpan = publishSpans[0];
expect(publishSpan.attributes[ATTR_AWS_SNS_TOPIC_ARN]).toBe(topicArn);
expect(publishSpan.kind).toBe(SpanKind.CLIENT);
});
});
});