Skip to content

Commit

Permalink
Keeps backwards compatibility for telemetry span exception event
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Nov 12, 2024
1 parent ae7e29a commit 545cc3e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 43 deletions.
23 changes: 10 additions & 13 deletions lib/bandit/telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ defmodule Bandit.Telemetry do
* `conn`: The `Plug.Conn` representing this connection. Not present in cases where `error`
is also set and the nature of error is such that Bandit was unable to successfully build
the conn
* `kind`: The kind of unexpected condition, typically `:error` or `:throw`
* `reason`: The error reason. An Exception struct or the thrown value
* `kind`: The kind of unexpected condition, typically `:exit`
* `exception`: The exception which caused this unexpected termination
* `stacktrace`: The stacktrace of the location which caused this unexpected termination
## `[:bandit, :websocket, *]`
Expand Down Expand Up @@ -189,25 +189,22 @@ defmodule Bandit.Telemetry do
end

@spec span_exception(t(), Exception.kind(), Exception.t() | term(), Exception.stacktrace()) :: :ok
def span_exception(span, kind, reason, stacktrace) do
def span_exception(span, :error, reason, stacktrace) when is_exception(reason) do
metadata =
Map.merge(span.start_metadata, %{
kind: kind,
reason: reason,
# Using :exit for backwards-compatiblity with Bandit =< 1.5.7
kind: :exit,
exception: reason,
stacktrace: stacktrace
})

# Keeps exception field for backwards compatibility
metadata =
if kind == :error and is_exception(reason) do
Map.put(metadata, :exception, reason)
else
metadata
end

span_event(span, :exception, %{}, metadata)
end

def span_exception(_span, _kind, _reason, _stacktrace) do
:ignore
end

@doc false
@spec span_event(t(), span_name(), :telemetry.event_measurements(), :telemetry.event_metadata()) ::
:ok
Expand Down
18 changes: 3 additions & 15 deletions test/bandit/http1/request_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2208,8 +2208,7 @@ defmodule HTTP1RequestTest do
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, []),
kind: :error,
reason: %RuntimeError{message: "boom"},
kind: :exit,
exception: %RuntimeError{message: "boom"},
stacktrace: list()
}}
Expand All @@ -2219,7 +2218,7 @@ defmodule HTTP1RequestTest do
assert output =~ "(RuntimeError) boom"
end

test "it should send `exception` events for throwing requests", context do
test "it should not send `exception` events for throwing requests", context do
output =
capture_log(fn ->
{:ok, collector_pid} =
Expand All @@ -2229,18 +2228,7 @@ defmodule HTTP1RequestTest do

Process.sleep(100)

assert Bandit.TelemetryCollector.get_events(collector_pid)
~> [
{[:bandit, :request, :exception], %{monotonic_time: integer()},
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, []),
kind: :throw,
reason: "thrown",
stacktrace: list()
}}
]
assert [] = Bandit.TelemetryCollector.get_events(collector_pid)
end)

assert output =~ "(throw) \"thrown\""
Expand Down
18 changes: 3 additions & 15 deletions test/bandit/http2/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,7 @@ defmodule HTTP2PlugTest do
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, []),
kind: :error,
reason: %RuntimeError{message: "boom"},
kind: :exit,
exception: %RuntimeError{message: "boom"},
stacktrace: list()
}}
Expand All @@ -1028,7 +1027,7 @@ defmodule HTTP2PlugTest do
raise "boom"
end

test "it should send `exception` events for throwing requests", context do
test "it should not send `exception` events for throwing requests", context do
output =
capture_log(fn ->
{:ok, collector_pid} =
Expand All @@ -1038,18 +1037,7 @@ defmodule HTTP2PlugTest do

Process.sleep(100)

assert Bandit.TelemetryCollector.get_events(collector_pid)
~> [
{[:bandit, :request, :exception], %{monotonic_time: integer()},
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, []),
kind: :throw,
reason: "thrown",
stacktrace: list()
}}
]
assert [] = Bandit.TelemetryCollector.get_events(collector_pid)
end)

assert output =~ "(throw) \"thrown\""
Expand Down

0 comments on commit 545cc3e

Please sign in to comment.