Skip to content

Instantly share code, notes, and snippets.

@endeepak
Last active January 13, 2020 05:06
Show Gist options
  • Save endeepak/ad970e3bc11d926e6c55e67c35e8bc3c to your computer and use it in GitHub Desktop.
Save endeepak/ad970e3bc11d926e6c55e67c35e8bc3c to your computer and use it in GitHub Desktop.
NodeJS Request Log Tracing : SNS Event Publisher
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