Skip to content

Commit

Permalink
Add LiveView url template attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Jan 8, 2025
1 parent c65f72d commit 8462da6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 25 deletions.
24 changes: 21 additions & 3 deletions instrumentation/opentelemetry_phoenix/lib/opentelemetry_phoenix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ defmodule OpentelemetryPhoenix do
@tracer_id,
"#{inspect(live_view)}.mount",
meta,
%{kind: :server}
live_view_start_opts(meta)
)
end

Expand All @@ -181,7 +181,7 @@ defmodule OpentelemetryPhoenix do
@tracer_id,
"#{inspect(live_view)}.handle_params",
meta,
%{kind: :server}
live_view_start_opts(meta)
)
end

Expand All @@ -195,7 +195,7 @@ defmodule OpentelemetryPhoenix do
@tracer_id,
"#{inspect(live_view)}.handle_event##{event}",
meta,
%{kind: :server}
live_view_start_opts(meta)
)
end

Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}

Expand All @@ -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}
]}
Expand Down Expand Up @@ -95,17 +112,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
Expand All @@ -119,17 +136,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
Expand All @@ -143,17 +160,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
Expand Down Expand Up @@ -185,20 +202,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(
Expand Down Expand Up @@ -232,12 +249,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(
Expand Down

0 comments on commit 8462da6

Please sign in to comment.