Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add live_component update telemetry event #2914

Merged
merged 5 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions lib/phoenix_live_view/diff.ex
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,6 @@ defmodule Phoenix.LiveView.Diff do
{{pending, diffs, components}, seen_ids} =
Enum.reduce(pending, acc, fn {component, entries}, acc ->
{{pending, diffs, components}, seen_ids} = acc
update_many? = function_exported?(component, :update_many, 1)
entries = maybe_preload_components(component, Enum.reverse(entries))

{assigns_sockets, metadata, components, seen_ids} =
Expand All @@ -653,26 +652,37 @@ 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)
metadata = Enum.reverse(metadata)
update_many? = function_exported?(component, :update_many, 1)

telemetry_metadata = %{
socket: socket,
component: component,
assigns_sockets: assigns_sockets,
update_many?: update_many?
josevalim marked this conversation as resolved.
Show resolved Hide resolved
}

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})
{triplet, seen_ids}
end)
Expand Down
36 changes: 36 additions & 0 deletions test/phoenix_live_view/integrations/telemetry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,42 @@ defmodule Phoenix.LiveView.TelemtryTest 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
refute metadata.update_many?
josevalim marked this conversation as resolved.
Show resolved Hide resolved

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
Loading