Skip to content

Commit

Permalink
feat: Uses telemetry span for plug.router_dispatch (elixir-plug#1037)
Browse files Browse the repository at this point in the history
Co-authored-by: Isaac Sanders <isanders@drwholdings.com>
  • Loading branch information
isaacsanders and isaacsanders authored Jun 19, 2021
1 parent bc811b4 commit 8d86122
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 29 deletions.
38 changes: 11 additions & 27 deletions lib/plug/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,15 @@ defmodule Plug.Router do
* `[:plug, :router_dispatch, :start]` - dispatched before dispatching to a matched route
* Measurement: `%{system_time: System.system_time}`
* Metadata: `%{conn: Plug.Conn.t, route: binary, router: module}`
* Metadata: `%{telemetry_span_context: term(), conn: Plug.Conn.t, route: binary, router: module}`
* `[:plug, :router_dispatch, :exception]` - dispatched after exceptions on dispatching a route
* Measurement: `%{duration: native_time}`
* Metadata: `%{conn: Plug.Conn.t, route: binary, router: module}`
* Metadata: `%{telemetry_span_context: term(), conn: Plug.Conn.t, route: binary, router: module, kind: :throw | :error | :exit, reason: term(), stacktrace: list()}`
* `[:plug, :router_dispatch, :stop]` - dispatched after successfully dispatching a matched route
* Measurement: `%{duration: native_time}`
* Metadata: `%{conn: Plug.Conn.t, route: binary, router: module}`
* Metadata: `%{telemetry_span_context: term(), conn: Plug.Conn.t, route: binary, router: module}`
"""

Expand All @@ -270,35 +270,19 @@ defmodule Plug.Router do

@doc false
def dispatch(%Plug.Conn{} = conn, opts) do
start = System.monotonic_time()
{path, fun} = Map.fetch!(conn.private, :plug_route)
metadata = %{conn: conn, route: path, router: __MODULE__}

:telemetry.execute(
[:plug, :router_dispatch, :start],
%{system_time: System.system_time()},
metadata
)

try do
fun.(conn, opts)
else
conn ->
duration = System.monotonic_time() - start
metadata = %{metadata | conn: conn}
:telemetry.execute([:plug, :router_dispatch, :stop], %{duration: duration}, metadata)
conn
:telemetry.span(
[:plug, :router_dispatch],
%{conn: conn, route: path, router: __MODULE__},
fn ->
conn = fun.(conn, opts)
{conn, %{conn: conn, route: path, router: __MODULE__}}
end
)
catch
kind, reason ->
duration = System.monotonic_time() - start
metadata = %{kind: kind, reason: reason, stacktrace: __STACKTRACE__}

:telemetry.execute(
[:plug, :router_dispatch, :exception],
%{duration: duration},
metadata
)

Plug.Conn.WrapperError.reraise(conn, kind, reason, __STACKTRACE__)
end
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule Plug.MixProject do
[
{:mime, "~> 1.0"},
{:plug_crypto, "~> 1.1.1 or ~> 1.2"},
{:telemetry, "~> 0.4"},
{:telemetry, ">= 0.4.3 and < 1.0.0"},
{:ex_doc, "~> 0.21", only: :docs}
]
end
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"mime": {:hex, :mime, "1.6.0", "dabde576a497cef4bbdd60aceee8160e02a6c89250d6c0b29e56c0dfb00db3d2", [:mix], [], "hexpm", "31a1a8613f8321143dde1dafc36006a17d28d02bdfecb9e95a880fa7aabd19a7"},
"nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"},
"plug_crypto": {:hex, :plug_crypto, "1.2.1", "5c854427528bf61d159855cedddffc0625e2228b5f30eff76d5a4de42d896ef4", [:mix], [], "hexpm", "6961c0e17febd9d0bfa89632d391d2545d2e0eb73768f5f50305a23961d8782c"},
"telemetry": {:hex, :telemetry, "0.4.0", "8339bee3fa8b91cb84d14c2935f8ecf399ccd87301ad6da6b71c09553834b2ab", [:rebar3], [], "hexpm", "e9e3cacfd37c1531c0ca70ca7c0c30ce2dbb02998a4f7719de180fe63f8d41e4"},
"telemetry": {:hex, :telemetry, "0.4.3", "a06428a514bdbc63293cd9a6263aad00ddeb66f608163bdec7c8995784080818", [:rebar3], [], "hexpm", "eb72b8365ffda5bed68a620d1da88525e326cb82a75ee61354fc24b844768041"},
}

0 comments on commit 8d86122

Please sign in to comment.