-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: AWS-SDK SNS Context propagation (#728)
- Loading branch information
Showing
8 changed files
with
363 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
plugins/node/opentelemetry-instrumentation-aws-sdk/doc/sns.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# SNS | ||
|
||
SNS is amazon's managed pub/sub system. 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). | ||
|
||
## Specific trace semantic | ||
|
||
The following methods are automatically enhanced: | ||
|
||
### Publish messages | ||
|
||
- [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. | ||
- OpenTelemetry trace context is injected as SNS MessageAttributes, so the service receiving the message can link cascading spans to the trace which created the message. | ||
|
||
### Consumers | ||
There are many potential consumers: SQS, Lambda, HTTP/S, Email, SMS, mobile notifications. each one of them will received the propagated context in its own way. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/MessageAttributes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { | ||
TextMapGetter, | ||
TextMapSetter, | ||
context, | ||
propagation, | ||
diag, | ||
} from '@opentelemetry/api'; | ||
import type { SQS, SNS } from 'aws-sdk'; | ||
|
||
// https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-quotas.html | ||
export const MAX_MESSAGE_ATTRIBUTES = 10; | ||
class ContextSetter | ||
implements | ||
TextMapSetter<SQS.MessageBodyAttributeMap | SNS.MessageAttributeMap> | ||
{ | ||
set( | ||
carrier: SQS.MessageBodyAttributeMap | SNS.MessageAttributeMap, | ||
key: string, | ||
value: string | ||
) { | ||
carrier[key] = { | ||
DataType: 'String', | ||
StringValue: value as string, | ||
}; | ||
} | ||
} | ||
export const contextSetter = new ContextSetter(); | ||
|
||
class ContextGetter | ||
implements | ||
TextMapGetter<SQS.MessageBodyAttributeMap | SNS.MessageAttributeMap> | ||
{ | ||
keys( | ||
carrier: SQS.MessageBodyAttributeMap | SNS.MessageAttributeMap | ||
): string[] { | ||
return Object.keys(carrier); | ||
} | ||
|
||
get( | ||
carrier: SQS.MessageBodyAttributeMap | SNS.MessageAttributeMap, | ||
key: string | ||
): undefined | string | string[] { | ||
return carrier?.[key]?.StringValue; | ||
} | ||
} | ||
export const contextGetter = new ContextGetter(); | ||
|
||
export const injectPropagationContext = ( | ||
attributesMap?: SQS.MessageBodyAttributeMap | SNS.MessageAttributeMap | ||
): SQS.MessageBodyAttributeMap | SNS.MessageAttributeMap => { | ||
const attributes = attributesMap ?? {}; | ||
if (Object.keys(attributes).length < MAX_MESSAGE_ATTRIBUTES) { | ||
propagation.inject(context.active(), attributes, contextSetter); | ||
} else { | ||
diag.warn( | ||
'aws-sdk instrumentation: cannot set context propagation on SQS/SNS message due to maximum amount of MessageAttributes' | ||
); | ||
} | ||
return attributes; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
plugins/node/opentelemetry-instrumentation-aws-sdk/src/services/sns.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { Span, Tracer, SpanKind } from '@opentelemetry/api'; | ||
import { | ||
MessagingDestinationKindValues, | ||
SemanticAttributes, | ||
} from '@opentelemetry/semantic-conventions'; | ||
import { | ||
NormalizedRequest, | ||
NormalizedResponse, | ||
AwsSdkInstrumentationConfig, | ||
} from '../types'; | ||
import { injectPropagationContext } from './MessageAttributes'; | ||
import { RequestMetadata, ServiceExtension } from './ServiceExtension'; | ||
|
||
export class SnsServiceExtension implements ServiceExtension { | ||
requestPreSpanHook(request: NormalizedRequest): RequestMetadata { | ||
let spanKind: SpanKind = SpanKind.CLIENT; | ||
let spanName = `SNS ${request.commandName}`; | ||
const spanAttributes = { | ||
[SemanticAttributes.MESSAGING_SYSTEM]: 'aws.sns', | ||
}; | ||
|
||
if (request.commandName === 'Publish') { | ||
spanKind = SpanKind.PRODUCER; | ||
|
||
spanAttributes[SemanticAttributes.MESSAGING_DESTINATION_KIND] = | ||
MessagingDestinationKindValues.TOPIC; | ||
const { TopicArn, TargetArn, PhoneNumber } = request.commandInput; | ||
spanAttributes[SemanticAttributes.MESSAGING_DESTINATION] = | ||
this.extractDestinationName(TopicArn, TargetArn, PhoneNumber); | ||
|
||
spanName = `${spanAttributes[SemanticAttributes.MESSAGING_DESTINATION]} ${ | ||
request.commandName | ||
}`; | ||
} | ||
|
||
return { | ||
isIncoming: false, | ||
spanAttributes, | ||
spanKind, | ||
spanName, | ||
}; | ||
} | ||
|
||
requestPostSpanHook(request: NormalizedRequest): void { | ||
if (request.commandName === 'Publish') { | ||
const origMessageAttributes = | ||
request.commandInput['MessageAttributes'] ?? {}; | ||
if (origMessageAttributes) { | ||
request.commandInput['MessageAttributes'] = injectPropagationContext( | ||
origMessageAttributes | ||
); | ||
} | ||
} | ||
} | ||
|
||
responseHook( | ||
response: NormalizedResponse, | ||
span: Span, | ||
tracer: Tracer, | ||
config: AwsSdkInstrumentationConfig | ||
): void {} | ||
|
||
extractDestinationName( | ||
topicArn: string, | ||
targetArn: string, | ||
phoneNumber: string | ||
): string { | ||
if (topicArn || targetArn) { | ||
const arn = topicArn ?? targetArn; | ||
try { | ||
return arn.substr(arn.lastIndexOf(':') + 1); | ||
} catch (err) { | ||
return arn; | ||
} | ||
} else if (phoneNumber) { | ||
return phoneNumber; | ||
} else { | ||
return 'unknown'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.