- Newest
- Most votes
- Most comments
You can use CentralizedSamplingStrategy class independently just as how a servlet filter would use it. https://github.com/aws/aws-xray-sdk-java/blob/master/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/strategy/sampling/CentralizedSamplingStrategy.java
The shouldTrace method takes a sampling request and returns a decision to the caller. Now the question becomes how would you like to use different rules for matching on certain message processing. Or if you just want to config the default rule in which case an empty sampling request object should just work.
Please let me know if this answers your question.
Thanks,
Haotian
Hi,
Sorry for the late reply. Thanks for having a look and replying for my issue. Your answer helped me go ahead with the work around. Calling the shouldTrace API will internally kick off the RulePoller and TargetPoller which will regularly poll for rules that qualify for the current segment being started. So my understanding is that, all the rules that I have configured on the Xray console and that are applicable for this segment ( I identify segments based on unique service names ), will be automatically applied by Xray SDK. Here is the code that I developed for beginning segments for Worker Threads:
/**
* AWS XRay does not seem to support centralized sampling of custom segments started in a
* application context which is not instantiated by a HTTP request [In Worker Threads]. As
* a result, Sampling falls back to default localized sampling. As per Design, where it
* is expected that the default Sampling Rules configured for the environment on the XRay
* Console, these default rules should control the sampling rate always.
*
* This method helps in beginning a new segment in the above case where the Sampling strategy
* configured on the recorder is consulted to check if the segment needs to sampled or not before
* the segment is created.
*
* @param segmentName
* @return
*/
public static Segment beginSegment( String segmentName )
{
AWSXRayRecorder recorder = AWSXRay.getGlobalRecorder();
SamplingStrategy samplingStrategy = recorder.getSamplingStrategy();
Segment created = null;
SamplingRequest samplingRequest = new SamplingRequest( segmentName, "", "", "", recorder.getOrigin() );
SamplingResponse samplingResponse = samplingStrategy.shouldTrace( samplingRequest );
SampleDecision sampleDecision = getSampleDecision( samplingResponse );
if ( SampleDecision.SAMPLED.equals( sampleDecision ) ) {
created = recorder.beginSegment( segmentName );
if ( samplingResponse.getRuleName().isPresent() ) {
LOGGER.debug( "Sampling strategy decided to use rule named: " + samplingResponse.getRuleName().get() + "." );
created.setRuleName( samplingResponse.getRuleName().get() );
}
} else {
if ( samplingStrategy.isForcedSamplingSupported() ) {
created = recorder.beginSegment( segmentName );
created.setSampled( false );
} else {
created = recorder.beginDummySegment();
}
}
return created;
}
Please do let me know if this is what you suggested. Also, please advice if I should factor in trace ID for the segments that I create this way.
Regards,
Shri
Edited by: shricloud on Jul 3, 2019 12:25 PM
Edited by: shricloud on Jul 3, 2019 12:26 PM
Edited by: shricloud on Jul 3, 2019 12:27 PM
Relevant content
- asked 3 years ago
- asked 4 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 7 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated a year ago