M - The type that uniquely identifies this method, specifically for tracing. Most often a
trace annotation, but could also be a Method or another declarative
reference such as javax.ws.rs.container.ResourceInfo.public final class DeclarativeSampler<M> extends Object
Example: A user defines an annotation, for example com.myco.Traced, and a lookup
function for its rate (could be simple as reading a field, or even a constant). An interceptor
uses this sampler on each invocation of a potentially annotated target. The result decides
whether a new trace should be started or not.
No runtime parameters are considered here, but that doesn't mean you can't achieve
parameterized sampling using this. If your method is annotated such that it only accepts a
fraction of requests, adding a custom @Traced annotation would apply to that subset. For
example, if you have a JAX-RS method, it is already qualified by method and likely path. A user
can add and inspect their own grouping annotation to override whatever the default rate is.
Under the scenes, a map of samplers by method is maintained. The size of this map should not be a problem when it directly relates to declared methods. For example, this would be invalid if annotations were created at runtime and didn't match.
| Modifier and Type | Class | Description |
|---|---|---|
static interface |
DeclarativeSampler.RateForMethod<M> |
| Modifier and Type | Method | Description |
|---|---|---|
static <M> DeclarativeSampler<M> |
create(DeclarativeSampler.RateForMethod<M> rateForMethod) |
|
SamplingFlags |
sample(M method) |
|
Sampler |
toSampler(M method) |
Used with
Tracer.withSampler(Sampler) to override the default sampling decision. |
Sampler |
toSampler(M method,
Sampler fallback) |
Like
toSampler(Object), except allows a fallback decision, usually from
Tracing.sampler(), when there was no rate for an input |
public static <M> DeclarativeSampler<M> create(DeclarativeSampler.RateForMethod<M> rateForMethod)
public Sampler toSampler(M method)
Tracer.withSampler(Sampler) to override the default sampling decision.
Ex:
// When there is no trace in progress, this decides using an annotation
Sampler decideUsingAnnotation = declarativeSampler.toSampler(traced);
Tracer tracer = tracing.tracer().withSampler(decideUsingAnnotation);
// This code looks the same as if there was no declarative override
Span span = tracer.nextSpan().name(name).start();
method - input to the sampling functionpublic Sampler toSampler(M method, Sampler fallback)
toSampler(Object), except allows a fallback decision, usually from
Tracing.sampler(), when there was no rate for an input
Ex:
// When there is no trace in progress, this decides using an annotation
Sampler decideUsingAnnotation = declarativeSampler.toSampler(traced, tracing.sampler());
Tracer tracer = tracing.tracer().withSampler(decideUsingAnnotation);
// This code looks the same as if there was no declarative override
brave.Span span = tracer.nextSpan().name("").start();
method - input to the sampling functionfallback - when there is no rate for the input, usually Tracing.sampler()public SamplingFlags sample(@Nullable M method)
Copyright © 2018 OpenZipkin. All rights reserved.