Skip to content

Commit

Permalink
Add live_component update event handling
Browse files Browse the repository at this point in the history
Much like the events already handled, the live_component update event,
which is triggered whenever live components are updated.

Closes #102
  • Loading branch information
jeffkreeftmeijer committed Nov 21, 2024
1 parent 3fa95ae commit 5f2f07c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changesets/handle-live-component-update-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: minor
type: add
---

Handle live component update events
3 changes: 2 additions & 1 deletion lib/appsignal_phoenix/live_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ defmodule Appsignal.Phoenix.LiveView do
[:phoenix, :live_view, :handle_params],
[:phoenix, :live_view, :handle_event],
[:phoenix, :live_view, :render],
[:phoenix, :live_component, :handle_event]
[:phoenix, :live_component, :handle_event],
[:phoenix, :live_component, :update]
]
|> Enum.each(fn event ->
name = Enum.join(event, ".")
Expand Down
66 changes: 66 additions & 0 deletions test/appsignal_phoenix/live_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,21 @@ defmodule Appsignal.Phoenix.LiveViewTest do
:telemetry.detach(
{Appsignal.Phoenix.LiveView, [:phoenix, :live_component, :handle_event, :exception]}
)

:ok =
:telemetry.detach(
{Appsignal.Phoenix.LiveView, [:phoenix, :live_component, :update, :start]}
)

:ok =
:telemetry.detach(
{Appsignal.Phoenix.LiveView, [:phoenix, :live_component, :update, :stop]}
)

:ok =
:telemetry.detach(
{Appsignal.Phoenix.LiveView, [:phoenix, :live_component, :update, :exception]}
)
end)
end

Expand All @@ -281,6 +296,9 @@ defmodule Appsignal.Phoenix.LiveViewTest do
assert attached?([:phoenix, :live_component, :handle_event, :start])
assert attached?([:phoenix, :live_component, :handle_event, :stop])
assert attached?([:phoenix, :live_component, :handle_event, :exception])
assert attached?([:phoenix, :live_component, :update, :start])
assert attached?([:phoenix, :live_component, :update, :stop])
assert attached?([:phoenix, :live_component, :update, :exception])
end
end

Expand Down Expand Up @@ -445,6 +463,54 @@ defmodule Appsignal.Phoenix.LiveViewTest do
end
end

describe "handle_event_start/4, with a live_component update event" do
setup do
event = [:phoenix, :live_component, :update, :start]

:telemetry.attach(
{__MODULE__, event},
event,
&Appsignal.Phoenix.LiveView.handle_event_start/4,
:ok
)

:telemetry.execute(
[:phoenix, :live_component, :update, :start],
%{monotonic_time: -576_457_566_461_433_920, system_time: 1_653_474_764_790_125_080},
%{
params: %{foo: "bar"},
socket: %Phoenix.LiveView.Socket{view: __MODULE__}
}
)
end

test "creates a root span with a namespace and a start time" do
assert {:ok, [{"live_view", nil, [start_time: 1_653_474_764_790_125_080]}]} =
Test.Tracer.get(:create_span)
end

test "sets the span's name" do
assert {:ok, [{%Span{}, "Appsignal.Phoenix.LiveViewTest#update"}]} =
Test.Span.get(:set_name)
end

test "sets the span's category" do
assert {:ok, attributes} = Test.Span.get(:set_attribute)

assert Enum.any?(attributes, fn {%Span{}, key, data} ->
key == "appsignal:category" and data == "update.live_view"
end)
end

test "sets the span's params" do
assert {:ok, attributes} = Test.Span.get(:set_sample_data)

assert Enum.any?(attributes, fn {%Span{}, key, data} ->
key == "params" and data == %{foo: "bar"}
end)
end
end

describe "handle_event_stop/4" do
setup do
event = [:phoenix, :live_view, :mount, :stop]
Expand Down

0 comments on commit 5f2f07c

Please sign in to comment.