Skip to content

Commit

Permalink
includes exception request data if available
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Sep 4, 2024
1 parent fad71cb commit cd58a86
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
13 changes: 12 additions & 1 deletion lib/tower_sentry/reporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ defmodule TowerSentry.Reporter do
reason: exception,
stacktrace: stacktrace,
id: id,
metadata: metadata
metadata: metadata,
plug_conn: plug_conn
}) do
if enabled?() do
# TODO: Include plug conn data if available
Sentry.capture_exception(
exception,
stacktrace: stacktrace,
request: request_data(plug_conn),
extra: %{id: id, metadata: metadata}
)
else
Expand Down Expand Up @@ -41,6 +43,15 @@ defmodule TowerSentry.Reporter do
end
end

defp request_data(%Plug.Conn{} = conn) do
%{
method: conn.method,
url: "#{conn.scheme}://#{conn.host}:#{conn.port}#{conn.request_path}"
}
end

defp request_data(_), do: %{}

defp enabled? do
Sentry.Config.dsn()
# Application.get_env(:sentry, :dsn)
Expand Down
7 changes: 6 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ defmodule TowerSentry.MixProject do
app: :tower_sentry,
version: "0.1.0",
elixir: "~> 1.16",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]

# Run "mix help compile.app" to learn about applications.
def application do
[
Expand All @@ -27,7 +31,8 @@ defmodule TowerSentry.MixProject do
{:hackney, "~> 1.20", optional: true},

# Test
{:bypass, "~> 2.1", only: :test}
{:bypass, "~> 2.1", only: :test},
{:plug_cowboy, "~> 2.7", only: :test}
]
end
end
16 changes: 16 additions & 0 deletions test/support/error_test_plug.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule TowerSentry.ErrorTestPlug do
use Plug.Router

plug(:match)
plug(:dispatch)

get "/arithmetic-error" do
1 / 0

send_resp(conn, 200, "OK")
end

match _ do
send_resp(conn, 404, "Not Found")
end
end
63 changes: 63 additions & 0 deletions test/tower_sentry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,69 @@ defmodule TowerSentryTest do
assert_receive({^ref, :sent}, 500)
end

test "reports arithmetic error when a Plug.Conn IS present with Plug.Cowboy", %{bypass: bypass} do
# ref message synchronization trick copied from
# https://github.com/PSPDFKit-labs/bypass/issues/112
parent = self()
ref = make_ref()
# An ephemeral port hopefully not being in the host running this code
plug_port = 51111
url = "http://127.0.0.1:#{plug_port}/arithmetic-error"

Bypass.expect_once(bypass, "POST", "/api/1/envelope", fn conn ->
{:ok, body, conn} = Plug.Conn.read_body(conn)

assert [_id, _header, event] = String.split(body, "\n", trim: true)

assert(
{
:ok,
%{
"level" => "error",
"environment" => "test",
"exception" => [exception],
"request" => %{
"method" => "GET",
"url" => ^url
}
}
} = Jason.decode(event)# |> IO.inspect()
)

assert(
%{
"type" => "ArithmeticError",
"value" => "bad argument in arithmetic expression",
"stacktrace" => %{"frames" => frames}
} = exception
)

assert(
%{
"function" => "anonymous fn/2 in TowerSentry.ErrorTestPlug.do_match/4",
"filename" => "test/support/error_test_plug.ex",
"lineno" => 8
} = List.last(frames)
)

send(parent, {ref, :sent})

conn
|> Plug.Conn.put_resp_content_type("application/json")
|> Plug.Conn.resp(200, Jason.encode!(%{"ok" => true}))
end)

start_supervised!(
{Plug.Cowboy, plug: TowerSentry.ErrorTestPlug, scheme: :http, port: plug_port}
)

capture_log(fn ->
{:ok, _response} = :httpc.request(:get, {url, [{~c"user-agent", "httpc client"}]}, [], [])
end)

assert_receive({^ref, :sent}, 500)
end

test "reports message", %{bypass: bypass} do
# ref message synchronization trick copied from
# https://github.com/PSPDFKit-labs/bypass/issues/112
Expand Down

0 comments on commit cd58a86

Please sign in to comment.