Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// This handler function gets called for each SQS message that arrives in my process
mySqsHandler = function(msg) {
var self = this;
if (!msg.Attributes || !msg.Attributes.AWSTraceHeader) {
// This message was not selected for tracing by the sampler in
// a higher level service
runMySqsLogic(msg, function() {
// Noop, no segment to run
});
}
var trace = AWSXRay.utils.processTraceData(msg.Attributes.AWSTraceHeader);
var segment = new AWSXRay.Segment('message-distributor', trace.Root, trace.Parent);
segment.notTraced = (trace.Sampled !== '1');
// Create an instrumented namespace that will create trace spans.
var ns = AWSXRay.getNamespace();
ns.run(function() {
AWSXRay.setSegment(segment);
runMySqsLogic(msg, function() {
// After my SQS logic ran don't forget to close the segment.
segment.close();
});
});
};
@gvasquez95

This comment has been minimized.

Copy link

@gvasquez95 gvasquez95 commented Dec 9, 2020

@nathanpeck I'm following from your tweeter feedback, and what I'm missing here is the AWSTraceHeader, which I don't really have in the context of our code. What should be the proper way to start a new trace from a "blank slate"?

I'm specifically referring to lines 13-14 where your extract de traceHeader from the msg, data that we don't have :(

@nathanpeck

This comment has been minimized.

Copy link
Owner Author

@nathanpeck nathanpeck commented Dec 9, 2020

Ah in that case you can skip that. For this example the only way the SQS message would get to this process was because it was generated by a higher level service that would have initiated the transaction and have a trace header already. You can start a brand new segment from scratch if you want to by just leaving out that if statement on lines 5-11

@gvasquez95

This comment has been minimized.

Copy link

@gvasquez95 gvasquez95 commented Dec 9, 2020

ok, and what about testing this locally on my computer, how would the x-ray agent should be run?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment