How to filter or drop spans before sent it to collector in micrometer with Otel in spring 3?

395 Views Asked by At

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.

1

There are 1 best solutions below

4
Marcin Grzejszczak On

You can use the SpanExportingPredicate from Micrometer Tracing or set up the Sampler in your configuration.