Skip to content

Commit 78bb89a

Browse files
authored
Refactor: unify trace data structures (#815)
1 parent 0fcdccc commit 78bb89a

File tree

16 files changed

+280
-256
lines changed

16 files changed

+280
-256
lines changed

lib/live_debugger/api/traces_storage.ex

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ defmodule LiveDebugger.API.TracesStorage do
55
"""
66

77
alias LiveDebugger.Structs.Trace
8-
alias LiveDebugger.Structs.DiffTrace
98
alias LiveDebugger.CommonTypes
109

11-
@type trace() :: Trace.t() | DiffTrace.t()
10+
import Trace, only: [is_trace: 1, is_trace_id: 1]
1211

1312
@typedoc """
1413
Pid is used to store mapping to table references.
@@ -21,8 +20,8 @@ defmodule LiveDebugger.API.TracesStorage do
2120

2221
@callback init() :: :ok
2322
@callback insert(Trace.t()) :: true
24-
@callback insert!(table_ref :: reference(), trace()) :: true
25-
@callback get_by_id!(table_identifier(), trace_id :: integer()) :: Trace.t() | nil
23+
@callback insert!(table_ref :: reference(), Trace.t()) :: true
24+
@callback get_by_id!(table_identifier(), trace_id :: Trace.id()) :: Trace.t() | nil
2625
@callback get!(table_identifier(), opts :: keyword()) ::
2726
{[Trace.t()], continuation()} | :end_of_table
2827
@callback clear!(table_identifier(), node_id :: pid() | CommonTypes.cid() | nil) :: true
@@ -33,7 +32,6 @@ defmodule LiveDebugger.API.TracesStorage do
3332
@callback table_size(table_identifier()) :: non_neg_integer()
3433

3534
defguard is_table_identifier(id) when is_pid(id) or is_reference(id)
36-
defguard is_trace(trace) when is_struct(trace, Trace) or is_struct(trace, DiffTrace)
3735

3836
@doc """
3937
Initializes ets table.
@@ -45,9 +43,9 @@ defmodule LiveDebugger.API.TracesStorage do
4543
@doc """
4644
Inserts a new trace into the storage.
4745
It has worse performance then `insert/2` as it has to perform lookup for reference.
48-
It stores the trace in table associated with `pid` given in `Trace` struct.
46+
It stores the trace in table associated with `pid` given in trace struct.
4947
"""
50-
@spec insert(trace()) :: true
48+
@spec insert(Trace.t()) :: true
5149
def insert(trace) when is_trace(trace) do
5250
impl().insert(trace)
5351
end
@@ -57,7 +55,7 @@ defmodule LiveDebugger.API.TracesStorage do
5755
It has better performance then `insert/1` as it does not perform lookup for reference.
5856
In order to use it properly you have to store the reference returned by `get_table/1`.
5957
"""
60-
@spec insert!(table_ref :: reference(), trace()) :: true
58+
@spec insert!(table_ref :: reference(), Trace.t()) :: true
6159
def insert!(table_ref, trace) when is_reference(table_ref) and is_trace(trace) do
6260
impl().insert!(table_ref, trace)
6361
end
@@ -68,9 +66,9 @@ defmodule LiveDebugger.API.TracesStorage do
6866
* `table_id` - PID or reference to an existing table. Using reference increases performance as it skips lookup step.
6967
* `trace_id` - Id of a trace stored in a table.
7068
"""
71-
@spec get_by_id!(table_identifier(), trace_id :: integer()) :: trace() | nil
69+
@spec get_by_id!(table_identifier(), trace_id :: Trace.id()) :: Trace.t() | nil
7270
def get_by_id!(table_id, trace_id)
73-
when is_table_identifier(table_id) and is_integer(trace_id) do
71+
when is_table_identifier(table_id) and is_trace_id(trace_id) do
7472
impl().get_by_id!(table_id, trace_id)
7573
end
7674

@@ -90,7 +88,7 @@ defmodule LiveDebugger.API.TracesStorage do
9088
* `:trace_diffs` - Boolean flag to include DiffTrace structs in results
9189
"""
9290
@spec get!(table_identifier(), opts :: keyword()) ::
93-
{[trace()], continuation()} | :end_of_table
91+
{[Trace.t()], continuation()} | :end_of_table
9492
def get!(table_id, opts \\ []) when is_table_identifier(table_id) and is_list(opts) do
9593
impl().get!(table_id, opts)
9694
end
@@ -168,7 +166,7 @@ defmodule LiveDebugger.API.TracesStorage do
168166
@traces_table_name :lvdbg_traces
169167
@processes_table_name :lvdbg_traces_processes
170168

171-
@type ets_elem() :: {integer(), TracesStorage.trace()}
169+
@type ets_elem() :: {integer(), Trace.t()}
172170
@type continuation() :: TracesStorage.continuation()
173171
@type ets_table_id() :: TracesStorage.ets_table_id()
174172
@type table_identifier() :: TracesStorage.table_identifier()
@@ -348,10 +346,10 @@ defmodule LiveDebugger.API.TracesStorage do
348346

349347
# Applies simple case-sensitive substring search on entire struct
350348
@spec filter_by_search(
351-
{[TracesStorage.trace()], continuation() | :searched_without_limit} | :end_of_table,
349+
{[Trace.t()], continuation() | :searched_without_limit} | :end_of_table,
352350
String.t()
353351
) ::
354-
{[TracesStorage.trace()], continuation() | :searched_without_limit} | :end_of_table
352+
{[Trace.t()], continuation() | :searched_without_limit} | :end_of_table
355353
defp filter_by_search(:end_of_table, _phrase), do: :end_of_table
356354
defp filter_by_search({traces, cont}, ""), do: {traces, cont}
357355

@@ -380,19 +378,19 @@ defmodule LiveDebugger.API.TracesStorage do
380378

381379
# Formats the continuation token and handles end-of-table marker.
382380
@spec format_response(
383-
{[TracesStorage.trace()], continuation() | :searched_without_limit}
381+
{[Trace.t()], continuation() | :searched_without_limit}
384382
| :end_of_table
385383
) ::
386-
{[TracesStorage.trace()], continuation() | :searched_without_limit} | :end_of_table
384+
{[Trace.t()], continuation() | :searched_without_limit} | :end_of_table
387385
defp format_response(:end_of_table), do: :end_of_table
388386
defp format_response({traces, :"$end_of_table"}), do: {traces, :end_of_table}
389387
defp format_response({traces, cont}), do: {traces, cont}
390388

391389
@spec limit_response(
392-
{[TracesStorage.trace()], :searched_without_limit} | :end_of_table,
390+
{[Trace.t()], :searched_without_limit} | :end_of_table,
393391
limit :: pos_integer()
394392
) ::
395-
{[TracesStorage.trace()], continuation()} | :end_of_table
393+
{[Trace.t()], continuation()} | :end_of_table
396394
defp limit_response(:end_of_table, _), do: :end_of_table
397395

398396
defp limit_response({traces, :searched_without_limit}, limit) do
@@ -475,7 +473,7 @@ defmodule LiveDebugger.API.TracesStorage do
475473
[
476474
{{:_,
477475
%{
478-
__struct__: LiveDebugger.Structs.Trace,
476+
__struct__: LiveDebugger.Structs.Trace.FunctionTrace,
479477
function: :"$1",
480478
execution_time: :"$2",
481479
arity: :"$3",
@@ -489,7 +487,7 @@ defmodule LiveDebugger.API.TracesStorage do
489487
[
490488
{{:_,
491489
%{
492-
__struct__: LiveDebugger.Structs.Trace,
490+
__struct__: LiveDebugger.Structs.Trace.FunctionTrace,
493491
function: :"$1",
494492
execution_time: :"$2",
495493
arity: :"$3",
@@ -502,7 +500,7 @@ defmodule LiveDebugger.API.TracesStorage do
502500
[
503501
{{:_,
504502
%{
505-
__struct__: LiveDebugger.Structs.Trace,
503+
__struct__: LiveDebugger.Structs.Trace.FunctionTrace,
506504
function: :"$1",
507505
execution_time: :"$2",
508506
arity: :"$3"
@@ -513,7 +511,7 @@ defmodule LiveDebugger.API.TracesStorage do
513511
defp maybe_attach_diff_spec(trace_spec, true) do
514512
trace_spec ++
515513
[
516-
{{:_, %{__struct__: LiveDebugger.Structs.DiffTrace}}, [], [:"$_"]}
514+
{{:_, %{__struct__: LiveDebugger.Structs.Trace.DiffTrace}}, [], [:"$_"]}
517515
]
518516
end
519517

lib/live_debugger/app/debugger/callback_tracing/structs/trace_display.ex

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ defmodule LiveDebugger.App.Debugger.CallbackTracing.Structs.TraceDisplay do
88
"""
99

1010
alias LiveDebugger.Structs.Trace
11-
alias LiveDebugger.Structs.DiffTrace
11+
alias LiveDebugger.Structs.Trace.FunctionTrace
12+
alias LiveDebugger.Structs.Trace.DiffTrace
1213
alias LiveDebugger.App.Utils.Parsers
1314
alias LiveDebugger.CommonTypes
1415

@@ -43,7 +44,7 @@ defmodule LiveDebugger.App.Debugger.CallbackTracing.Structs.TraceDisplay do
4344
side_section_right: side_section_right()
4445
}
4546

46-
@spec from_trace(Trace.t() | DiffTrace.t(), boolean()) :: t()
47+
@spec from_trace(Trace.t(), boolean()) :: t()
4748
def from_trace(trace, from_event? \\ false) do
4849
%__MODULE__{
4950
id: trace.id,
@@ -72,13 +73,13 @@ defmodule LiveDebugger.App.Debugger.CallbackTracing.Structs.TraceDisplay do
7273
end
7374

7475
defp get_type(%{type: :exception_from}), do: :error
75-
defp get_type(%LiveDebugger.Structs.DiffTrace{}), do: :diff
76+
defp get_type(%DiffTrace{}), do: :diff
7677
defp get_type(_), do: :normal
7778

78-
defp get_title(%Trace{} = trace), do: Trace.callback_name(trace)
79+
defp get_title(%FunctionTrace{} = trace), do: FunctionTrace.callback_name(trace)
7980
defp get_title(%DiffTrace{}), do: "Diff sent"
8081

81-
defp get_subtitle(%Trace{module: module, cid: cid}) do
82+
defp get_subtitle(%FunctionTrace{module: module, cid: cid}) do
8283
module_string = Parsers.module_to_string(module)
8384
cid_string = if cid, do: " (#{cid})", else: ""
8485

@@ -87,15 +88,15 @@ defmodule LiveDebugger.App.Debugger.CallbackTracing.Structs.TraceDisplay do
8788

8889
defp get_subtitle(%DiffTrace{}), do: nil
8990

90-
defp get_subtitle_link_data(%Trace{pid: pid, cid: cid}) do
91+
defp get_subtitle_link_data(%FunctionTrace{pid: pid, cid: cid}) do
9192
%{pid: pid, cid: cid}
9293
end
9394

9495
defp get_subtitle_link_data(%DiffTrace{}), do: nil
9596

96-
defp get_body(%Trace{args: args} = trace) do
97+
defp get_body(%FunctionTrace{args: args} = trace) do
9798
Enum.with_index(args, fn arg, index ->
98-
{"Arg #{index} (#{Trace.arg_name(trace, index)})", arg}
99+
{"Arg #{index} (#{FunctionTrace.arg_name(trace, index)})", arg}
99100
end)
100101
end
101102

@@ -105,7 +106,7 @@ defmodule LiveDebugger.App.Debugger.CallbackTracing.Structs.TraceDisplay do
105106
{:timestamp, timestamp}
106107
end
107108

108-
defp get_side_section_right(%Trace{execution_time: execution_time}) do
109+
defp get_side_section_right(%FunctionTrace{execution_time: execution_time}) do
109110
{:execution_time, execution_time}
110111
end
111112

lib/live_debugger/app/debugger/callback_tracing/web/hooks/display_new_traces.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule LiveDebugger.App.Debugger.CallbackTracing.Web.Hooks.DisplayNewTraces d
1010

1111
use LiveDebugger.App.Web, :hook
1212

13-
alias LiveDebugger.Structs.Trace
13+
alias LiveDebugger.Structs.Trace.FunctionTrace
1414
alias LiveDebugger.API.TracesStorage
1515
alias LiveDebugger.App.Debugger.CallbackTracing.Structs.TraceDisplay
1616
alias LiveDebugger.App.Debugger.CallbackTracing.Web.Helpers.Filters, as: FiltersHelpers
@@ -115,7 +115,7 @@ defmodule LiveDebugger.App.Debugger.CallbackTracing.Web.Hooks.DisplayNewTraces d
115115
)
116116
end
117117

118-
defp matches_execution_time_filter?(socket, %Trace{execution_time: execution_time}) do
118+
defp matches_execution_time_filter?(socket, %FunctionTrace{execution_time: execution_time}) do
119119
execution_time_limits = FiltersHelpers.get_execution_times(socket.assigns.current_filters)
120120

121121
min_time = Map.get(execution_time_limits, "exec_time_min", 0)

lib/live_debugger/services/callback_tracer/actions/diff_trace.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule LiveDebugger.Services.CallbackTracer.Actions.DiffTrace do
33
This module provides actions for LiveView diffs.
44
"""
55

6-
alias LiveDebugger.Structs.DiffTrace
6+
alias LiveDebugger.Structs.Trace.DiffTrace
77
alias LiveDebugger.Bus
88
alias LiveDebugger.Services.CallbackTracer.Events.DiffTraceCreated
99
alias LiveDebugger.API.TracesStorage

lib/live_debugger/services/callback_tracer/actions/trace.ex renamed to lib/live_debugger/services/callback_tracer/actions/function_trace.ex

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
defmodule LiveDebugger.Services.CallbackTracer.Actions.Trace do
1+
defmodule LiveDebugger.Services.CallbackTracer.Actions.FunctionTrace do
22
@moduledoc """
33
This module provides actions for traces.
44
"""
55

6-
alias LiveDebugger.Structs.Trace
6+
alias LiveDebugger.Structs.Trace.FunctionTrace
77
alias LiveDebugger.API.TracesStorage
88
alias LiveDebugger.API.LiveViewDebug
99
alias LiveDebugger.Utils.Modules, as: UtilsModules
@@ -20,9 +20,9 @@ defmodule LiveDebugger.Services.CallbackTracer.Actions.Trace do
2020
args :: list(),
2121
pid :: pid(),
2222
timestamp :: :erlang.timestamp()
23-
) :: {:ok, Trace.t()} | {:error, term()}
23+
) :: {:ok, FunctionTrace.t()} | {:error, term()}
2424
def create_trace(n, module, fun, args, pid, timestamp) do
25-
trace = Trace.new(n, module, fun, args, pid, timestamp)
25+
trace = FunctionTrace.new(n, module, fun, args, pid, timestamp)
2626

2727
case trace.transport_pid do
2828
nil ->
@@ -39,7 +39,7 @@ defmodule LiveDebugger.Services.CallbackTracer.Actions.Trace do
3939
pid :: pid(),
4040
cid :: String.t(),
4141
timestamp :: :erlang.timestamp()
42-
) :: {:ok, Trace.t()} | :live_debugger_trace | {:error, term()}
42+
) :: {:ok, FunctionTrace.t()} | :live_debugger_trace | {:error, term()}
4343
def create_delete_component_trace(n, args, pid, cid, timestamp) do
4444
pid
4545
|> LiveViewDebug.socket()
@@ -49,7 +49,7 @@ defmodule LiveDebugger.Services.CallbackTracer.Actions.Trace do
4949
:live_debugger_trace
5050
else
5151
trace =
52-
Trace.new(
52+
FunctionTrace.new(
5353
n,
5454
Phoenix.LiveView.Diff,
5555
:delete_component,
@@ -69,13 +69,13 @@ defmodule LiveDebugger.Services.CallbackTracer.Actions.Trace do
6969
end
7070
end
7171

72-
@spec update_trace(Trace.t(), map()) :: {:ok, Trace.t()}
73-
def update_trace(%Trace{} = trace, params) do
72+
@spec update_trace(FunctionTrace.t(), map()) :: {:ok, FunctionTrace.t()}
73+
def update_trace(%FunctionTrace{} = trace, params) do
7474
{:ok, Map.merge(trace, params)}
7575
end
7676

77-
@spec persist_trace(Trace.t()) :: {:ok, reference()} | {:error, term()}
78-
def persist_trace(%Trace{pid: pid} = trace) do
77+
@spec persist_trace(FunctionTrace.t()) :: {:ok, reference()} | {:error, term()}
78+
def persist_trace(%FunctionTrace{pid: pid} = trace) do
7979
with ref when is_reference(ref) <- TracesStorage.get_table(pid),
8080
true <- TracesStorage.insert!(ref, trace) do
8181
{:ok, ref}
@@ -85,15 +85,15 @@ defmodule LiveDebugger.Services.CallbackTracer.Actions.Trace do
8585
end
8686
end
8787

88-
@spec persist_trace(Trace.t(), reference()) :: {:ok, reference()}
89-
def persist_trace(%Trace{} = trace, ref) do
88+
@spec persist_trace(FunctionTrace.t(), reference()) :: {:ok, reference()}
89+
def persist_trace(%FunctionTrace{} = trace, ref) do
9090
TracesStorage.insert!(ref, trace)
9191

9292
{:ok, ref}
9393
end
9494

95-
@spec publish_trace(Trace.t(), reference() | nil) :: :ok | {:error, term()}
96-
def publish_trace(%Trace{pid: pid} = trace, ref \\ nil) do
95+
@spec publish_trace(FunctionTrace.t(), reference() | nil) :: :ok | {:error, term()}
96+
def publish_trace(%FunctionTrace{pid: pid} = trace, ref \\ nil) do
9797
trace
9898
|> get_event(ref)
9999
|> Bus.broadcast_trace!(pid)
@@ -102,7 +102,7 @@ defmodule LiveDebugger.Services.CallbackTracer.Actions.Trace do
102102
{:error, err}
103103
end
104104

105-
defp get_event(%Trace{type: :call} = trace, ref) do
105+
defp get_event(%FunctionTrace{type: :call} = trace, ref) do
106106
%TraceCalled{
107107
trace_id: trace.id,
108108
ets_ref: ref,
@@ -115,7 +115,7 @@ defmodule LiveDebugger.Services.CallbackTracer.Actions.Trace do
115115
}
116116
end
117117

118-
defp get_event(%Trace{type: :return_from} = trace, ref) do
118+
defp get_event(%FunctionTrace{type: :return_from} = trace, ref) do
119119
%TraceReturned{
120120
trace_id: trace.id,
121121
ets_ref: ref,
@@ -128,7 +128,7 @@ defmodule LiveDebugger.Services.CallbackTracer.Actions.Trace do
128128
}
129129
end
130130

131-
defp get_event(%Trace{type: :exception_from} = trace, ref) do
131+
defp get_event(%FunctionTrace{type: :exception_from} = trace, ref) do
132132
%TraceErrored{
133133
trace_id: trace.id,
134134
ets_ref: ref,

lib/live_debugger/services/callback_tracer/actions/state.ex

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule LiveDebugger.Services.CallbackTracer.Actions.State do
55

66
alias LiveDebugger.API.StatesStorage
77
alias LiveDebugger.API.LiveViewDebug
8-
alias LiveDebugger.Structs.Trace
8+
alias LiveDebugger.Structs.Trace.FunctionTrace
99
alias LiveDebugger.Structs.LvState
1010

1111
alias LiveDebugger.Bus
@@ -14,17 +14,22 @@ defmodule LiveDebugger.Services.CallbackTracer.Actions.State do
1414
@doc """
1515
It checks if the trace is state changing and if so, it saves the state.
1616
"""
17-
@spec maybe_save_state!(Trace.t()) :: :ok
18-
def maybe_save_state!(%Trace{pid: pid, function: :render, type: :return_from}) do
17+
@spec maybe_save_state!(FunctionTrace.t()) :: :ok
18+
def maybe_save_state!(%FunctionTrace{pid: pid, function: :render, type: :return_from}) do
1919
do_save_state!(pid)
2020
end
2121

22-
def maybe_save_state!(%Trace{pid: pid, function: function, type: type, args: [_, _, socket]})
22+
def maybe_save_state!(%FunctionTrace{
23+
pid: pid,
24+
function: function,
25+
type: type,
26+
args: [_, _, socket]
27+
})
2328
when function in [:mount, :handle_params] and type in [:return_from, :exception_from] do
2429
do_save_initial_state!(pid, socket)
2530
end
2631

27-
def maybe_save_state!(%Trace{pid: pid, function: :delete_component, type: :call}) do
32+
def maybe_save_state!(%FunctionTrace{pid: pid, function: :delete_component, type: :call}) do
2833
do_save_state!(pid)
2934
end
3035

0 commit comments

Comments
 (0)