Skip to content

Commit 24095d4

Browse files
authored
Enhancement: Add module in label for global traces (#494)
* use slot in stream component * use module name in trace label * add test for module name
1 parent 1021b59 commit 24095d4

File tree

5 files changed

+74
-16
lines changed

5 files changed

+74
-16
lines changed

lib/live_debugger_web/live/traces/components/stream.ex

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
defmodule LiveDebuggerWeb.Live.Traces.Components.Stream do
22
@moduledoc """
33
This component is used to display the traces stream.
4-
It uses under the hood the `Trace` component to display the traces.
5-
The `Trace` component produces the `toggle-collapsible` and `trace-fullscreen` which are handled in the `Trace` component hook.
4+
It allows to compose style for the trace using `LiveDebuggerWeb.Live.Traces.Components.Trace` components.
65
"""
76

87
use LiveDebuggerWeb, :hook_component
98

10-
alias LiveDebuggerWeb.Live.Traces.Components.Trace
11-
129
@required_assigns [:id, :existing_traces_status]
1310

1411
@doc """
1512
Initializes the component by attaching the hook to the socket.
16-
Since the `Trace` component is used by this component, we need to attach the hook to the socket.
1713
"""
1814
@spec init(Phoenix.LiveView.Socket.t()) :: Phoenix.LiveView.Socket.t()
1915
def init(socket) do
2016
socket
2117
|> check_assigns!(@required_assigns)
2218
|> check_stream!(:existing_traces)
23-
|> Trace.init()
2419
|> register_hook(:traces_stream)
2520
end
2621

@@ -33,6 +28,8 @@ defmodule LiveDebuggerWeb.Live.Traces.Components.Stream do
3328
attr(:existing_traces_status, :atom, required: true)
3429
attr(:existing_traces, :any, required: true)
3530

31+
slot(:trace, required: true, doc: "Used for styling trace element. Remember to add `id`")
32+
3633
def traces_stream(assigns) do
3734
~H"""
3835
<div id={"#{@id}-stream"} phx-update="stream" class="flex flex-col gap-2">
@@ -56,7 +53,7 @@ defmodule LiveDebuggerWeb.Live.Traces.Components.Stream do
5653
<%= if wrapped_trace.id == "separator" do %>
5754
<.separator id={dom_id} />
5855
<% else %>
59-
<Trace.trace id={dom_id} wrapped_trace={wrapped_trace} />
56+
<%= render_slot(@trace, {dom_id, wrapped_trace}) %>
6057
<% end %>
6158
<% end %>
6259
</div>

lib/live_debugger_web/live/traces/components/trace.ex

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ defmodule LiveDebuggerWeb.Live.Traces.Components.Trace do
3737
attr(:id, :string, required: true)
3838
attr(:wrapped_trace, :map, required: true, doc: "The Trace to render")
3939

40+
slot :label, required: true do
41+
attr(:class, :string, doc: "Additional class for label")
42+
end
43+
4044
def trace(assigns) do
4145
assigns =
4246
assigns
@@ -51,15 +55,17 @@ defmodule LiveDebuggerWeb.Live.Traces.Components.Trace do
5155
icon="icon-chevron-right"
5256
chevron_class="w-5 h-5 text-accent-icon"
5357
class="max-w-full border border-default-border rounded last:mb-4"
54-
label_class="font-semibold bg-surface-1-bg h-10 p-2"
58+
label_class="font-semibold bg-surface-1-bg p-2 py-3"
5559
phx-click={if(@render_body?, do: nil, else: "toggle-collapsible")}
5660
phx-value-trace-id={@trace.id}
5761
>
5862
<:label>
59-
<div id={@id <> "-label"} class="w-[90%] grow flex items-center ml-2 gap-3">
60-
<p class="font-medium text-sm"><%= @callback_name %></p>
61-
<.short_trace_content trace={@trace} />
62-
<.trace_time_info id={@id} trace={@trace} from_tracing?={@from_tracing?} />
63+
<div
64+
:for={label <- @label}
65+
id={@id <> "-label"}
66+
class={["w-[90%] grow grid items-center gap-x-3 ml-2" | List.wrap(label[:class])]}
67+
>
68+
<%= render_slot(@label, assigns) %>
6369
</div>
6470
</:label>
6571
<div class="relative">
@@ -85,9 +91,29 @@ defmodule LiveDebuggerWeb.Live.Traces.Components.Trace do
8591
"""
8692
end
8793

94+
attr(:trace, Trace, required: true)
95+
attr(:class, :string, default: "")
96+
97+
def module(assigns) do
98+
~H"""
99+
<div class={["text-primary text-2xs font-normal truncate", @class]}>
100+
<%= Parsers.module_to_string(@trace.module) %>
101+
<%= if(@trace.cid, do: "(#{@trace.cid})") %>
102+
</div>
103+
"""
104+
end
105+
106+
attr(:content, :string, required: true)
107+
108+
def callback_name(assigns) do
109+
~H"""
110+
<p class="font-medium text-sm"><%= @content %></p>
111+
"""
112+
end
113+
88114
attr(:trace, :map, default: nil)
89115

90-
defp short_trace_content(assigns) do
116+
def short_trace_content(assigns) do
91117
assigns = assign(assigns, :content, Enum.map_join(assigns.trace.args, " ", &inspect/1))
92118

93119
~H"""
@@ -101,7 +127,7 @@ defmodule LiveDebuggerWeb.Live.Traces.Components.Trace do
101127
attr(:trace, :map, default: nil)
102128
attr(:from_tracing?, :boolean, default: false)
103129

104-
defp trace_time_info(assigns) do
130+
def trace_time_info(assigns) do
105131
~H"""
106132
<div class="flex text-xs font-normal text-secondary-text align-center">
107133
<.tooltip id={@id <> "-timestamp"} content="timestamp" class="min-w-24">

lib/live_debugger_web/live/traces/node_traces_live.ex

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ defmodule LiveDebuggerWeb.Live.Traces.NodeTracesLive do
6868
|> Helpers.assign_current_filters()
6969
|> Components.ClearButton.init()
7070
|> Components.LoadMoreButton.init(@page_size)
71+
|> Components.Trace.init()
7172
|> Components.Stream.init()
7273
|> Hooks.TracingFuse.init()
7374
|> Hooks.ExistingTraces.init(@page_size)
@@ -111,7 +112,21 @@ defmodule LiveDebuggerWeb.Live.Traces.NodeTracesLive do
111112
id={@id}
112113
existing_traces_status={@existing_traces_status}
113114
existing_traces={@streams.existing_traces}
114-
/>
115+
>
116+
<:trace :let={{id, wrapped_trace}}>
117+
<Components.Trace.trace id={id} wrapped_trace={wrapped_trace}>
118+
<:label :let={trace_assigns} class="grid-cols-[auto_1fr_auto]">
119+
<Components.Trace.callback_name content={trace_assigns.callback_name} />
120+
<Components.Trace.short_trace_content trace={trace_assigns.trace} />
121+
<Components.Trace.trace_time_info
122+
id={trace_assigns.id}
123+
trace={trace_assigns.trace}
124+
from_tracing?={trace_assigns.from_tracing?}
125+
/>
126+
</:label>
127+
</Components.Trace.trace>
128+
</:trace>
129+
</Components.Stream.traces_stream>
115130
<Components.LoadMoreButton.load_more_button
116131
:if={not @tracing_started? and not @traces_empty?}
117132
traces_continuation={@traces_continuation}

lib/live_debugger_web/live/traces/process_traces_live.ex

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ defmodule LiveDebuggerWeb.Live.Traces.ProcessTracesLive do
5555
|> Components.RefreshButton.init()
5656
|> Components.ClearButton.init()
5757
|> Components.ToggleTracingButton.init()
58+
|> Components.Trace.init()
5859
|> Components.Stream.init()
5960
|> ok()
6061
end
@@ -83,7 +84,22 @@ defmodule LiveDebuggerWeb.Live.Traces.ProcessTracesLive do
8384
id={@id}
8485
existing_traces_status={@existing_traces_status}
8586
existing_traces={@streams.existing_traces}
86-
/>
87+
>
88+
<:trace :let={{id, wrapped_trace}}>
89+
<Components.Trace.trace id={id} wrapped_trace={wrapped_trace}>
90+
<:label :let={trace_assigns} class="grid-cols-[auto_1fr_auto]">
91+
<Components.Trace.module trace={trace_assigns.trace} class="col-span-3" />
92+
<Components.Trace.callback_name content={trace_assigns.callback_name} />
93+
<Components.Trace.short_trace_content trace={trace_assigns.trace} />
94+
<Components.Trace.trace_time_info
95+
id={trace_assigns.id}
96+
trace={trace_assigns.trace}
97+
from_tracing?={trace_assigns.from_tracing?}
98+
/>
99+
</:label>
100+
</Components.Trace.trace>
101+
</:trace>
102+
</Components.Stream.traces_stream>
87103
<Components.LoadMoreButton.load_more_button
88104
:if={not @tracing_started? and not @traces_empty?}
89105
traces_continuation={@traces_continuation}

test/live_debugger/process_callback_traces_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ defmodule LiveDebugger.ProcessCallbackTracesTest do
3434
|> assert_has(trace_name(text: "handle_event/3", count: 1))
3535
|> assert_has(trace_name(text: "handle_info/2", count: 1))
3636
|> assert_has(trace_name(text: "render/1", count: 1))
37+
|> assert_has(trace_module(text: "LiveDebuggerDev.LiveViews.Main", count: 2))
38+
|> assert_has(trace_module(text: "LiveDebuggerDev.LiveComponents.Send (4)", count: 1))
3739
|> click(clear_traces_button())
3840
|> assert_has(traces(count: 0))
3941
|> assert_has(no_traces_info())
@@ -54,5 +56,7 @@ defmodule LiveDebugger.ProcessCallbackTracesTest do
5456

5557
defp trace_name(opts), do: css("#global-traces-stream details p.font-medium", opts)
5658

59+
defp trace_module(opts), do: css("#global-traces-stream details div.col-span-3", opts)
60+
5761
defp global_callback_traces_button(), do: css("button[aria-label=\"Icon globe\"]")
5862
end

0 commit comments

Comments
 (0)