Skip to content

Commit

Permalink
feat: reports safelisted Logger.metadata when Cowboy
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Nov 27, 2024
1 parent dd74d12 commit 2d2f8c1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
8 changes: 7 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ defmodule Tower.MixProject do

# Test
{:assert_eventually, "~> 1.0", only: :test},
{:plug_cowboy, "~> 2.7", only: :test},
{
:plug_cowboy,
github: "mimiquate/plug_cowboy",
branch: "logger_metadata",
override: true,
only: :test
},
{:phoenix, "~> 1.7", only: :test},
{:phoenix_html, "~> 4.1", only: :test},
{:oban, "~> 2.18", only: :test},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.3", "3168d78ba41835aecad272d5e8cd51aa87a7ac9eb836eabc42f6e57538e3731d", [:mix], [], "hexpm", "bba06bc1dcfd8cb086759f0edc94a8ba2bc8896d5331a1e2c2902bf8e36ee502"},
"phoenix_template": {:hex, :phoenix_template, "1.0.4", "e2092c132f3b5e5b2d49c96695342eb36d0ed514c5b252a77048d5969330d639", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "2c0c81f0e5c6753faf5cca2f229c9709919aba34fab866d3bc05060c9c444206"},
"plug": {:hex, :plug, "1.16.1", "40c74619c12f82736d2214557dedec2e9762029b2438d6d175c5074c933edc9d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a13ff6b9006b03d7e33874945b2755253841b238c34071ed85b0e86057f8cddc"},
"plug_cowboy": {:hex, :plug_cowboy, "2.7.2", "fdadb973799ae691bf9ecad99125b16625b1c6039999da5fe544d99218e662e4", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "245d8a11ee2306094840c000e8816f0cbed69a23fc0ac2bcf8d7835ae019bb2f"},
"plug_cowboy": {:git, "https://github.com/mimiquate/plug_cowboy.git", "baa0769ef328f85455cbc959c6a43a676989d1b3", [branch: "logger_metadata"]},
"plug_crypto": {:hex, :plug_crypto, "2.1.0", "f44309c2b06d249c27c8d3f65cfe08158ade08418cf540fd4f72d4d6863abb7b", [:mix], [], "hexpm", "131216a4b030b8f8ce0f26038bc4421ae60e4bb95c5cf5395e1421437824c4fa"},
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},
"telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"},
Expand Down
12 changes: 12 additions & 0 deletions test/tower/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ defmodule TowerPlugTest do
end

test "reports runtime error during plug dispatch with Plug.Cowboy" do
put_env(:logger_metadata, [:user_id])

# An ephemeral port hopefully not being in the host running this code
plug_port = 51111
url = "http://127.0.0.1:#{plug_port}/runtime-error"
Expand All @@ -31,6 +33,7 @@ defmodule TowerPlugTest do
kind: :error,
reason: %RuntimeError{message: "an error"},
stacktrace: stacktrace,
metadata: metadata,
plug_conn: %Plug.Conn{} = plug_conn,
by: Tower.LoggerHandler
}
Expand All @@ -40,10 +43,13 @@ defmodule TowerPlugTest do
assert String.length(id) == 36
assert recent_datetime?(datetime)
assert [_ | _] = stacktrace
assert metadata == %{user_id: 123}
assert Plug.Conn.request_url(plug_conn) == url
end

test "reports uncaught throw during plug dispatch with Plug.Cowboy" do
put_env(:logger_metadata, [:user_id])

# An ephemeral port hopefully not being in the host running this code
plug_port = 51111
url = "http://127.0.0.1:#{plug_port}/uncaught-throw"
Expand All @@ -63,6 +69,7 @@ defmodule TowerPlugTest do
kind: :throw,
reason: "something",
stacktrace: stacktrace,
metadata: metadata,
plug_conn: %Plug.Conn{} = plug_conn,
by: Tower.LoggerHandler
}
Expand All @@ -72,10 +79,13 @@ defmodule TowerPlugTest do
assert String.length(id) == 36
assert recent_datetime?(datetime)
assert [_ | _] = stacktrace
assert metadata == %{user_id: 123}
assert Plug.Conn.request_url(plug_conn) == url
end

test "reports abnormal exit during plug dispatch with Plug.Cowboy" do
put_env(:logger_metadata, [:user_id])

# An ephemeral port hopefully not being in the host running this code
plug_port = 51111
url = "http://127.0.0.1:#{plug_port}/abnormal-exit"
Expand All @@ -95,6 +105,7 @@ defmodule TowerPlugTest do
kind: :exit,
reason: :abnormal,
stacktrace: stacktrace,
metadata: metadata,
plug_conn: %Plug.Conn{} = plug_conn,
by: Tower.LoggerHandler
}
Expand All @@ -105,6 +116,7 @@ defmodule TowerPlugTest do
assert recent_datetime?(datetime)
# Plug.Cowboy doesn't provide stacktrace for exits
assert [] = stacktrace
assert metadata == %{user_id: 123}
assert Plug.Conn.request_url(plug_conn) == url
end

Expand Down

0 comments on commit 2d2f8c1

Please sign in to comment.