| 30 | import { getAWS, execute } from '../aws/SDK2'; |
| 31 | |
| 32 | class AWS2SQSPlugin implements SwPlugin { |
| 33 | readonly module = 'aws-sdk'; |
| 34 | readonly versions = '2.*'; |
| 35 | |
| 36 | install(installer: PluginInstaller): void { |
| 37 | const AWS = getAWS(installer); |
| 38 | const _SQS = AWS.SQS; |
| 39 | |
| 40 | function SQS(this: any) { |
| 41 | const sqs = _SQS.apply(this, arguments); |
| 42 | |
| 43 | function instrumentSend(name: string, addTraceId: any): void { |
| 44 | const _func = sqs[name]; |
| 45 | |
| 46 | sqs[name] = function (params: any, callback: any) { |
| 47 | const queueUrl = params.QueueUrl; |
| 48 | const operation = `AWS/SQS/${name}/${queueUrl.slice(queueUrl.lastIndexOf('/') + 1)}`; |
| 49 | const span = ContextManager.current.newExitSpan(operation, Component.AWS_SQS, Component.HTTP); |
| 50 | |
| 51 | span.component = Component.AWS_SQS; |
| 52 | span.layer = SpanLayer.MQ; |
| 53 | |
| 54 | return execute(span, this, _func, addTraceId(params, span), callback, 'mqBroker'); |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | instrumentSend('sendMessage', (params: any, span: Span) => { |
| 59 | params = Object.assign({}, params); |
| 60 | params.MessageAttributes = params.MessageAttributes ? Object.assign({}, params.MessageAttributes) : {}; |
| 61 | params.MessageAttributes.__revdTraceId = { |
| 62 | DataType: 'String', |
| 63 | StringValue: `${span.inject().value}/${hostname()}`, |
| 64 | }; |
| 65 | |
| 66 | return params; |
| 67 | }); |
| 68 | |
| 69 | instrumentSend('sendMessageBatch', (params: any, span: Span) => { |
| 70 | const traceId = { __revdTraceId: { DataType: 'String', StringValue: `${span.inject().value}/${hostname()}` } }; |
| 71 | params = Object.assign({}, params); |
| 72 | params.Entries = params.Entries.map( |
| 73 | (e: any) => |
| 74 | (e = Object.assign({}, e, { |
| 75 | MessageAttributes: e.MessageAttributes ? Object.assign({}, e.MessageAttributes, traceId) : traceId, |
| 76 | })), |
| 77 | ); |
| 78 | |
| 79 | return params; |
| 80 | }); |
| 81 | |
| 82 | const _receiveMessage = sqs.receiveMessage; |
| 83 | |
| 84 | sqs.receiveMessage = function (params: any, callback: any) { |
| 85 | params = Object.assign({}, params); |
| 86 | const _MessageAttributeNames = params.MessageAttributeNames; |
| 87 | params.MessageAttributeNames = _MessageAttributeNames |
| 88 | ? _MessageAttributeNames.concat(['__revdTraceId']) |
| 89 | : ['__revdTraceId']; |
nothing calls this directly
no outgoing calls
no test coverage detected