Skip to content

Commit

Permalink
Add telemetry metadata (#447)
Browse files Browse the repository at this point in the history
* Add plug to telemetry metadata

* Add websock to telemetry metadata
  • Loading branch information
mtrudel authored Jan 3, 2025
1 parent 80dd593 commit 667f3b6
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 23 deletions.
6 changes: 5 additions & 1 deletion lib/bandit/pipeline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ defmodule Bandit.Pipeline do
| {:error, term()}
def run(transport, plug, connection_span, opts) do
measurements = %{monotonic_time: Bandit.Telemetry.monotonic_time()}
metadata = %{connection_telemetry_span_context: connection_span.telemetry_span_context}

metadata = %{
connection_telemetry_span_context: connection_span.telemetry_span_context,
plug: plug
}

try do
{:ok, method, request_target, headers, transport} =
Expand Down
5 changes: 5 additions & 0 deletions lib/bandit/telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ 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
* `plug`: The Plug which is being used to serve this request. Specified as `{plug_module, plug_opts}`
This span is ended by the following event:
Expand Down Expand Up @@ -56,6 +57,7 @@ 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
* `plug`: The Plug which is being used to serve this request. Specified as `{plug_module, plug_opts}`
* `error`: The error that caused the span to end, if it ended in error
The following events may be emitted within this span:
Expand All @@ -76,6 +78,7 @@ 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
* `plug`: The Plug which is being used to serve this request. Specified as `{plug_module, plug_opts}`
* `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
Expand All @@ -100,6 +103,7 @@ defmodule Bandit.Telemetry do
* `telemetry_span_context`: A unique identifier for this span
* `connection_telemetry_span_context`: The span context of the Thousand Island `:connection`
span which contains this request
* `websock`: The WebSock which is being used to serve this request. Specified as `websock_module`
This span is ended by the following event:
Expand Down Expand Up @@ -143,6 +147,7 @@ defmodule Bandit.Telemetry do
this connection originated
* `connection_telemetry_span_context`: The span context of the Thousand Island `:connection`
span which contains this request
* `websock`: The WebSock which is being used to serve this request. Specified as `websock_module`
* `error`: The error that caused the span to end, if it ended in error
"""

Expand Down
3 changes: 2 additions & 1 deletion lib/bandit/websocket/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ defmodule Bandit.WebSocket.Connection do

span =
Bandit.Telemetry.start_span(:websocket, %{compress: compress}, %{
connection_telemetry_span_context: connection_telemetry_span_context
connection_telemetry_span_context: connection_telemetry_span_context,
websock: websock
})

instance = %__MODULE__{
Expand Down
27 changes: 19 additions & 8 deletions test/bandit/http1/request_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2116,7 +2116,8 @@ defmodule HTTP1RequestTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, [])
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []}
}}
]
end
Expand Down Expand Up @@ -2145,7 +2146,8 @@ defmodule HTTP1RequestTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, [])
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []}
}}
]
end
Expand Down Expand Up @@ -2176,7 +2178,8 @@ defmodule HTTP1RequestTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, [])
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []}
}}
]
end
Expand Down Expand Up @@ -2211,7 +2214,8 @@ defmodule HTTP1RequestTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, [])
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []}
}}
]
end
Expand Down Expand Up @@ -2242,7 +2246,8 @@ defmodule HTTP1RequestTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, [])
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []}
}}
]
end
Expand Down Expand Up @@ -2279,7 +2284,8 @@ defmodule HTTP1RequestTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, [])
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []}
}}
]
end
Expand All @@ -2306,7 +2312,8 @@ defmodule HTTP1RequestTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, [])
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []}
}}
]
end
Expand All @@ -2333,7 +2340,8 @@ defmodule HTTP1RequestTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, [])
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []}
}}
]
end
Expand All @@ -2352,6 +2360,7 @@ defmodule HTTP1RequestTest do
~> [
{[:bandit, :request, :stop], %{monotonic_time: integer(), duration: integer()},
%{
plug: {__MODULE__, []},
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
error: string()
Expand All @@ -2376,6 +2385,7 @@ defmodule HTTP1RequestTest do
~> [
{[:bandit, :request, :stop], %{monotonic_time: integer(), duration: integer()},
%{
plug: {__MODULE__, []},
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
error: "Header read timeout"
Expand Down Expand Up @@ -2403,6 +2413,7 @@ defmodule HTTP1RequestTest do
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []},
kind: :exit,
exception: %RuntimeError{message: "boom"},
stacktrace: list()
Expand Down
23 changes: 16 additions & 7 deletions test/bandit/http2/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,8 @@ defmodule HTTP2PlugTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, [])
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []}
}}
]
end
Expand Down Expand Up @@ -813,7 +814,8 @@ defmodule HTTP2PlugTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, [])
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []}
}}
]
end
Expand Down Expand Up @@ -842,7 +844,8 @@ defmodule HTTP2PlugTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, [])
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []}
}}
]
end
Expand Down Expand Up @@ -875,7 +878,8 @@ defmodule HTTP2PlugTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, [])
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []}
}}
]
end
Expand Down Expand Up @@ -910,7 +914,8 @@ defmodule HTTP2PlugTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, [])
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []}
}}
]
end
Expand All @@ -935,7 +940,8 @@ defmodule HTTP2PlugTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, [])
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []}
}}
]
end
Expand All @@ -960,7 +966,8 @@ defmodule HTTP2PlugTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, [])
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []}
}}
]
end
Expand All @@ -986,6 +993,7 @@ defmodule HTTP2PlugTest do
~> [
{[:bandit, :request, :stop], %{monotonic_time: integer(), duration: integer()},
%{
plug: {__MODULE__, []},
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
error: string()
Expand Down Expand Up @@ -1013,6 +1021,7 @@ defmodule HTTP2PlugTest do
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []},
kind: :exit,
exception: %RuntimeError{message: "boom"},
stacktrace: list()
Expand Down
17 changes: 12 additions & 5 deletions test/bandit/websocket/sock_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,8 @@ defmodule WebSocketWebSockTest do
%{monotonic_time: integer(), compress: maybe(boolean())},
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference()
telemetry_span_context: reference(),
websock: TelemetrySock
}}
]
end
Expand Down Expand Up @@ -1351,7 +1352,8 @@ defmodule WebSocketWebSockTest do
},
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference()
telemetry_span_context: reference(),
websock: TelemetrySock
}}
]
end
Expand All @@ -1376,7 +1378,8 @@ defmodule WebSocketWebSockTest do
},
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference()
telemetry_span_context: reference(),
websock: TelemetrySock
}}
]
end
Expand All @@ -1401,7 +1404,8 @@ defmodule WebSocketWebSockTest do
},
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference()
telemetry_span_context: reference(),
websock: TelemetrySock
}}
]
end
Expand All @@ -1420,7 +1424,8 @@ defmodule WebSocketWebSockTest do
{[:bandit, :websocket, :stop], %{monotonic_time: integer(), duration: integer()},
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference()
telemetry_span_context: reference(),
websock: TelemetrySock
}}
]
end
Expand Down Expand Up @@ -1448,6 +1453,7 @@ defmodule WebSocketWebSockTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
websock: TelemetrySock,
error: :nope
}}
]
Expand All @@ -1472,6 +1478,7 @@ defmodule WebSocketWebSockTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
websock: TelemetrySock,
error: :timeout
}}
]
Expand Down
3 changes: 2 additions & 1 deletion test/bandit/websocket/upgrade_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ defmodule WebSocketUpgradeTest do
%{
connection_telemetry_span_context: reference(),
telemetry_span_context: reference(),
conn: struct_like(Plug.Conn, [])
conn: struct_like(Plug.Conn, []),
plug: {__MODULE__, []}
}}
]
end
Expand Down

0 comments on commit 667f3b6

Please sign in to comment.