-
Hey folks, I've integrated opentelemetry for our system (it works great!). I'm now attempting to add some extra attributes to make it easier to debug issues on different axis. Context here is we have a web application with a GraphQL backend with some authorization in front of it. I've been trying to find a way to set userId as an attribute for all child spans once I've made it through the authorization layer. Conceptually this feels like setting a default attribute for all spans post authorization, but I haven't been able to find a way to do that with today's API. Is there a known good way of doing that? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The easiest way would be to use the baggage api to store those metadata, then having this span processor registered on your backend sdk: import {
propagation,
context
} from '@opentelemetry/api'
import { BatchSpanProcessor, Span } from '@opentelemetry/sdk-trace-base'
export class CustomBatchSpanProcessor extends BatchSpanProcessor {
onStart (span: Span) {
const baggage = propagation.getBaggage(context.active())
if (baggage === undefined) return
span.setAttributes(baggage.getAllEntries().reduce((agg, entry) => {
agg[entry[0]] = entry[1].value
return agg
}, {} as Record<string, string>))
}
} |
Beta Was this translation helpful? Give feedback.
The easiest way would be to use the baggage api to store those metadata, then having this span processor registered on your backend sdk: