Skip to content

Set default attribute for child spans (adding userId attribute post authorization) #2622

Answered by vmarchaud
aitskovi asked this question in Q&A
Discussion options

You must be logged in to vote

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>))
  }
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by vmarchaud
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants