Skip to content

Instantly share code, notes, and snippets.

@nathanpeck
Last active December 9, 2020 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanpeck/f5fbba9e502a4d39991b9024658cfbdd to your computer and use it in GitHub Desktop.
Save nathanpeck/f5fbba9e502a4d39991b9024658cfbdd to your computer and use it in GitHub Desktop.
// 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
Copy link

@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
Copy link
Author

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
Copy link

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