Last active
January 13, 2020 05:06
-
-
Save endeepak/ad970e3bc11d926e6c55e67c35e8bc3c to your computer and use it in GitHub Desktop.
NodeJS Request Log Tracing : SNS Event Publisher
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
const requestTracing = require('request-tracing'); // Wrapper library to read the tracing information from CLS context | |
class SNSEventPublisher { | |
constructor(sns, topicNameToArnMap) { | |
this.sns = sns; | |
this.topicNameToArnMap = topicNameToArnMap; | |
} | |
async publish(topicName, message) { | |
const messageAttributes = {}; | |
const tracingId = requestTracing.getTracingId(); // Same as requestTracingNamespace.get('tracingId'); | |
if(tracingId) { | |
messageAttributes['X-Tracing-Id'] = { | |
DataType: 'String', | |
StringValue: tracingId | |
}; | |
} | |
const params = { | |
Message: message, | |
TopicArn: this.topicNameToArnMap[topicName], | |
MessageAttributes: messageAttributes | |
}; | |
const resp = await this.sns.publish(params).promise(); | |
return resp.MessageId; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment