Skip to content

Commit

Permalink
Add live_component update telemetry event (#2914)
Browse files Browse the repository at this point in the history
  • Loading branch information
bamorim authored Nov 13, 2023
1 parent 84899de commit 5599a00
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
33 changes: 21 additions & 12 deletions lib/phoenix_live_view/diff.ex
Original file line number Diff line number Diff line change
Expand Up @@ -653,24 +653,33 @@ defmodule Phoenix.LiveView.Diff do
put_cid(components, component, id, cid)}
end

assigns_sockets =
if update_many? do
[{new_assigns, socket} | assigns_sockets]
else
[Utils.maybe_call_update!(socket, component, new_assigns) | assigns_sockets]
end

assigns_sockets = [{new_assigns, socket} | assigns_sockets]
metadata = [{cid, id, new?} | metadata]
seen_ids = Map.put(seen_ids, [component | id], true)
{assigns_sockets, metadata, components, seen_ids}
end)

assigns_sockets = Enum.reverse(assigns_sockets)

telemetry_metadata = %{
socket: socket,
component: component,
assigns_sockets: assigns_sockets
}

sockets =
if update_many? do
component.update_many(Enum.reverse(assigns_sockets))
else
Enum.reverse(assigns_sockets)
end
:telemetry.span([:phoenix, :live_component, :update], telemetry_metadata, fn ->
sockets =
if update_many? do
component.update_many(assigns_sockets)
else
Enum.map(assigns_sockets, fn {assigns, socket} ->
Utils.maybe_call_update!(socket, component, assigns)
end)
end

{sockets, Map.put(telemetry_metadata, :sockets, sockets)}
end)

metadata = Enum.reverse(metadata)
triplet = zip_components(sockets, metadata, component, cids, {pending, diffs, components})
Expand Down
35 changes: 35 additions & 0 deletions test/phoenix_live_view/integrations/telemetry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,41 @@ defmodule Phoenix.LiveView.TelemetryTest do
assert metadata.component == Phoenix.LiveViewTest.StatefulComponent
assert metadata.params == %{"op" => "boom"}
end

test "emits telemetry events for update calls", %{conn: conn} do
attach_telemetry([:phoenix, :live_component, :update])

{:ok, view, _html} = live(conn, "/multi-targets")

assert_receive {:event, [:phoenix, :live_component, :update, :start], %{system_time: _},
metadata}

assert metadata.socket
assert metadata.component == Phoenix.LiveViewTest.StatefulComponent

assert [
{
%{id: _id, name: name},
%{assigns: %{myself: cid}} = component_socket
},
_
] = metadata.assigns_sockets

assert_receive {:event, [:phoenix, :live_component, :update, :stop], %{duration: _},
metadata}

assert metadata.socket
assert metadata.component == Phoenix.LiveViewTest.StatefulComponent
assert [updated_component_socket, _] = metadata.sockets

assert updated_component_socket != component_socket
assert %{myself: ^cid} = updated_component_socket.assigns

render_click(view, "disable", %{"name" => name})

assert_receive {:event, [:phoenix, :live_component, :update, :start], %{system_time: _},
%{assigns_sockets: [{%{name: ^name, disabled: true}, _} | _]}}
end
end

describe "logging configuration" do
Expand Down
4 changes: 4 additions & 0 deletions test/support/live_views/components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ defmodule Phoenix.LiveViewTest.WithMultipleTargets do
def handle_event("transform", %{"op" => _op}, socket) do
{:noreply, assign(socket, :message, "Parent was updated")}
end

def handle_event("disable", %{"name" => name}, socket) do
{:noreply, assign(socket, :disabled, Enum.uniq([name | socket.assigns.disabled]))}
end
end

defmodule Phoenix.LiveViewTest.WithLogOverride do
Expand Down

0 comments on commit 5599a00

Please sign in to comment.