diff --git a/lib/plug/request_id.ex b/lib/plug/request_id.ex index bc52cb15..a17bbc90 100644 --- a/lib/plug/request_id.ex +++ b/lib/plug/request_id.ex @@ -34,6 +34,12 @@ defmodule Plug.RequestId do plug Plug.RequestId, http_header: "custom-request-id" + * `:assign_as` - The name of the key that will be used to store the + discovered or generated request id in `conn.private`. If not provided, + the request id will not be stored. + + plug Plug.RequestId, assign_as: :plug_request_id + """ require Logger @@ -41,15 +47,16 @@ defmodule Plug.RequestId do @behaviour Plug @impl true - def init(opts) do - Keyword.get(opts, :http_header, "x-request-id") - end + def init(opts), do: opts @impl true - def call(conn, req_id_header) do + def call(conn, opts) do + header = Keyword.get(opts, :http_header, "x-request-id") + assign_as = Keyword.get(opts, :assign_as) + conn - |> get_request_id(req_id_header) - |> set_request_id(req_id_header) + |> get_request_id(header) + |> set_request_id(header, assign_as) end defp get_request_id(conn, header) do @@ -59,8 +66,11 @@ defmodule Plug.RequestId do end end - defp set_request_id({conn, request_id}, header) do + defp set_request_id({conn, request_id}, header, assign_as) do Logger.metadata(request_id: request_id) + + conn = if assign_as, do: Conn.put_private(conn, assign_as, request_id), else: conn + Conn.put_resp_header(conn, header, request_id) end diff --git a/test/plug/request_id_test.exs b/test/plug/request_id_test.exs index 54cc9510..ca236b63 100644 --- a/test/plug/request_id_test.exs +++ b/test/plug/request_id_test.exs @@ -65,6 +65,22 @@ defmodule Plug.RequestIdTest do assert res_request_id == meta_request_id end + test "assigns the request id to conn.private when given an assignment key" do + request_id = "existingidthatislongenough" + + conn = + conn(:get, "/") + |> put_req_header("x-request-id", request_id) + |> call(assign_as: :plug_request_id) + + [res_request_id] = get_resp_header(conn, "x-request-id") + meta_request_id = Logger.metadata()[:request_id] + assigned_request_id = conn.private.plug_request_id + assert assigned_request_id == request_id + assert res_request_id == assigned_request_id + assert res_request_id == meta_request_id + end + defp generated_request_id?(request_id) do Regex.match?(~r/\A[A-Za-z0-9-_]+\z/, request_id) end