Skip to content

Commit

Permalink
identify phoenix_live_view transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
tmaszk committed Jan 22, 2024
1 parent 63edba5 commit d23437d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
12 changes: 12 additions & 0 deletions examples/apps/phx_example/lib/phx_example_web/live/error_live.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule PhxExampleWeb.ErrorLive do
use PhxExampleWeb, :live_view

@impl true
def render(assigns) do
~H"""
<div>
<h1><%= @some_variable %></h1>
</div>
"""
end
end
13 changes: 13 additions & 0 deletions examples/apps/phx_example/lib/phx_example_web/live/home_live.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule PhxExampleWeb.HomeLive do
use PhxExampleWeb, :live_view

@impl true
def render(assigns) do
~H"""
<div>
<h1>Home</h1>
<p>Some content</p>
</div>
"""
end
end
3 changes: 3 additions & 0 deletions examples/apps/phx_example/lib/phx_example_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ defmodule PhxExampleWeb.Router do
scope "/phx", PhxExampleWeb do
pipe_through :browser

live "/home", HomeLive, :index
live "/live_error", ErrorLive, :index

get "/error", PageController, :error
get "/:foo", PageController, :index
end
Expand Down
49 changes: 49 additions & 0 deletions examples/apps/phx_example/test/phx_example_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ defmodule PhxExampleTest do
assert event[:status] == 200
end

test "Phoenix metrics generated for LiveView" do
TestSupport.restart_harvest_cycle(Collector.Metric.HarvestCycle)
TestSupport.restart_harvest_cycle(Collector.TransactionEvent.HarvestCycle)

{:ok, %{body: body}} = request("/phx/home")
assert body =~ "Some content"

metrics = TestSupport.gather_harvest(Collector.Metric.Harvester)

assert TestSupport.find_metric(
metrics,
"WebTransaction/Phoenix/PhxExampleWeb.HomeLive/index"
)

[[_, event]] = TestSupport.gather_harvest(Collector.TransactionEvent.Harvester)

assert event[:"phoenix.endpoint"] == "PhxExampleWeb.Endpoint"
assert event[:"phoenix.router"] == "PhxExampleWeb.Router"
assert event[:"phoenix.controller"] == "Phoenix.LiveView.Plug"
assert event[:"phoenix.action"] == "index"
assert event[:status] == 200
end

@tag :capture_log
test "Phoenix error" do
TestSupport.restart_harvest_cycle(Collector.Metric.HarvestCycle)
Expand All @@ -54,6 +77,32 @@ defmodule PhxExampleTest do
assert event[:error]
end

@tag :capture_log
test "Phoenix LiveView error" do
TestSupport.restart_harvest_cycle(Collector.Metric.HarvestCycle)
TestSupport.restart_harvest_cycle(Collector.TransactionEvent.HarvestCycle)

{:ok, %{body: body, status_code: 500}} = request("/phx/live_error")

assert body =~ "Opps, Internal Server Error"

metrics = TestSupport.gather_harvest(Collector.Metric.Harvester)

assert TestSupport.find_metric(
metrics,
"WebTransaction/Phoenix/PhxExampleWeb.ErrorLive/index"
)

[[_, event]] = TestSupport.gather_harvest(Collector.TransactionEvent.Harvester)

assert event[:status] == 500
assert event[:"phoenix.endpoint"] == "PhxExampleWeb.Endpoint"
assert event[:"phoenix.router"] == "PhxExampleWeb.Router"
assert event[:"phoenix.controller"] == "Phoenix.LiveView.Plug"
assert event[:"phoenix.action"] == "index"
assert event[:error]
end

test "Phoenix route not found" do
TestSupport.restart_harvest_cycle(Collector.Metric.HarvestCycle)
TestSupport.restart_harvest_cycle(Collector.TransactionEvent.HarvestCycle)
Expand Down
4 changes: 4 additions & 0 deletions lib/new_relic/telemetry/phoenix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ defmodule NewRelic.Telemetry.Phoenix do
:ignore
end

defp phoenix_name(%{phoenix_live_view: {module, action, _, _}}) when is_atom(action) do
"/Phoenix/#{inspect(module)}/#{action}"
end

defp phoenix_name(%{plug: controller, plug_opts: action}) when is_atom(action) do
"/Phoenix/#{inspect(controller)}/#{action}"
end
Expand Down

0 comments on commit d23437d

Please sign in to comment.