elixir-plug / plug

Compose web applications with functions
https://hex.pm/packages/plug
Other
2.84k stars 582 forks source link

Plug.RequestId: put the `request_id` in `:private` or `:assigns`? #1171

Closed halostatue closed 11 months ago

halostatue commented 11 months ago

This may not be possible because it might collide with existing private or assigns assignments, but trying to use the :request_id right now is a bit of a pain:

  def sentry_request_id(conn) do
    if request_id = Keyword.get(Logger.metadata(), :request_id) do
      Sentry.Context.set_request_context(%{request_id: request_id})
    end

    conn
  end

This would be much easier if Plug.RequestId also wrote the retrieved or generated request_id in either :private or :assigns (I know that:private is the correct place). It need not be called request_id (plug_request_id seems unlikely to have been used by anyone), but it could also be a configurable option.

plug Plug.RequestId, store_as: :request_id

If it were set up that way, the current behaviour would hold unless store_as (someone please give me a better name) and would do Conn.put_private(store_as_key, request_id or something similar.

I’d be happy to create a PR for this, but I wanted to gauge the temperature and get feedback before developing this.

josevalim commented 11 months ago

A PR for assign_as is welcome!