Skip to content

Commit

Permalink
Rename Events section to Callback traces (#78)
Browse files Browse the repository at this point in the history
* Changed naming for rate limiter

* Renamed events_list

* Replaced events in detail_view
  • Loading branch information
kraleppa authored Feb 11, 2025
1 parent 33f9d0e commit f6efc9c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 71 deletions.
8 changes: 4 additions & 4 deletions lib/live_debugger/live_components/detail_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule LiveDebugger.LiveComponents.DetailView do
socket
|> assign(:hide_assigns_section?, false)
|> assign(:hide_info_section?, false)
|> assign(:hide_events_section?, false)
|> assign(:hide_traces_section?, false)
|> ok()
end

Expand Down Expand Up @@ -68,8 +68,8 @@ defmodule LiveDebugger.LiveComponents.DetailView do
<.assigns_card assigns={node.assigns} myself={@myself} hide?={@hide_assigns_section?} />
</div>
<.live_component
id="event-list"
module={LiveDebugger.LiveComponents.EventsList}
id="trace-list"
module={LiveDebugger.LiveComponents.TracesList}
debugged_node_id={@node_id}
socket_id={@socket_id}
/>
Expand All @@ -85,7 +85,7 @@ defmodule LiveDebugger.LiveComponents.DetailView do
case section do
"info" -> :hide_info_section?
"assigns" -> :hide_assigns_section?
"events" -> :hide_events_section?
"traces" -> :hide_traces_section?
end

socket
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule LiveDebugger.LiveComponents.EventsList do
defmodule LiveDebugger.LiveComponents.TracesList do
@moduledoc """
This module provides a LiveComponent to display events.
This module provides a LiveComponent to display traces.
"""

use LiveDebuggerWeb, :live_component
Expand Down Expand Up @@ -53,8 +53,8 @@ defmodule LiveDebugger.LiveComponents.EventsList do
~H"""
<div>
<Collapsible.section
title="Events"
id="events"
title="Callback traces"
id="traces"
class="h-full md:overflow-y-auto"
myself={@myself}
hide?={@hide_section?}
Expand All @@ -64,7 +64,7 @@ defmodule LiveDebugger.LiveComponents.EventsList do
<.button color="primary" phx-click="switch-tracing" phx-target={@myself}>
<%= if @tracing_started?, do: "Stop", else: "Start" %>
</.button>
<.button variant="simple" color="primary" phx-click="clear-events" phx-target={@myself}>
<.button variant="simple" color="primary" phx-click="clear-traces" phx-target={@myself}>
Clear
</.button>
</div>
Expand All @@ -73,7 +73,7 @@ defmodule LiveDebugger.LiveComponents.EventsList do
<div id={"#{assigns.id}-stream"} phx-update="stream">
<div id={"#{assigns.id}-stream-empty"} class="only:block hidden text-gray-700">
<div :if={@existing_traces_status == :ok}>
No events have been recorded yet.
No traces have been recorded yet.
</div>
<div
:if={@existing_traces_status == :loading}
Expand All @@ -85,9 +85,9 @@ defmodule LiveDebugger.LiveComponents.EventsList do
:if={@existing_traces_status == :error}
variant="danger"
with_icon
heading="Error fetching historical events"
heading="Error fetching historical traces"
>
The new events still will be displayed as they come. Check logs for more
The new traces still will be displayed as they come. Check logs for more
</.alert>
</div>
<%= for {dom_id, trace} <- @streams.existing_traces do %>
Expand Down Expand Up @@ -131,7 +131,7 @@ defmodule LiveDebugger.LiveComponents.EventsList do
|> noreply()
end

def handle_event("clear-events", _, socket) do
def handle_event("clear-traces", _, socket) do
ets_table_id = socket.assigns.ets_table_id
node_id = socket.assigns.debugged_node_id

Expand Down
4 changes: 2 additions & 2 deletions lib/live_debugger/live_views/channel_dashboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ defmodule LiveDebugger.LiveViews.ChannelDashboard do
debugged_node_id = socket.assigns.node_id || socket.assigns.debugged_pid.result

if Trace.node_id(trace) == debugged_node_id do
send_update(LiveDebugger.LiveComponents.EventsList, %{id: "event-list", new_trace: trace})
send_update(LiveDebugger.LiveComponents.TracesList, %{id: "trace-list", new_trace: trace})
send_update(LiveDebugger.LiveComponents.DetailView, %{id: "detail_view", new_trace: trace})
end

Expand Down Expand Up @@ -181,7 +181,7 @@ defmodule LiveDebugger.LiveViews.ChannelDashboard do

defp assign_rate_limiter_pid(socket) do
if connected?(socket) do
{:ok, pid} = LiveDebugger.Services.EventRateLimiter.start_link()
{:ok, pid} = LiveDebugger.Services.TraceRateLimiter.start_link()
assign(socket, :rate_limiter_pid, pid)
else
assign(socket, :rate_limiter_pid, nil)
Expand Down
56 changes: 0 additions & 56 deletions lib/live_debugger/services/event_rate_limiter.ex

This file was deleted.

56 changes: 56 additions & 0 deletions lib/live_debugger/services/trace_rate_limiter.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
defmodule LiveDebugger.Services.TraceRateLimiter do
@moduledoc """
This module provides a rate limiter for traces.
It should be used as a proxy between the `:dbg` tracer and LiveDebugger dashboard.
It limits the number of traces that are sent to the dashboard.
You can configure the number of traces and the period in which they are counted via module attributes.
"""
use GenServer

@traces_number 10
@period_ms 1000
@interval_ms div(@period_ms, @traces_number)

def start_link(target_pid \\ self()) do
GenServer.start_link(__MODULE__, %{target_pid: target_pid})
end

@impl true
def init(%{target_pid: target_pid}) do
schedule_processing()
{:ok, %{target_pid: target_pid, traces: []}}
end

@impl true
def handle_info(:__do_process__, state) do
schedule_processing()

state.traces
|> Enum.reverse()
|> Enum.each(fn {_key, %{last_trace: trace, counter: counter}} ->
send(state.target_pid, {:new_trace, %{trace | counter: counter}})
end)

{:noreply, %{state | traces: []}}
end

@impl true
def handle_info({:new_trace, %{function: key} = trace}, %{traces: traces} = state) do
updated_traces =
traces
|> Keyword.get(key)
|> case do
nil ->
Keyword.put(traces, key, %{last_trace: trace, counter: 1})

%{counter: counter} ->
Keyword.put(traces, key, %{last_trace: trace, counter: counter + 1})
end

{:noreply, %{state | traces: updated_traces}}
end

defp schedule_processing() do
Process.send_after(self(), :__do_process__, @interval_ms)
end
end

0 comments on commit f6efc9c

Please sign in to comment.