Is there any way to drop unwanted spans in micrometer-tracing with opentelemetry before sent it to collector?
I tried to filtered spans by configuring custom Sampler from openTelemetry api.
if management.tracing.sampling.probability=1.0 , then it works expected.
but if management.tracing.sampling.probability=0.0 then, it drop parent span and sends child spans.
Expectation: It should not sent any spans, if probability=0.0
@Override
public SamplingResult shouldSample(Context context, String traceId, String name, SpanKind spanKind, Attributes attributes, List<LinkData> list) {
if (name.equalsIgnoreCase("xyz")) {
return SamplingResult.drop();
} else {
// put condition for record and recordAndSample based on sampling probability.
return SamplingResult.recordAndSample();
}
}
@Override
public String getDescription() {
return CustomSampler.class.getSimpleName();
}
}
@AutoConfigureBefore(OpenTelemetryAutoConfiguration.class)
@ConditionalOnClass({ OtelTracer.class, SdkTracerProvider.class, OpenTelemetry.class })
@EnableConfigurationProperties(TracingProperties.class)
public class SamplerConfig {
@Bean
@Primary
public Sampler customSampler() {
Float samplerProbability = this.tracingProperties.getSampling().getProbability();
return Sampler.parentBasedBuilder(Sampler.traceIdRatioBased(samplerProbability))
.setRemoteParentNotSampled(new CustomSampler())
.setLocalParentNotSampled(new CustomSampler())
.setLocalParentSampled(new CustomSampler())
.setRemoteParentSampled(new CustomSampler())
.build();
}
}
Please suggest if there is any other good way to achieve this.
You can use the SpanExportingPredicate from Micrometer Tracing or set up the Sampler in your configuration.