Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to stamp a "tenant id" attribute on all spans? #3456

Closed
trask opened this issue Aug 7, 2021 · 5 comments · Fixed by #3458
Closed

How to stamp a "tenant id" attribute on all spans? #3456

trask opened this issue Aug 7, 2021 · 5 comments · Fixed by #3458
Labels
Feature Request Suggest an idea for this project

Comments

@trask
Copy link
Member

trask commented Aug 7, 2021

Is your feature request related to a problem? Please describe.

I want to stamp a "tenant id" attribute on all spans.

Describe the solution you'd like

Open to suggestions.

One option that I think would work well is to build a SpanProcessor, and onStart(), copy the "tenant id" attribute from the parent span down to the new span.

Then the application can stamp the "tenant id" on the SERVER span in a servlet filter, and it will get propagated down to all spans, including auto-instrumentation spans (if you register the SpanProcessor via new javaagent extension mechanism).

Currently though this would require calling toSpanData() on the active parent span which is expensive, so if going this route I'd like to see if it's ok to add a getAttribute(AttributeKey) method to ReadableSpan.

Describe alternatives you've considered

Another option is to put the "tenant id" into the Context, and have a SpanProcessor that copies the "tenant id" into the new span.

The main problem with this approach is that it doesn't work great with the javaagent because the ContextKey can't easily be shared between the application that is putting the "tenant id" into the Context and the SpanProcessor that lives in the javaagent (extension).

A possible mitigation to this problem is to put the "tenant id" context key into the bootstrap class loader, which is possible now via extensions, but this starts to feel like a pretty complex solution to a simple-ish use case.

Yet another option is to put the "tenant id" into baggage, which would not have the javaagent problem that custom context keys have. But there are no controls on baggage propagation over the wire (except to disable it completely), which makes me hesitant to recommend this as a general solution to this use case.

Additional context

I believe this is similar use case to open-telemetry/opentelemetry-specification#1337

@trask trask added the Feature Request Suggest an idea for this project label Aug 7, 2021
@Oberon00
Copy link
Member

Oberon00 commented Aug 9, 2021

I want to stamp a "tenant id" attribute on all spans.

If you want to stamp the same ID on "all spans", you should use a resource attribute (you can even copy certain resource attributes back onto the spans in the exporter or OnEnd).

@jkwatson
Copy link
Contributor

jkwatson commented Aug 9, 2021

Is this really for "all spans"? Or is it just "all spans within a trace"?

IMO, this is what baggage is intended for. An enhanced baggage propagator could use metadata to decide if the entry should be propagated downstream. @trask Does this need to be a feature that is built-in to the core SDK, or could the microsoft distro have a custom baggage propagator and use baggage for this?

@anuraaga
Copy link
Contributor

@trask If the agent bridge is the main issue with using context, do you think this is a more general problem we have to solve in instrumentation API? Otherwise basically no instrumentation library is allowed to use context if they want to be used in the agent too.

@anuragagarwal561994
Copy link

@trask how did we solve this. I was trying this on my own.

So in SpanProcessor we get parentContext and the current ReadWriteSpan

If I want to get an attribute from the parent, correct me if I am wrong I will first have to do Span.fromContext(parentContext)

But this will give me a Span which doesn't have getAttribute as part of the interface. Instead ReadWriteSpan has so how does did we copy attributes from parent to child.

@trask
Copy link
Member Author

trask commented Sep 13, 2022

@anuragagarwal561994 you can check if Span is an instanceof ReadableSpan and then cast to that and read the attribute from the parent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request Suggest an idea for this project
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants