[Feature]: Override Resource for individual spans
Related Problems?
I am implementing opentelemetry integration for tracexec, which is an exec tracing tool for Linux.
So I am not directly using opentelemetry to instrument tracexec but instead tracexec produces spans on behalf of the ptrace/eBPF-instrumented processes, where each program(as identified by its path) should have its own resource.
Currently the Resource is set at the provider level. This means that I would need to manage a provider and tracer for each program(where there can easily be thousands of programs at the same time), which is cumbersome and resource-heavy. (each provider is going to maintain its own connection to the collector unless I implement a custom provider to share a global underlying connection :/)
Describe the solution you'd like:
It would be convenient to have a way to override the Resource when creating the span or have a way to modify it in a post-process pipeline like SpanProcessor. So that it would be easy to achieve this using only a single provider:
Considered Alternatives
I have implemented a hack in my fork of opentelemetry-proto: https://github.com/open-telemetry/opentelemetry-rust/commit/01822c88f6d217cf673c3bc35c84bf552f552c82
tracexec is published on crates.io, which means that all of its dependencies need to be on crates.io. Cargo currently does not allow [patch]ing dependency from the same source, which means that I cannot simply fork opentelemetry-proto and publish it to crates.io.
So another unfortunate alternative is to fork opentelemetry-proto and anything that depends on opentelemetry-proto(currently there is only opentelemetry-otlp in my dependency tree) and publish all of them to crates.io.
Additional Context
No response
Hey @kxxt sorry for the slow reply here!
The resource isn't intended to be modified in the middle of span processing like this - the spec is explicit:
Resources are immutable. Thus, in addition to resource creation, only the following operations should be provided ... ( retrieve attributes )
When used with distributed tracing, a resource can be associated with the TracerProvider when the TracerProvider is created. That association cannot be changed later. When associated with a TracerProvider, all Spans produced by any Tracer from the provider MUST be associated with this Resource.
Having said that, there's a similar request over in https://github.com/open-telemetry/opentelemetry-java/issues/5395 that suggests this sort of behaviour would require a spec change, and that change is tracked here https://github.com/open-telemetry/opentelemetry-specification/issues/3433. You might want to go and add weight to that spec request with your use case!
In the meantime, perhaps you could use span attributes, and potentially use the collector's ability to transform telemetry as it flows through to line things up how you want them?