suraj
suraj

Reputation: 61

How to exclude urls(health check traces) in opentelemetry

I am trying to integrate opentelemetry in spring boot with Automatic instrumentation, it's generating health check urls. How to exclude specific urls in opentelemetry

Upvotes: 4

Views: 8629

Answers (2)

ptanov
ptanov

Reputation: 76

Opentelemetry Samplers are used to filter spans. They can be registered globally using environment variable OTEL_TRACES_SAMPLER as mentioned here: https://opentelemetry.io/docs/concepts/sdk-configuration/general-sdk-configuration/#otel_traces_sampler

You can check this link for an example of a custom sampler (called DemoSampler : https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/examples/extension#extensions-examples (these two files: DemoSampler and DemoConfigurableSamplerProvider). To try it you have to clone the repository, enter examples/extension folder and run ./gradlew build. Then you should use environment variable OTEL_JAVAAGENT_EXTENSIONS (or system property otel.javaagent.extensions) to reference created jar opentelemetry-java-instrumentation/examples/extension/build/libs/opentelemetry-java-instrumentation-extension-demo-1.0-all.jar as mentioned here: https://opentelemetry.io/docs/instrumentation/java/automatic/agent-config/. Then set environment variable OTEL_TRACES_SAMPLER to demo (this name is defined by the sampler provider https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/65251c4e8a4bdc60ee8b3dbe9fc4d2ccd36f6bda/examples/extension/src/main/java/com/example/javaagent/DemoConfigurableSamplerProvider.java#L23).

Your custom sampler should check every sample's name, spanKind and attributes in the method Sampler.shouldSample(Context parentContext, String traceId, String name, SpanKind spanKind, Attributes attributes, List<LinkData> parentLinks) and decide whether it is a healtcheck or something other and return either SamplingResult.create(SamplingDecision.DROP); or SamplingResult.create(SamplingDecision.RECORD_AND_SAMPLE); respectively.

Upvotes: 1

user20623698
user20623698

Reputation: 31

/**
 * This demo sampler filters out all internal spans whose name contains string "greeting".
 *
 * <p>See <a
 * href="https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/sdk.md#sampling">
 * OpenTelemetry Specification</a> for more information about span sampling.
 *
 */
public class MySampler implements Sampler {
  @Override
  public SamplingResult shouldSample(
      Context parentContext,
      String traceId,
      String name,
      SpanKind spanKind,
      Attributes attributes,
      List<LinkData> parentLinks) {
      String attr =  attributes.get(InternalAttributeKeyImpl.create("http.target", AttributeType.STRING));

    if(name.contains("health"))
    {
      return SamplingResult.create(SamplingDecision.DROP);
    } else if(spanKind == SpanKind.INTERNAL && name.contains("OperationHandler.handle"))
    {
      return SamplingResult.create(SamplingDecision.DROP);
    } else if (spanKind == SpanKind.SERVER &&
            (attr != null && attr.contains("actuator"))) {
      return SamplingResult.create(SamplingDecision.DROP);
    } else {
      return SamplingResult.create(SamplingDecision.RECORD_AND_SAMPLE);
    }
  }

  @Override
  public String getDescription() {
    return "MySampler";
  }
}




/**
 * This is one of the main entry points for Instrumentation Agent's customizations. It allows
 * configuring the {@link AutoConfigurationCustomizer}. See the {@link
 * #customize(AutoConfigurationCustomizer)} method below.
 *
 * <p>Also see https://github.com/open-telemetry/opentelemetry-java/issues/2022
 *
 * @see AutoConfigurationCustomizerProvider
 */
@AutoService(AutoConfigurationCustomizerProvider.class)
public class MyAutoConfigurationCustomizerProvider
        implements AutoConfigurationCustomizerProvider {

    @Override
    public void customize(AutoConfigurationCustomizer autoConfiguration) {
        Resource resourceNew = Resource.getDefault()
                .merge(Resource.create(Attributes.of(ResourceAttributes.SERVICE_INSTANCE_ID, System.getenv("MY_POD_NAME"))))
                .merge(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, System.getenv("DT_RELEASE_PRODUCT"))));

        autoConfiguration.addSamplerCustomizer((sampler, configProperties) -> new MySampler());
        autoConfiguration.addResourceCustomizer((resource, configProperties) -> resourceNew);
    }
}

Upvotes: 3

Related Questions