From a8975ac9dd24ba2f2ccba74ccfee2596eb23c84e Mon Sep 17 00:00:00 2001 From: Dan Schultzer <1254724+danschultzer@users.noreply.github.com> Date: Fri, 13 Dec 2024 09:16:30 -0800 Subject: [PATCH] Add LiveView url template attribute --- .../lib/opentelemetry_phoenix.ex | 24 +++++++- .../test/opentelemetry_phoenix_test.exs | 61 ++++++++++++------- 2 files changed, 60 insertions(+), 25 deletions(-) diff --git a/instrumentation/opentelemetry_phoenix/lib/opentelemetry_phoenix.ex b/instrumentation/opentelemetry_phoenix/lib/opentelemetry_phoenix.ex index 14459ec5..24d64c8f 100644 --- a/instrumentation/opentelemetry_phoenix/lib/opentelemetry_phoenix.ex +++ b/instrumentation/opentelemetry_phoenix/lib/opentelemetry_phoenix.ex @@ -167,7 +167,7 @@ defmodule OpentelemetryPhoenix do @tracer_id, "#{inspect(live_view)}.mount", meta, - %{kind: :server} + live_view_start_opts(meta) ) end @@ -181,7 +181,7 @@ defmodule OpentelemetryPhoenix do @tracer_id, "#{inspect(live_view)}.handle_params", meta, - %{kind: :server} + live_view_start_opts(meta) ) end @@ -195,7 +195,7 @@ defmodule OpentelemetryPhoenix do @tracer_id, "#{inspect(live_view)}.handle_event##{event}", meta, - %{kind: :server} + live_view_start_opts(meta) ) end @@ -222,4 +222,22 @@ defmodule OpentelemetryPhoenix do OpenTelemetry.Span.set_status(ctx, OpenTelemetry.status(:error, "")) OpentelemetryTelemetry.end_telemetry_span(@tracer_id, meta) end + + defp live_view_start_opts(meta) do + %{ + kind: :server, + attributes: url_attributes(meta) + } + end + + defp url_attributes(%{uri: uri, socket: %{router: router}}) do + uri = URI.parse(uri) + + case Phoenix.Router.route_info(router, "GET", uri.path, uri.host) do + :error -> %{} + route_info -> %{URLAttributes.url_template() => route_info.route} + end + end + + defp url_attributes(_meta), do: %{} end diff --git a/instrumentation/opentelemetry_phoenix/test/opentelemetry_phoenix_test.exs b/instrumentation/opentelemetry_phoenix/test/opentelemetry_phoenix_test.exs index d8bb018e..d8312403 100644 --- a/instrumentation/opentelemetry_phoenix/test/opentelemetry_phoenix_test.exs +++ b/instrumentation/opentelemetry_phoenix/test/opentelemetry_phoenix_test.exs @@ -1,3 +1,19 @@ +defmodule NnnnnWeb.Router do + use Phoenix.Router, helpers: false + + import Phoenix.LiveView.Router + + scope "/", NnnnnWeb do + scope "/resources/", ResourceLive do + live "/:resource_id", Show, :show + end + end +end + +defmodule NnnnnWeb.ResourceLive.Show do + use Phoenix.LiveView, log: false +end + defmodule OpentelemetryPhoenixTest do use ExUnit.Case, async: false doctest OpentelemetryPhoenix @@ -12,23 +28,24 @@ defmodule OpentelemetryPhoenixTest do %Phoenix.LiveView.Socket{ endpoint: NnnnWeb.Endpoint, router: NnnnnWeb.Router, - view: NnnnnWeb.MyTestLive + view: NnnnnWeb.ResourceLive.Show }, %{ connect_params: %{}, connect_info: %{}, - root_view: NnnnnWeb.MyTestLive, + root_view: NnnnnWeb.ResourceLive.Show, live_temp: %{} }, - nil, + :show, %{}, - URI.parse("https://localhost:4000/live?foo=bar") + URI.parse("https://localhost:4000/resources/123?foo=bar") ) @telemetry_meta_base %{ socket: @socket, params: %{"foo" => "bar"}, - uri: "https://localhost:4000/live?foo=bar", + uri: "https://localhost:4000/resources/123?foo=bar", + session: %{}, telemetry_span_context: :dummy_ref } @@ -37,9 +54,9 @@ defmodule OpentelemetryPhoenixTest do @exception_meta %{ reason: %RuntimeError{message: "stop"}, stacktrace: [ - {NnnnnWeb.MyTestLive, :handle_params, 3, + {NnnnnWeb.ResourceLive.Show, :handle_params, 3, [ - file: ~c"lib/nnnnn_web/live/my_test_live.ex", + file: ~c"lib/nnnnn_web/live/resources_live/show.ex", line: 28, error_info: %{module: Exception} ]} @@ -94,17 +111,17 @@ defmodule OpentelemetryPhoenixTest do :telemetry.execute( [:phoenix, :live_view, :mount, :stop], - %{system_time: System.system_time()}, + %{system_time: System.system_time(), duration: 10}, @telemetry_meta.mount.stop ) assert_receive {:span, span( - name: "NnnnnWeb.MyTestLive.mount", + name: "NnnnnWeb.ResourceLive.Show.mount", attributes: attributes )} - assert %{} == :otel_attributes.map(attributes) + assert :otel_attributes.map(attributes) == %{:"url.template" => "/resources/:resource_id"} end test "records spans for Phoenix LiveView handle_params" do @@ -116,17 +133,17 @@ defmodule OpentelemetryPhoenixTest do :telemetry.execute( [:phoenix, :live_view, :handle_params, :stop], - %{system_time: System.system_time()}, + %{system_time: System.system_time(), duration: 10}, @telemetry_meta.handle_params.stop ) assert_receive {:span, span( - name: "NnnnnWeb.MyTestLive.handle_params", + name: "NnnnnWeb.ResourceLive.Show.handle_params", attributes: attributes )} - assert %{} == :otel_attributes.map(attributes) + assert :otel_attributes.map(attributes) == %{:"url.template" => "/resources/:resource_id"} end test "records spans for Phoenix LiveView handle_event" do @@ -138,17 +155,17 @@ defmodule OpentelemetryPhoenixTest do :telemetry.execute( [:phoenix, :live_view, :handle_event, :stop], - %{system_time: System.system_time()}, + %{system_time: System.system_time(), duration: 10}, @telemetry_meta.handle_event.stop ) assert_receive {:span, span( - name: "NnnnnWeb.MyTestLive.handle_event#hello", + name: "NnnnnWeb.ResourceLive.Show.handle_event#hello", attributes: attributes )} - assert %{} == :otel_attributes.map(attributes) + assert :otel_attributes.map(attributes) == %{:"url.template" => "/resources/:resource_id"} end test "handles exception during Phoenix LiveView handle_params" do @@ -178,20 +195,20 @@ defmodule OpentelemetryPhoenixTest do assert_receive {:span, span( - name: "NnnnnWeb.MyTestLive.mount", + name: "NnnnnWeb.ResourceLive.Show.mount", attributes: attributes )} - assert %{} == :otel_attributes.map(attributes) + assert :otel_attributes.map(attributes) == %{:"url.template" => "/resources/:resource_id"} assert_receive {:span, span( - name: "NnnnnWeb.MyTestLive.handle_params", + name: "NnnnnWeb.ResourceLive.Show.handle_params", attributes: attributes, events: events )} - assert %{} == :otel_attributes.map(attributes) + assert :otel_attributes.map(attributes) == %{:"url.template" => "/resources/:resource_id"} [ event( @@ -223,12 +240,12 @@ defmodule OpentelemetryPhoenixTest do assert_receive {:span, span( - name: "NnnnnWeb.MyTestLive.handle_event#hello", + name: "NnnnnWeb.ResourceLive.Show.handle_event#hello", attributes: attributes, events: events )} - assert %{} == :otel_attributes.map(attributes) + assert :otel_attributes.map(attributes) == %{:"url.template" => "/resources/:resource_id"} [ event(