opentelemetry-java-instrumentation icon indicating copy to clipboard operation
opentelemetry-java-instrumentation copied to clipboard

Exclude URLs from Tracing

Open jakob-o opened this issue 5 years ago • 59 comments

Is your feature request related to a problem? Please describe. As already mentioned here https://github.com/open-telemetry/opentelemetry-specification/issues/173 I'd like to be able to exclude or sample a list of URLs / URL-Patterns from instrumentation. In my case particularly to avoid generating many events from health- and liveness-checks.

Describe the solution you'd like I opened the issue https://github.com/open-telemetry/opentelemetry-java/issues/1552 to discuss if / how tracing might be disabled from instrumentation. To my knowledge there currently is no API / SDK method to disable tracing centrally on the context. If I understood correctly a (maybe temporary) solution might be to create a non-recording / invalid span in the HTTP instrumentation, which due to the ParentOrElse-Sampler, would lead to ignoring child spans as well, if the request matches a URL pattern. Any hint to where this would be architecturally appropriately implemented is highly appreciated. Maybe in the HttpServerTracer?

Describe alternatives you've considered We already attempted to use the otel.trace.classes.exclude but only succeeded in completely disabling WebMvc instrumentation.

/CC @gabrielthunig @spaletta

jakob-o avatar Aug 19 '20 14:08 jakob-o

If I understood correctly a (maybe temporary) solution might be to create a non-recording / invalid span in the HTTP instrumentation, which due to the ParentOrElse-Sampler, would lead to ignoring child spans as well, if the request matches a URL pattern. Any hint to where this would be architecturally appropriately implemented is highly appreciated. Maybe in the HttpServerTracer

That seems like a reasonable approach :)

iNikem avatar Aug 21 '20 15:08 iNikem

Been trying auto instrumentation in a container lately, and was slightly annoyed myself with tracing of health check. Then a customer trying it out independently had the same feedback - it's exactly the kind of input we're hoping for in trials :) So I may actually mark this required for GA, the UX is impacted a lot with having no control of tracing by URL pattern.

anuraaga avatar Sep 16 '20 15:09 anuraaga

I have started looking into this.

Is there a proposed design for this? In OpenTracing this was an instrumentation feature. The instrumentation check if the URL matches exclude pattern if yes then the span wasn't created. However if the excluded URL uses another instrumentation (or makes downstream call) that would create a span. The question is whether we want to exclude just specific URLs or the whole trace starting at that URL.

pavolloffay avatar Sep 24 '20 06:09 pavolloffay

Yes, we have a proposal right in the task's description:

If I understood correctly a (maybe temporary) solution might be to create a non-recording / invalid span in the HTTP instrumentation, which due to the ParentOrElse-Sampler, would lead to ignoring child spans as well, if the request matches a URL pattern. Any hint to where this would be architecturally appropriately implemented is highly appreciated. Maybe in the HttpServerTracer?

This should lead to ignoring the whole subtree starting from that SERVER span.

iNikem avatar Sep 24 '20 07:09 iNikem

+1 I think that is the right way to go https://github.com/open-telemetry/opentelemetry-specification/issues/173#issuecomment-698190021. It would also make sense to have a consolidated config property for this.

@iNikem what is the API to create non-recording span? In OT there was sampling.priority=bool tag that could be applied on the span builder https://github.com/opentracing/specification/blob/master/semantic_conventions.yaml#L26

pavolloffay avatar Sep 24 '20 08:09 pavolloffay

I think the right way is to use one of the factory methods on io.opentelemetry.trace.DefaultSpan.

iNikem avatar Sep 24 '20 08:09 iNikem

Yeah, I wanted to avoid having two paths of span creation.

pavolloffay avatar Sep 24 '20 08:09 pavolloffay

Sorry if double-spam, I thought I had already posted this. How about we have a special Sampler itself configured that delegates to the default, except for when the path matches the allow list, then it's ParentOnly? It means we need to refactor to make sure our tracers set attributes on Span.Builder instead of Span - a little annoying but we should have been doing that already so maybe good motivation for it.

anuraaga avatar Sep 24 '20 12:09 anuraaga

Note that DefaultSpan might get renamed to something that would be naming-wise not a good fit with what we want to do here https://github.com/open-telemetry/opentelemetry-specification/pull/994#pullrequestreview-495461768

anuraaga avatar Sep 24 '20 12:09 anuraaga

refactor to make sure our tracers set attributes on Span.Builder instead of Span

Yes, we want to do that eventually.

iNikem avatar Sep 24 '20 12:09 iNikem

Is there a way now to exclude health check traces? I checked processors but could not find any solution too.

cemo avatar Dec 20 '20 22:12 cemo

No, this functionality is not yet implemented.

iNikem avatar Dec 21 '20 06:12 iNikem

bump. Is there any workaround here in the meantime? Sampling of health checks is not ideal.

irl-segfault avatar Aug 04 '21 19:08 irl-segfault

bump. Is there any workaround here in the meantime? Sampling of health checks is not ideal.

The only known workaround is to write custom sampler.

But I have plans to address this issue during the next month or so.

iNikem avatar Aug 04 '21 20:08 iNikem

