Skip to content

feat: Add AWS_SNS_TOPIC_ARN semantic convention support for AWS SNS SDK #2885

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

Merged
merged 4 commits into from
Jul 4, 2025
Merged
Show file tree
Hide file tree
Changes from all 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,13 @@ 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;

/**
* Originally from '@opentelemetry/semantic-conventions/incubating'
* https://github.com/open-telemetry/semantic-conventions/blob/main/docs/registry/attributes/aws.md#amazon-sns-attributes
* 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`.
*/
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 createTopicSpans = getTestSpans().filter(
(s: ReadableSpan) => s.name === 'SNS CreateTopic'
);
expect(createTopicSpans.length).toBe(1);
const span = createTopicSpans[0];
expect(span.attributes[ATTR_AWS_SNS_TOPIC_ARN]).toBe(topicArn);
expect(span.kind).toBe(SpanKind.CLIENT);
});
});
});