How to filter or drop spans before sent it to collector in micrometer with Otel in spring 3?
07:46 01 Oct 2023

Is there any way to drop unwanted spans in micrometer-tracing with opentelemetry before sent it to collector?

I tried to filter 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 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.

open-telemetry spring-3 distributed-tracing micrometer-tracing