Skip to content

Commit e0c97cc

Browse files
Merge pull request #81 from getsentry/plug-id
Allow sending request identifiers to Sentry with Phoenix/Plug.RequestId supported by default
2 parents bd066d0 + 9970a34 commit e0c97cc

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

lib/sentry/plug.ex

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,40 @@ defmodule Sentry.Plug do
3333
3434
use Sentry.Plug, scrubber: {MyModule, :scrub_params}
3535
36-
*Please Note*: If your are sending large files you will want to scrub them out.
36+
*Please Note*: If you are sending large files you will want to scrub them out.
3737
3838
### Headers Scrubber
3939
4040
By default we will scrub Authorization and Authentication headers from all requests before sending them.
4141
42+
### Including Request Identifiers
43+
44+
If you're using Phoenix, Plug.RequestId, or another method to set a request ID response header, and would like to include that information with errors reported by Sentry.Plug, the `:request_id_header` option allows you to set which header key Sentry should check. It will default to "x-request-id", which Plug.RequestId (and therefore Phoenix) also default to.
45+
46+
use Sentry.Plug, request_id_header: "application-request-id"
4247
"""
4348

49+
@default_plug_request_id_header "x-request-id"
4450

4551

4652
defmacro __using__(env) do
4753
scrubber = Keyword.get(env, :scrubber, nil)
54+
request_id_header = Keyword.get(env, :request_id_header, nil)
4855

4956
quote do
5057
defp handle_errors(conn, %{kind: kind, reason: reason, stack: stack}) do
51-
request = Sentry.Plug.build_request_interface_data(conn, unquote(scrubber))
58+
opts = [scrubber: unquote(scrubber), request_id_header: unquote(request_id_header)]
59+
request = Sentry.Plug.build_request_interface_data(conn, opts)
5260
exception = Exception.normalize(kind, reason, stack)
5361
Sentry.capture_exception(exception, [stacktrace: stack, request: request])
5462
end
5563
end
5664
end
5765

58-
def build_request_interface_data(%Plug.Conn{} = conn, scrubber) do
66+
def build_request_interface_data(%Plug.Conn{} = conn, opts) do
67+
scrubber = Keyword.get(opts, :scrubber)
68+
request_id = Keyword.get(opts, :request_id_header) || @default_plug_request_id_header
69+
5970
conn = conn
6071
|> Plug.Conn.fetch_cookies
6172
|> Plug.Conn.fetch_query_params
@@ -72,6 +83,7 @@ defmodule Sentry.Plug do
7283
"REMOTE_PORT" => remote_port(conn.peer),
7384
"SERVER_NAME" => conn.host,
7485
"SERVER_PORT" => conn.port,
86+
"REQUEST_ID" => Plug.Conn.get_resp_header(conn, request_id) |> List.first,
7587
}
7688
}
7789
end

test/plug_test.exs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ defmodule Sentry.PlugTest do
55
defmodule ExampleApp do
66
use Plug.Router
77
use Plug.ErrorHandler
8-
use Sentry.Plug
8+
use Sentry.Plug, request_id_header: "x-request-id"
99

1010

1111
plug Plug.Parsers, parsers: [:multipart]
12+
plug Plug.RequestId
1213
plug :match
1314
plug :dispatch
1415

@@ -49,7 +50,7 @@ defmodule Sentry.PlugTest do
4950
|> put_req_cookie("cookie_key", "cookie_value")
5051
|> put_req_header("accept-language", "en-US")
5152

52-
request_data = Sentry.Plug.build_request_interface_data(conn, nil)
53+
request_data = Sentry.Plug.build_request_interface_data(conn, [])
5354

5455
assert request_data[:url] =~ ~r/\/error_route$/
5556
assert request_data[:method] == "GET"
@@ -81,10 +82,18 @@ defmodule Sentry.PlugTest do
8182
|> Enum.into(%{})
8283
end
8384

84-
request_data = Sentry.Plug.build_request_interface_data(conn, scrubber)
85+
request_data = Sentry.Plug.build_request_interface_data(conn, scrubber: scrubber)
8586
assert request_data[:method] == "POST"
8687
assert request_data[:data] == %{"hello" => "world"}
8788
assert request_data[:headers] == %{"cookie" => "cookie_key=cookie_value", "accept-language" => "en-US", "content-type" => "multipart/mixed; charset: utf-8"}
8889
assert request_data[:cookies] == %{"cookie_key" => "cookie_value"}
8990
end
91+
92+
test "gets request_id" do
93+
conn = conn(:get, "/error_route")
94+
|> Plug.Conn.put_resp_header("x-request-id", "my_request_id")
95+
96+
request_data = Sentry.Plug.build_request_interface_data(conn, [request_id_header: "x-request-id"])
97+
assert request_data[:env]["REQUEST_ID"] == "my_request_id"
98+
end
9099
end

0 commit comments

Comments
 (0)