The corresponding Sampler has been implemented in the contrib repo. It can be added to your deployment using extension mechanism. There is no immediate plans to add that sampler into this distribution, as this requires changes in Otel Specification and that requires some effort.

iNikem avatar Oct 01 '21 16:10 iNikem

@iNikem what is the proposed way to configure these classes from environment? Is there any mechanism to bind jvm parameter to fields?

cemo avatar Nov 16 '21 09:11 cemo

@iNikem what is the proposed way to configure these classes from environment?

There is no such way. To use Sampler from the contrib repo you have to add it to your deployment via extension.

iNikem avatar Nov 16 '21 11:11 iNikem

@iNikem what is the proposed way to configure these classes from environment?

There is no such way. To use Sampler from the contrib repo you have to add it to your deployment via extension.

👍

@cemo The current configuration is limited to key/value pairs, which doesn't model the sampler rules well. In the future, we're hoping to have a richer configuration file: https://github.com/open-telemetry/opentelemetry-specification/issues/1773. Then we could add experimental configuration support for this. And eventually we're hoping for these sampler rules (or something like them) to be spec'd and supported cross-language.

trask avatar Nov 16 '21 18:11 trask

@iNikem @trask Could provide an example how to use the RuleBasedRoutingSampler exactly? What i did so far is:

  • Clone the repo opentelemetry-java-contrib
  • Build the jar (opentelemetry-contrib-samplers-1.8.0-SNAPSHOT.jar)
  • And tried to run it with
java -javaagent:path/to/opentelemetry-javaagent.jar \
    -Dotel.javaagent.extensions=opentelemetry-contrib-samplers-1.8.0-SNAPSHOT.jar
    -jar myapp.jar

But how do i pass the rules to the agent? In the DemoSampler https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/examples/extension/src/main/java/com/example/javaagent/DemoSampler.java it's more clear as the rule/condition is directly inside the class

gtuk avatar Dec 02 '21 08:12 gtuk

hi @gtuk! opentelemetry-contrib-samplers isn't published as an opentelemetry javaagent extension, so you'll need to build your own extension that consumes (and programmatically configures) the rules

trask avatar Dec 05 '21 20:12 trask

@trask The extensions are working out pretty neat 🎉 . Thanks for that. One question though, Sampler's shouldSampleonly works with the following data to set the sampling decision.

  1. parentContext
  2. traceId
  3. name
  4. spanKind
  5. attributes
  6. parentLinks If we want sampling decision to be based on either span's status or span's events, Is there any way to achieve it using Extension sampler?

RashmiRam avatar Dec 14 '21 20:12 RashmiRam

hey @RashmiRam! check out #3907, and there was recent discussion also about this: https://github.com/open-telemetry/opentelemetry-java/issues/3963#issuecomment-988460277

trask avatar Dec 15 '21 04:12 trask

Thanks @trask. That was really helpful. Yes. https://github.com/open-telemetry/opentelemetry-java/issues/3963#issuecomment-988460277 is also my exact use case. Wanted to achieve the same thing to push all errors irrespective of the sampling decision. Do you think it is a valid ask to have configuration in SDK to push all errors irrespective of sampling?

RashmiRam avatar Dec 15 '21 05:12 RashmiRam

Do you think it is a valid ask to have configuration in SDK to push all errors irrespective of sampling?

the problem is that when the sampler decides not to sample a span (which happens at the start of a span), the SDK doesn't capture any telemetry on that span. this makes unsampled spans very efficient, but it makes impossible to change your mind at the end of an unsampled span.

trask avatar Dec 15 '21 05:12 trask

Maybe there's a better place to ask for help, but I'm trying to resolve this problem with the extension workaround. When attempting to run the extension example here I get an error because I'm trying to build for Java8 as opposed to Java11 - is that a strict requirement for using extensions?

I tried to downgrade some gradle dependencies, but I'm not that familiar with Gradle myself (mostly use Maven). If there's a better place to ask questions or get help, please let me know!

welshm avatar Jan 17 '22 05:01 welshm

hi @welshm! try building with Java 11, it should still produce Java 8 compatible code

trask avatar Jan 18 '22 22:01 trask

hello, is there any news about this feature ?

xpicio avatar Jul 20 '22 12:07 xpicio

Any update on this ?

mnadeem avatar Aug 19 '22 14:08 mnadeem

Can anyone provide an example code in github link on how to exlude urls(exlude health check urls)

Montyroi avatar Sep 22 '22 05:09 Montyroi

Hey @Montyroi , This feature has not been implemented in the "core" javaagent yet. However, you can write a sampler yourself and package it in an extension. You can use the rule based sampler from the contrib repo that Nikita mentioned a couple posts earlier:

The corresponding Sampler has been implemented in the contrib repo. It can be added to your deployment using extension mechanism. There is no immediate plans to add that sampler into this distribution, as this requires changes in Otel Specification and that requires some effort.

Or implement one from scratch, e.g. https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/examples/extension/src/main/java/com/example/javaagent/DemoSampler.java

mateuszrzeszutek avatar Sep 22 '22 13:09 mateuszrzeszutek