|
1 | | -import { Attributes } from "@opentelemetry/api"; |
| 1 | +import { Attributes, Tracer, SpanKind, Span } from "@opentelemetry/api"; |
2 | 2 | import { RequestMetadata } from "./service-attributes"; |
3 | 3 |
|
| 4 | +export enum SqsAttributeNames { |
| 5 | + // https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/messaging.md |
| 6 | + MESSAGING_SYSTEM = "messaging.system", |
| 7 | + MESSAGING_DESTINATION = "messaging.destination", |
| 8 | + MESSAGING_DESTINATIONKIND = "messaging.destination_kind", |
| 9 | + MESSAGING_MESSAGE_ID = "messaging.message_id", |
| 10 | + MESSAGING_OPERATION = "messaging.operation", |
| 11 | + MESSAGING_URL = "messaging.url", |
| 12 | +} |
| 13 | + |
| 14 | +export const START_SPAN_FUNCTION = Symbol( |
| 15 | + "opentelemetry.plugin.aws-sdk.start_span" |
| 16 | +); |
| 17 | + |
| 18 | +export const END_SPAN_FUNCTION = Symbol( |
| 19 | + "opentelemetry.plugin.aws-sdk.end_span" |
| 20 | +); |
| 21 | + |
| 22 | +function extractQueueUrl(request: AWS.Request<any, any>): string { |
| 23 | + return (request as any)?.params?.QueueUrl; |
| 24 | +} |
| 25 | + |
| 26 | +function extractQueueNameFromUrl(queueUrl: string): string { |
| 27 | + if (!queueUrl) return undefined; |
| 28 | + |
| 29 | + const pisces = queueUrl.split("/"); |
| 30 | + if (pisces.length === 0) return undefined; |
| 31 | + |
| 32 | + return pisces[pisces.length - 1]; |
| 33 | +} |
| 34 | + |
| 35 | +function startSingleMessageSpan( |
| 36 | + tracer: Tracer, |
| 37 | + queueUrl: string, |
| 38 | + queueName: string, |
| 39 | + message: AWS.SQS.Message |
| 40 | +): Span { |
| 41 | + const messageSpan = tracer.startSpan(queueName, { |
| 42 | + kind: SpanKind.CONSUMER, |
| 43 | + attributes: { |
| 44 | + [SqsAttributeNames.MESSAGING_SYSTEM]: "aws.sqs", |
| 45 | + [SqsAttributeNames.MESSAGING_DESTINATION]: queueName, |
| 46 | + [SqsAttributeNames.MESSAGING_DESTINATIONKIND]: "queue", |
| 47 | + [SqsAttributeNames.MESSAGING_MESSAGE_ID]: message.MessageId, |
| 48 | + [SqsAttributeNames.MESSAGING_URL]: queueUrl, |
| 49 | + [SqsAttributeNames.MESSAGING_OPERATION]: "process", |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + message[START_SPAN_FUNCTION] = () => |
| 54 | + console.log( |
| 55 | + "open-telemetry aws-sdk plugin: trying to start sqs processing span twice." |
| 56 | + ); |
| 57 | + message[END_SPAN_FUNCTION] = () => { |
| 58 | + messageSpan.end(); |
| 59 | + message[END_SPAN_FUNCTION] = () => |
| 60 | + console.log( |
| 61 | + "open-telemetry aws-sdk plugin: trying to end sqs processing span which was already ended." |
| 62 | + ); |
| 63 | + }; |
| 64 | + return messageSpan; |
| 65 | +} |
| 66 | + |
| 67 | +function patchArrayFunction( |
| 68 | + messages: AWS.SQS.Message[], |
| 69 | + functionName: string, |
| 70 | + tracer: Tracer |
| 71 | +) { |
| 72 | + const origFunc = messages[functionName]; |
| 73 | + messages[functionName] = function (callback) { |
| 74 | + return origFunc.call(this, function (message: AWS.SQS.Message) { |
| 75 | + const messageSpan = message[START_SPAN_FUNCTION](); |
| 76 | + tracer.withSpan(messageSpan, () => callback.apply(this, arguments)); |
| 77 | + message[END_SPAN_FUNCTION](); |
| 78 | + }); |
| 79 | + }; |
| 80 | +} |
| 81 | + |
4 | 82 | export function getSqsRequestSpanAttributes( |
5 | 83 | request: AWS.Request<any, any> |
6 | 84 | ): RequestMetadata { |
| 85 | + const queueUrl = extractQueueUrl(request); |
| 86 | + const queueName = extractQueueNameFromUrl(queueUrl); |
| 87 | + |
| 88 | + let isIncoming = false; |
| 89 | + const attributes = { |
| 90 | + [SqsAttributeNames.MESSAGING_SYSTEM]: "aws.sqs", |
| 91 | + [SqsAttributeNames.MESSAGING_DESTINATIONKIND]: "queue", |
| 92 | + [SqsAttributeNames.MESSAGING_DESTINATION]: queueName, |
| 93 | + [SqsAttributeNames.MESSAGING_URL]: queueUrl, |
| 94 | + }; |
| 95 | + |
7 | 96 | const operation = (request as any)?.operation; |
8 | 97 | switch (operation) { |
9 | 98 | case "receiveMessage": |
10 | | - return { |
11 | | - attributes: {}, |
12 | | - isIncoming: true, |
13 | | - }; |
| 99 | + isIncoming = true; |
| 100 | + attributes[SqsAttributeNames.MESSAGING_OPERATION] = "receive"; |
| 101 | + break; |
14 | 102 | } |
15 | 103 |
|
16 | 104 | return { |
17 | | - attributes: {}, |
18 | | - isIncoming: false, |
| 105 | + attributes, |
| 106 | + isIncoming, |
19 | 107 | }; |
20 | 108 | } |
21 | 109 |
|
22 | 110 | export function getSqsResponseSpanAttributes( |
23 | | - response: AWS.Response<any, any> |
| 111 | + response: AWS.Response<any, any>, |
| 112 | + tracer: Tracer |
24 | 113 | ): Attributes { |
| 114 | + const messages: AWS.SQS.Message[] = response.data.Messages; |
| 115 | + if (messages) { |
| 116 | + const queueUrl = extractQueueUrl((response as any)?.request); |
| 117 | + const queueName = extractQueueNameFromUrl(queueUrl); |
| 118 | + |
| 119 | + messages.forEach((message: AWS.SQS.Message) => { |
| 120 | + message[START_SPAN_FUNCTION] = () => |
| 121 | + startSingleMessageSpan(tracer, queueUrl, queueName, message); |
| 122 | + message[END_SPAN_FUNCTION] = () => |
| 123 | + console.log( |
| 124 | + "open-telemetry aws-sdk plugin: end span called on sqs message which was not started" |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + patchArrayFunction(messages, "forEach", tracer); |
| 129 | + patchArrayFunction(messages, "map", tracer); |
| 130 | + } |
25 | 131 | return {}; |
26 | 132 | } |
0 commit comments