opentelemetry-java-instrumentation
opentelemetry-java-instrumentation copied to clipboard
Exclude URLs from Tracing
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
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 :)
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.
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.
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.
+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
I think the right way is to use one of the factory methods on io.opentelemetry.trace.DefaultSpan.
Yeah, I wanted to avoid having two paths of span creation.
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.
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
refactor to make sure our tracers set attributes on Span.Builder instead of Span
Yes, we want to do that eventually.
Is there a way now to exclude health check traces? I checked processors but could not find any solution too.
No, this functionality is not yet implemented.
bump. Is there any workaround here in the meantime? Sampling of health checks is not ideal.
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.
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 what is the proposed way to configure these classes from environment? Is there any mechanism to bind jvm parameter to fields?
@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 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.
@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
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 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.
- parentContext
- traceId
- name
- spanKind
- attributes
- 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?
hey @RashmiRam! check out #3907, and there was recent discussion also about this: https://github.com/open-telemetry/opentelemetry-java/issues/3963#issuecomment-988460277
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?
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.
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!
hi @welshm! try building with Java 11, it should still produce Java 8 compatible code
hello, is there any news about this feature ?
Any update on this ?
Can anyone provide an example code in github link on how to exlude urls(exlude health check urls)
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