Skip to content

Commit bd9e782

Browse files
seemktrentm
andauthored
feat(aws-sdk)!: SQS receive: use span links instead of processing spans (#2345)
Co-authored-by: Trent Mick <[email protected]>
1 parent 8b09de9 commit bd9e782

File tree

9 files changed

+152
-168
lines changed

9 files changed

+152
-168
lines changed

package-lock.json

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/instrumentation-aws-sdk/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ aws-sdk instrumentation has few options available to choose from. You can set th
5353
| `preRequestHook` | `AwsSdkRequestCustomAttributeFunction` | Hook called before request send, which allow to add custom attributes to span. |
5454
| `responseHook` | `AwsSdkResponseCustomAttributeFunction` | Hook for adding custom attributes when response is received from aws. |
5555
| `exceptionHook` | `AwsSdkExceptionCustomAttributeFunction` | Hook for adding custom attributes when exception is received from aws. |
56-
| `sqsProcessHook` | `AwsSdkSqsProcessCustomAttributeFunction` | Hook called after starting sqs `process` span (for each sqs received message), which allow to add custom attributes to it. |
5756
| `suppressInternalInstrumentation` | `boolean` | Most aws operation use http requests under the hood. Set this to `true` to hide all underlying http spans. |
5857
| `sqsExtractContextPropagationFromPayload` | `boolean` | Will parse and extract context propagation headers from SQS Payload, false by default. [When should it be used?](./doc/sns.md#integration-with-sqs) |
5958
| `dynamoDBStatementSerializer` | `AwsSdkDynamoDBStatementSerializer` | AWS SDK instrumentation will serialize DynamoDB commands to the `db.statement` attribute using the specified function. Defaults to using a serializer that returns `undefined`. |
Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,19 @@
11
# SQS
22

3-
SQS is amazon's managed message queue. Thus, it should follow the [OpenTelemetry specification for Messaging systems](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/messaging.md).
3+
SQS is Amazon's managed message queue. Thus, it should follow the [OpenTelemetry specification for Messaging systems](https://opentelemetry.io/docs/specs/semconv/messaging/messaging-spans/).
44

55
## Specific trace semantic
66

77
The following methods are automatically enhanced:
88

99
### sendMessage / sendMessageBatch
1010

11-
- [Messaging Attributes](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/messaging.md#messaging-attributes) are added by this instrumentation according to the spec.
11+
- [Messaging Attributes](https://opentelemetry.io/docs/specs/semconv/messaging/messaging-spans/#messaging-attributes) are added by this instrumentation according to the spec.
1212
- OpenTelemetry trace context is injected as SQS MessageAttributes, so the service receiving the message can link cascading spans to the trace which created the message.
1313

1414
### receiveMessage
1515

16-
- [Messaging Attributes](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/messaging.md#messaging-attributes) are added by this instrumentation according to the spec.
17-
- Additional "processing spans" are created for each message received by the application.
18-
If an application invoked `receiveMessage`, and received a 10 messages batch, a single `messaging.operation` = `receive` span will be created for the `receiveMessage` operation, and 10 `messaging.operation` = `process` spans will be created, one for each message.
19-
Those processing spans are created by the library. This behavior is partially implemented, [See discussion below](#processing-spans).
20-
- Sets the inter process context correctly, so that additional spans created through the process will be linked to parent spans correctly.
21-
This behavior is partially implemented, [See discussion below](#processing-spans).
16+
- [Messaging Attributes](https://opentelemetry.io/docs/specs/semconv/messaging/messaging-spans/#messaging-attributes) are added by this instrumentation according to the spec.
17+
- Sets the inter process context correctly, so that additional spans created through the process will be linked to parent spans correctly.
18+
When multiple messages are received, the instrumentation will attach spank links to the receiving span containing the trace context and message ID of each message.
2219
- Extract trace context from SQS MessageAttributes, and set span's `parent` and `links` correctly according to the spec.
23-
24-
#### Processing Spans
25-
26-
See GH issue [here](https://github.com/open-telemetry/opentelemetry-js-contrib/issues/707)
27-
28-
According to OpenTelemetry specification (and to reasonable expectation for trace structure), user of this library would expect to see one span for the operation of receiving messages batch from SQS, and then, **for each message**, a span with it's own sub-tree for the processing of this specific message.
29-
30-
For example, if a `receiveMessages` returned 2 messages:
31-
32-
- `msg1` resulting in storing something to a DB.
33-
- `msg2` resulting in calling an external HTTP endpoint.
34-
35-
This will result in a creating a DB span that would be the child of `msg1` process span, and an HTTP span that would be the child of `msg2` process span (in opposed to mixing all those operations under the single `receive` span, or start a new trace for each of them).
36-
37-
Unfortunately, this is not so easy to implement in JS:
38-
39-
1. The SDK is calling a single callback for the messages batch, and it's not straightforward to understand when each individual message processing starts and ends (and set the context correctly for cascading spans).
40-
2. If async/await is used, context can be lost when returning data from async functions, for example:
41-
42-
```js
43-
async function asyncRecv() {
44-
const data = await sqs.receiveMessage(recvParams).promise();
45-
// context of receiveMessage is set here
46-
return data;
47-
}
48-
49-
async function poll() {
50-
const result = await asyncRecv();
51-
// context is lost when asyncRecv returns. following spans are created with root context.
52-
await Promise.all(
53-
result.Messages.map((message) => this.processMessage(message))
54-
);
55-
}
56-
```
57-
58-
Current implementation partially solves this issue by patching the `map` \ `forEach` \ `Filter` functions on the `Messages` array of `receiveMessage` result. This handles issues like the one above, but will not handle situations where the processing is done in other patterns (multiple map\forEach calls, index access to the array, other array operations, etc). This is currently an open issue in the instrumentation.
59-
60-
User can add custom attributes to the `process` span, by setting a function to `sqsProcessHook` in instrumentation config. For example:
61-
62-
```js
63-
awsInstrumentationConfig = {
64-
sqsProcessHook: (span, message) => {
65-
span.setAttribute("sqs.receipt_handle", message.params?.ReceiptHandle);
66-
},
67-
};
68-
```

packages/instrumentation-aws-sdk/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"dependencies": {
5151
"@opentelemetry/core": "^2.0.0",
5252
"@opentelemetry/instrumentation": "^0.203.0",
53-
"@opentelemetry/propagation-utils": "^0.31.3",
5453
"@opentelemetry/semantic-conventions": "^1.34.0"
5554
},
5655
"devDependencies": {

packages/instrumentation-aws-sdk/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ export type {
2121
AwsSdkRequestHookInformation,
2222
AwsSdkResponseCustomAttributeFunction,
2323
AwsSdkResponseHookInformation,
24-
AwsSdkSqsProcessCustomAttributeFunction,
25-
AwsSdkSqsProcessHookInformation,
2624
CommandInput,
2725
NormalizedRequest,
2826
NormalizedResponse,

packages/instrumentation-aws-sdk/src/semconv.ts

Lines changed: 81 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@
2020
* @see https://github.com/open-telemetry/opentelemetry-js/tree/main/semantic-conventions#unstable-semconv
2121
*/
2222

23+
/**
24+
* The ARN of the Secret stored in the Secrets Mangger
25+
*
26+
* @example arn:aws:secretsmanager:us-east-1:123456789012:secret:SecretName-6RandomCharacters
27+
*
28+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
29+
*/
30+
export const ATTR_AWS_SECRETSMANAGER_SECRET_ARN =
31+
'aws.secretsmanager.secret.arn' as const;
32+
33+
/**
34+
* 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.
35+
*
36+
* @example arn:aws:sns:us-east-1:123456789012:mystack-mytopic-NZJ5JSMVGFIE
37+
*
38+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
39+
*/
40+
export const ATTR_AWS_SNS_TOPIC_ARN = 'aws.sns.topic.arn' as const;
41+
2342
/**
2443
* The name of the operation being performed.
2544
*
@@ -139,42 +158,84 @@ export const ATTR_GEN_AI_USAGE_INPUT_TOKENS =
139158
export const ATTR_GEN_AI_USAGE_OUTPUT_TOKENS =
140159
'gen_ai.usage.output_tokens' as const;
141160

161+
/**
162+
* The number of messages sent, received, or processed in the scope of the batching operation.
163+
*
164+
* @example 0
165+
* @example 1
166+
* @example 2
167+
*
168+
* @note Instrumentations **SHOULD NOT** set `messaging.batch.message_count` on spans that operate with a single message. When a messaging client library supports both batch and single-message API for the same operation, instrumentations **SHOULD** use `messaging.batch.message_count` for batching APIs and **SHOULD NOT** use it for single-message APIs.
169+
*
170+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
171+
*/
172+
export const ATTR_MESSAGING_BATCH_MESSAGE_COUNT =
173+
'messaging.batch.message_count' as const;
174+
175+
/**
176+
* The message destination name
177+
*
178+
* @example MyQueue
179+
* @example MyTopic
180+
*
181+
* @note Destination name **SHOULD** uniquely identify a specific queue, topic or other entity within the broker. If
182+
* the broker doesn't have such notion, the destination name **SHOULD** uniquely identify the broker.
183+
*
184+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
185+
*/
186+
export const ATTR_MESSAGING_DESTINATION_NAME =
187+
'messaging.destination.name' as const;
188+
189+
/**
190+
* A value used by the messaging system as an identifier for the message, represented as a string.
191+
*
192+
* @example "452a7c7c7c7048c2f887f61572b18fc2"
193+
*
194+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
195+
*/
196+
export const ATTR_MESSAGING_MESSAGE_ID = 'messaging.message.id' as const;
197+
198+
/**
199+
* A string identifying the type of the messaging operation.
200+
*
201+
* @note If a custom value is used, it **MUST** be of low cardinality.
202+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
203+
*/
204+
export const ATTR_MESSAGING_OPERATION_TYPE =
205+
'messaging.operation.type' as const;
206+
142207
/**
143208
* Enum value "chat" for attribute {@link ATTR_GEN_AI_OPERATION_NAME}.
209+
*
210+
* Chat completion operation such as [OpenAI Chat API](https://platform.openai.com/docs/api-reference/chat)
211+
*
212+
* @experimental This enum value is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
144213
*/
145214
export const GEN_AI_OPERATION_NAME_VALUE_CHAT = 'chat' as const;
146215

147216
/**
148217
* Enum value "aws.bedrock" for attribute {@link ATTR_GEN_AI_SYSTEM}.
218+
*
219+
* AWS Bedrock
220+
*
221+
* @experimental This enum value is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
149222
*/
150223
export const GEN_AI_SYSTEM_VALUE_AWS_BEDROCK = 'aws.bedrock' as const;
151224

152225
/**
153226
* Enum value "input" for attribute {@link ATTR_GEN_AI_TOKEN_TYPE}.
227+
*
228+
* Input tokens (prompt, input, etc.)
229+
*
230+
* @experimental This enum value is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
154231
*/
155232
export const GEN_AI_TOKEN_TYPE_VALUE_INPUT = 'input' as const;
156233

157234
/**
158235
* Enum value "output" for attribute {@link ATTR_GEN_AI_TOKEN_TYPE}.
236+
*
237+
* Output tokens (completion, response, etc.)
238+
*
239+
* @experimental This enum value is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
159240
*/
160241
export const GEN_AI_TOKEN_TYPE_VALUE_OUTPUT = 'output' as const;
161-
162-
/**
163-
* Originally from '@opentelemetry/semantic-conventions/incubating'
164-
* https://github.com/open-telemetry/semantic-conventions/blob/main/docs/registry/attributes/aws.md#amazon-secrets-manager-attributes
165-
* The ARN of the Secret stored in the Secrets Mangger
166-
* @example arn:aws:secretsmanager:us-east-1:123456789012:secret:SecretName-6RandomCharacters
167-
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
168-
*/
169-
export const ATTR_AWS_SECRETSMANAGER_SECRET_ARN =
170-
'aws.secretsmanager.secret.arn' as const;
171-
172-
/**
173-
* Originally from '@opentelemetry/semantic-conventions/incubating'
174-
* https://github.com/open-telemetry/semantic-conventions/blob/main/docs/registry/attributes/aws.md#amazon-sns-attributes
175-
* The ARN of the AWS SNS Topic. An Amazon SNS [topic](https://docs.aws.amazon.com/sns/latest/dg/sns-create-topic.html)
176-
* is a logical access point that acts as a communication channel.
177-
* @example arn:aws:sns:us-east-1:123456789012:mystack-mytopic-NZJ5JSMVGFIE
178-
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
179-
*/
180-
export const ATTR_AWS_SNS_TOPIC_ARN = 'aws.sns.topic.arn' as const;

packages/instrumentation-aws-sdk/src/services/sqs.ts

Lines changed: 35 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ import {
1919
Span,
2020
propagation,
2121
trace,
22-
context,
2322
ROOT_CONTEXT,
2423
Attributes,
2524
} from '@opentelemetry/api';
26-
import { pubsubPropagation } from '@opentelemetry/propagation-utils';
2725
import { RequestMetadata, ServiceExtension } from './ServiceExtension';
2826
import type { SQS } from '../aws-sdk.types';
2927
import {
@@ -32,16 +30,15 @@ import {
3230
NormalizedResponse,
3331
} from '../types';
3432
import {
35-
MESSAGINGDESTINATIONKINDVALUES_QUEUE,
36-
MESSAGINGOPERATIONVALUES_PROCESS,
37-
MESSAGINGOPERATIONVALUES_RECEIVE,
38-
SEMATTRS_MESSAGING_DESTINATION,
39-
SEMATTRS_MESSAGING_DESTINATION_KIND,
40-
SEMATTRS_MESSAGING_MESSAGE_ID,
41-
SEMATTRS_MESSAGING_OPERATION,
33+
ATTR_URL_FULL,
4234
SEMATTRS_MESSAGING_SYSTEM,
43-
SEMATTRS_MESSAGING_URL,
4435
} from '@opentelemetry/semantic-conventions';
36+
import {
37+
ATTR_MESSAGING_BATCH_MESSAGE_COUNT,
38+
ATTR_MESSAGING_DESTINATION_NAME,
39+
ATTR_MESSAGING_MESSAGE_ID,
40+
ATTR_MESSAGING_OPERATION_TYPE,
41+
} from '../semconv';
4542
import {
4643
contextGetter,
4744
extractPropagationContext,
@@ -60,11 +57,9 @@ export class SqsServiceExtension implements ServiceExtension {
6057
let spanName: string | undefined;
6158

6259
const spanAttributes: Attributes = {
63-
[SEMATTRS_MESSAGING_SYSTEM]: 'aws.sqs',
64-
[SEMATTRS_MESSAGING_DESTINATION_KIND]:
65-
MESSAGINGDESTINATIONKINDVALUES_QUEUE,
66-
[SEMATTRS_MESSAGING_DESTINATION]: queueName,
67-
[SEMATTRS_MESSAGING_URL]: queueUrl,
60+
[SEMATTRS_MESSAGING_SYSTEM]: 'aws_sqs',
61+
[ATTR_MESSAGING_DESTINATION_NAME]: queueName,
62+
[ATTR_URL_FULL]: queueUrl,
6863
};
6964

7065
let isIncoming = false;
@@ -75,8 +70,7 @@ export class SqsServiceExtension implements ServiceExtension {
7570
isIncoming = true;
7671
spanKind = SpanKind.CONSUMER;
7772
spanName = `${queueName} receive`;
78-
spanAttributes[SEMATTRS_MESSAGING_OPERATION] =
79-
MESSAGINGOPERATIONVALUES_RECEIVE;
73+
spanAttributes[ATTR_MESSAGING_OPERATION_TYPE] = 'receive';
8074

8175
request.commandInput.MessageAttributeNames =
8276
addPropagationFieldsToAttributeNames(
@@ -136,61 +130,43 @@ export class SqsServiceExtension implements ServiceExtension {
136130
responseHook = (
137131
response: NormalizedResponse,
138132
span: Span,
139-
tracer: Tracer,
133+
_tracer: Tracer,
140134
config: AwsSdkInstrumentationConfig
141135
) => {
142136
switch (response.request.commandName) {
143137
case 'SendMessage':
144-
span.setAttribute(
145-
SEMATTRS_MESSAGING_MESSAGE_ID,
146-
response?.data?.MessageId
147-
);
138+
span.setAttribute(ATTR_MESSAGING_MESSAGE_ID, response?.data?.MessageId);
148139
break;
149140

150141
case 'SendMessageBatch':
151142
// TODO: How should this be handled?
152143
break;
153144

154145
case 'ReceiveMessage': {
155-
const messages: SQS.Message[] = response?.data?.Messages;
156-
if (messages) {
157-
const queueUrl = this.extractQueueUrl(response.request.commandInput);
158-
const queueName = this.extractQueueNameFromUrl(queueUrl);
159-
160-
pubsubPropagation.patchMessagesArrayToStartProcessSpans<SQS.Message>({
161-
messages,
162-
parentContext: trace.setSpan(context.active(), span),
163-
tracer,
164-
messageToSpanDetails: (message: SQS.Message) => ({
165-
name: queueName ?? 'unknown',
166-
parentContext: propagation.extract(
167-
ROOT_CONTEXT,
168-
extractPropagationContext(
169-
message,
170-
config.sqsExtractContextPropagationFromPayload
171-
),
172-
contextGetter
173-
),
146+
const messages: SQS.Message[] = response?.data?.Messages || [];
147+
148+
span.setAttribute(ATTR_MESSAGING_BATCH_MESSAGE_COUNT, messages.length);
149+
150+
for (const message of messages) {
151+
const propagatedContext = propagation.extract(
152+
ROOT_CONTEXT,
153+
extractPropagationContext(
154+
message,
155+
config.sqsExtractContextPropagationFromPayload
156+
),
157+
contextGetter
158+
);
159+
160+
const spanContext = trace.getSpanContext(propagatedContext);
161+
162+
if (spanContext) {
163+
span.addLink({
164+
context: spanContext,
174165
attributes: {
175-
[SEMATTRS_MESSAGING_SYSTEM]: 'aws.sqs',
176-
[SEMATTRS_MESSAGING_DESTINATION]: queueName,
177-
[SEMATTRS_MESSAGING_DESTINATION_KIND]:
178-
MESSAGINGDESTINATIONKINDVALUES_QUEUE,
179-
[SEMATTRS_MESSAGING_MESSAGE_ID]: message.MessageId,
180-
[SEMATTRS_MESSAGING_URL]: queueUrl,
181-
[SEMATTRS_MESSAGING_OPERATION]:
182-
MESSAGINGOPERATIONVALUES_PROCESS,
166+
[ATTR_MESSAGING_MESSAGE_ID]: message.MessageId,
183167
},
184-
}),
185-
processHook: (span: Span, message: SQS.Message) =>
186-
config.sqsProcessHook?.(span, { message }),
187-
});
188-
189-
pubsubPropagation.patchArrayForProcessSpans(
190-
messages,
191-
tracer,
192-
context.active()
193-
);
168+
});
169+
}
194170
}
195171
break;
196172
}

0 commit comments

Comments
 (0)