Sampling may be employed to reduce the data collected and reported out of process. When a span is not sampled, it adds no overhead (a noop).
Sampling is an up-front decision, meaning that the decision to report data is made at the first operation in a trace and that decision is propagated downstream.
By default, a global sampler applies a single rate to all traced operations.
Tracer.Builder.sampler
controls this setting, and it defaults to tracing every request.
Some applications need to sample based on the type or annotations of a java method.
Most users use a framework interceptor to automate this sort of policy. The following example shows how that might work internally:
@Autowired Tracing tracing; // derives a sample rate from an annotation on a java method DeclarativeSampler<Traced> sampler = DeclarativeSampler.create(Traced::sampleRate); @Around("@annotation(traced)") public Object traceThing(ProceedingJoinPoint pjp, Traced traced) throws Throwable { Span span = tracing.tracer().newTrace(sampler.sample(traced))... try { return pjp.proceed(); } finally { span.finish(); } }
Depending on what the operation is, you may want to apply different policies. For example, you might not want to trace requests to static resources such as images, or you might want to trace all requests to a new api.
Most users use a framework interceptor to automate this sort of policy. The following example shows how that might work internally:
@Autowired Tracer tracer; Span newTrace(Request input) { SamplingFlags flags = SamplingFlags.NONE; if (input.url().startsWith("/experimental")) { flags = SamplingFlags.SAMPLED; } else if (input.url().startsWith("/static")) { flags = SamplingFlags.NOT_SAMPLED; } return tracer.newTrace(flags); }
By default Spring Cloud Sleuth sets all spans to non-exportable.
That means that traces appear in logs but not in any remote store.
For testing the default is often enough, and it probably is all you need if you use only the logs (for example, with an ELK aggregator).
If you export span data to Zipkin, there is also an Sampler.ALWAYS_SAMPLE
setting that exports everything and a ProbabilityBasedSampler
setting that samples a fixed fraction of spans.
Note | |
---|---|
The |
A sampler can be installed by creating a bean definition, as shown in the following example:
@Bean public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; }
Tip | |
---|---|
You can set the HTTP header |