OpenTelemetry: set user context

I'm looking for guidance on how to have the user context populated in APM when sending traces through OpenTelemetry (Javascript @opentelemetry/api package).

Setting a span attribute of user.name results in the infamous labels.user_name.

From Elastic Observability to APM

Added nodejs

Added open-telemetry

Hi @skistar002

in order to understand better your context I need to know which is the way you're instrumenting your app:

  1. Using OpenTelemetry API and Node.js SDK
  2. Using OpenTelemetry API and Elastic's Node.js Agent OpenTelemetry bridge

With option 2 I think you can setup the context using setUserContext API. With option 1 I'm not sure this is possible so I'm going to ask the APM server team about it

Cheers,
David

Hi David,

I am using option 1.

For more context, my Javascript application is using the Next.js framework. I believe I ran into bundling problems when using Next.js's App Router and Elastic's Node.js Agent. There are callouts for APM limitations with Next.js on APM's documentation: Get stared with Next.js. The App Router is in versions higher than APM's supported Next.js versions of >=12.0.0 and <13.3.0. This made me explore using OpenTelemetry's SDK and not using Elastic's APM agent.

Thanks,
Tyler

Edit: Following up on this with a workaround I learned, which others might find useful. It's possible with option 1 that setting the user context isn't possible at the moment, so a span attribute of user.name turns into labels.user_name when viewing the transaction/span in the APM UI. I learned about Elasticsearch Ingest Pipelines which can be used to transform the incoming data using processors.

Hi Tyler,

I'm waiting a response from the APM server team but it's likely that you're right and the workaround provided is the best option for now. Elastic is doing an effort to add support to OTEL data format and to adapt Kibana UI to use the right data schema depending on how you instrument your service.

So setting user attributes in the span is correct and will be supported in the future.

Cheers,
David