Skip to content

Commit

Permalink
Add live_component handler to the LiveView integration (#50)
Browse files Browse the repository at this point in the history
* Add LiveView handler for live_component events

This patch adds a new function clause that handles :live_component
events, which is based on the :live_view handle_event implementation,
but doesn't include session data, as that's not present in live
components.

* Detach live_component handlers in LiveViewTest

Because the handlers weren't detached, the test suite started failing
sometimes because events came in twice. By detaching the handlers like
we do for all other events, we only have one set of handlers running in
the test suite.

* Remove extra LiveView clause for live_components

By depending on :appsignal 2.2.16 or above, the event handler can call
set_sample_data with an empty value for metadata without breaking the
call chain, so both function clauses can be merged.

* Add changeset to ecplain live_component handling

"Handle live_component events in LiveView integration"

Co-authored-by: AJ Foster <2789166+aj-foster@users.noreply.github.com>
  • Loading branch information
jeffkreeftmeijer and aj-foster authored Aug 4, 2022
1 parent e5da1e5 commit c915a34
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: "patch"
type: "add"
---

Handle live_component events in LiveView integration
2 changes: 1 addition & 1 deletion lib/appsignal_phoenix/live_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ defmodule Appsignal.Phoenix.LiveView do
end

def handle_event_start(
[:phoenix, :live_view, name, :start],
[:phoenix, _type, name, :start],
%{system_time: system_time},
metadata,
_event_name
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule Appsignal.Phoenix.MixProject do
end

[
{:appsignal, ">= 2.2.15 and < 3.0.0"},
{:appsignal, ">= 2.2.16 and < 3.0.0"},
{:appsignal_plug, ">= 2.0.11 and < 3.0.0"},
{:phoenix, "~> 1.4"},
{:phoenix_html, "~> 2.11 or ~> 3.0", optional: true},
Expand Down
63 changes: 63 additions & 0 deletions test/appsignal_phoenix/live_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,21 @@ defmodule Appsignal.Phoenix.LiveViewTest do
:telemetry.detach(
{Appsignal.Phoenix.LiveView, [:phoenix, :live_view, :handle_event, :exception]}
)

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

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

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

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

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

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

:telemetry.execute(
[:phoenix, :live_component, :handle_event, :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#handle_event"}]} =
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 == "handle_event.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 c915a34

Please sign in to comment.