diff --git a/lib/opentelemetry_plug.ex b/lib/opentelemetry_plug.ex index cf70547..fae3137 100644 --- a/lib/opentelemetry_plug.ex +++ b/lib/opentelemetry_plug.ex @@ -12,7 +12,7 @@ defmodule OpentelemetryPlug do Example: - OpentelemetryPlug.setup([:my, :plug]) + OpentelemetryPlug.setup([]) You may also supply the following options in the second argument: @@ -23,38 +23,44 @@ defmodule OpentelemetryPlug do defaults to the concatenation of the event name with periods, e.g. `"my.plug.start"`. """ - def setup(event_prefix, config \\ []) do + def setup(config \\ []) do # register the tracer. just re-registers if called for multiple repos _ = OpenTelemetry.register_application_tracer(:opentelemetry_plug) + :telemetry.attach({__MODULE__, {event_prefix, :opentelemetry_phoenix_tracer_router_start}}, + [:phoenix, :router_dispatch, :start], + &__MODULE__.handle_route/4, + config) + + :telemetry.attach({__MODULE__, {event_prefix, :opentelemetry_plug_tracer_router_start}}, + [:plug, :router_dispatch, :start], + &__MODULE__.handle_route/4, + config) + :telemetry.attach({__MODULE__, {event_prefix, :opentelemetry_plug_tracer_start}}, - event_prefix ++ [:start], + [:plug_adapter, :call, :start], &__MODULE__.handle_start/4, config) :telemetry.attach({__MODULE__, {event_prefix, :opentelemetry_plug_tracer_stop}}, - event_prefix ++ [:stop], + [:plug_adapter, :call, :stop], &__MODULE__.handle_stop/4, config) :telemetry.attach({__MODULE__, {event_prefix, :opentelemetry_plug_tracer_exception}}, - event_prefix ++ [:exception], + [:plug_adapter, :call, :exception], &__MODULE__.handle_exception/4, config) end @doc false def handle_start(_, _measurements, %{conn: conn}, _config) do + # TODO: add config for what paths are traced + # setup OpenTelemetry context based on request headers :ot_propagation.http_extract(conn.req_headers) - # TODO: add config option to allow `conn.request_path` as span name - span_name = case Map.get(conn.private, :plug_route) do - nil -> - "HTTP " <> conn.method - {route, _fun} -> - route - end + span_name = "HTTP " <> conn.method {_, adapter} = conn.adapter user_agent = header_or_empty(conn, "User-Agent") @@ -79,6 +85,12 @@ defmodule OpentelemetryPlug do OpenTelemetry.Tracer.start_span(span_name, %{attributes: attributes}) end + @doc false + def handle_route(_, _measurements, %{route: route}, _config) do + # TODO: add config option to allow `conn.request_path` as span name + OpenTelemetry.Span.update_name(route) + end + @doc false def handle_stop(_, _measurements, %{conn: conn}, _config) do OpenTelemetry.Span.set_attribute("http.status", conn.status) diff --git a/mix.exs b/mix.exs index 7b9d7eb..fdbe9f3 100644 --- a/mix.exs +++ b/mix.exs @@ -21,7 +21,7 @@ defmodule OpentelemetryPlug.MixProject do # Run "mix help deps" to learn about dependencies. defp deps do [ - {:plug, "~> 1.10"}, + {:plug, github: "elixir-plug/plug", branch: "master"}, {:opentelemetry_api, "~> 0.3"}, {:opentelemetry, "~> 0.3"}, {:telemetry, "~> 0.4"} diff --git a/mix.lock b/mix.lock index b9b1713..a70bee3 100644 --- a/mix.lock +++ b/mix.lock @@ -2,7 +2,7 @@ "mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm", "6cbe761d6a0ca5a31a0931bf4c63204bceb64538e664a8ecf784a9a6f3b875f1"}, "opentelemetry": {:hex, :opentelemetry, "0.4.0", "293729f014009b03a1a2c47e6367db6f280b41412faa5639f06dcce9733d18a6", [:rebar3], [{:opentelemetry_api, "~> 0.3.1", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "f0eb4281f26879147322b0a6f1c457c57f3f1c4121cbff1f6056e4c98b1647a7"}, "opentelemetry_api": {:hex, :opentelemetry_api, "0.3.1", "aa042f9ff0b774c3e9827e215fcf972d5cfccd9a79ed55194b58c329a945b486", [:mix, :rebar3], [], "hexpm", "91fc78c521b9fc7f72f47144bb9f0838d57584b1be5bbd3098d8aaf2f2d42686"}, - "plug": {:hex, :plug, "1.10.0", "6508295cbeb4c654860845fb95260737e4a8838d34d115ad76cd487584e2fc4d", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "422a9727e667be1bf5ab1de03be6fa0ad67b775b2d84ed908f3264415ef29d4a"}, + "plug": {:git, "https://github.com/elixir-plug/plug.git", "9c880dee5a3d55c81f97a87076e540a9b5878cbd", [branch: "master"]}, "plug_crypto": {:hex, :plug_crypto, "1.1.2", "bdd187572cc26dbd95b87136290425f2b580a116d3fb1f564216918c9730d227", [:mix], [], "hexpm", "6b8b608f895b6ffcfad49c37c7883e8df98ae19c6a28113b02aa1e9c5b22d6b5"}, "telemetry": {:hex, :telemetry, "0.4.1", "ae2718484892448a24470e6aa341bc847c3277bfb8d4e9289f7474d752c09c7f", [:rebar3], [], "hexpm", "4738382e36a0a9a2b6e25d67c960e40e1a2c95560b9f936d8e29de8cd858480f"}, } diff --git a/test/opentelemetry_plug_test.exs b/test/opentelemetry_plug_test.exs index dfa65ae..ca514ea 100644 --- a/test/opentelemetry_plug_test.exs +++ b/test/opentelemetry_plug_test.exs @@ -39,7 +39,6 @@ defmodule MyRouter do use Plug.Router plug :match - plug Plug.Telemetry, event_prefix: [:my, :plug] plug :dispatch forward "/hello/:foo", to: MyPlug