From 667f3b6b901fada055b768520347ad8873299167 Mon Sep 17 00:00:00 2001 From: Mat Trudel Date: Thu, 2 Jan 2025 19:48:55 -0500 Subject: [PATCH] Add telemetry metadata (#447) * Add plug to telemetry metadata * Add websock to telemetry metadata --- lib/bandit/pipeline.ex | 6 +++++- lib/bandit/telemetry.ex | 5 +++++ lib/bandit/websocket/connection.ex | 3 ++- test/bandit/http1/request_test.exs | 27 ++++++++++++++++++-------- test/bandit/http2/plug_test.exs | 23 +++++++++++++++------- test/bandit/websocket/sock_test.exs | 17 +++++++++++----- test/bandit/websocket/upgrade_test.exs | 3 ++- 7 files changed, 61 insertions(+), 23 deletions(-) diff --git a/lib/bandit/pipeline.ex b/lib/bandit/pipeline.ex index 0bc51f87..139f02f3 100644 --- a/lib/bandit/pipeline.ex +++ b/lib/bandit/pipeline.ex @@ -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} = diff --git a/lib/bandit/telemetry.ex b/lib/bandit/telemetry.ex index 51e9e8aa..fcb6fced 100644 --- a/lib/bandit/telemetry.ex +++ b/lib/bandit/telemetry.ex @@ -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: @@ -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: @@ -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 @@ -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: @@ -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 """ diff --git a/lib/bandit/websocket/connection.ex b/lib/bandit/websocket/connection.ex index 3aeafe49..3b8ca14c 100644 --- a/lib/bandit/websocket/connection.ex +++ b/lib/bandit/websocket/connection.ex @@ -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__{ diff --git a/test/bandit/http1/request_test.exs b/test/bandit/http1/request_test.exs index e8268582..200a1650 100644 --- a/test/bandit/http1/request_test.exs +++ b/test/bandit/http1/request_test.exs @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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() @@ -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" @@ -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() diff --git a/test/bandit/http2/plug_test.exs b/test/bandit/http2/plug_test.exs index 13cd525d..872a132b 100644 --- a/test/bandit/http2/plug_test.exs +++ b/test/bandit/http2/plug_test.exs @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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() @@ -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() diff --git a/test/bandit/websocket/sock_test.exs b/test/bandit/websocket/sock_test.exs index 55071bb2..d1d11dfe 100644 --- a/test/bandit/websocket/sock_test.exs +++ b/test/bandit/websocket/sock_test.exs @@ -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 @@ -1351,7 +1352,8 @@ defmodule WebSocketWebSockTest do }, %{ connection_telemetry_span_context: reference(), - telemetry_span_context: reference() + telemetry_span_context: reference(), + websock: TelemetrySock }} ] end @@ -1376,7 +1378,8 @@ defmodule WebSocketWebSockTest do }, %{ connection_telemetry_span_context: reference(), - telemetry_span_context: reference() + telemetry_span_context: reference(), + websock: TelemetrySock }} ] end @@ -1401,7 +1404,8 @@ defmodule WebSocketWebSockTest do }, %{ connection_telemetry_span_context: reference(), - telemetry_span_context: reference() + telemetry_span_context: reference(), + websock: TelemetrySock }} ] end @@ -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 @@ -1448,6 +1453,7 @@ defmodule WebSocketWebSockTest do %{ connection_telemetry_span_context: reference(), telemetry_span_context: reference(), + websock: TelemetrySock, error: :nope }} ] @@ -1472,6 +1478,7 @@ defmodule WebSocketWebSockTest do %{ connection_telemetry_span_context: reference(), telemetry_span_context: reference(), + websock: TelemetrySock, error: :timeout }} ] diff --git a/test/bandit/websocket/upgrade_test.exs b/test/bandit/websocket/upgrade_test.exs index 9937a3a7..77ce56a4 100644 --- a/test/bandit/websocket/upgrade_test.exs +++ b/test/bandit/websocket/upgrade_test.exs @@ -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