Something like an application environment that changes during execution?

Hello,

My application is serving HTTP requests with Plug. I generate for each request an identifier. I would like all of my modules to use this identifier, for instance in the logs messages.

So in an imperative world, it would look like a global variable that would be updated for each request. How would you do this in Elixir ?

That’s what logger metadata is for. But also Plug.RequestId does already do that. Any reason not to use that one?

1 Like

You can use the request ID and put it as an attribute in your OpenTelemetry spans so not a problem. You can also pass it around if your request spawns background tasks or other corollary activities and not lose its track. It’s being done regularly, what is your difficulty in particular?

Thank you for your ideas.
I ended up using Plug.RequestId and use Logger.metadata[:request_id] to where needed.