Last active
December 9, 2020 18:42
-
-
Save nathanpeck/f5fbba9e502a4d39991b9024658cfbdd to your computer and use it in GitHub Desktop.
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
// 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(); | |
}); | |
}); | |
}; |
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
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
@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 :(