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

Fix: Add ETS buffering for telemetry events in TelemetryListener #450

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
23 changes: 21 additions & 2 deletions lib/phoenix/live_dashboard/telemetry_listener.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
defmodule Phoenix.LiveDashboard.TelemetryListener do
# This module is the one responsible for listening to
# telemetry events and sending metrics from the given node.
# This module is responsible for listening to telemetry events and sending metrics from the given node.
@moduledoc false
use GenServer, restart: :temporary

@event_buffer_size 10_000

def listen(node, metrics) do
DynamicSupervisor.start_child(
{Phoenix.LiveDashboard.DynamicSupervisor, node},
Expand All @@ -25,6 +26,10 @@ defmodule Phoenix.LiveDashboard.TelemetryListener do
{index, label, measurement, time}
end

# Added event buffering for high-load situations
:ets.insert(:dashboard_telemetry, {:event, entries})
handle_buffer_overflow()

send(parent, {:telemetry, entries})
end

Expand Down Expand Up @@ -69,6 +74,14 @@ defmodule Phoenix.LiveDashboard.TelemetryListener do
end
end

# Handles buffer overflow by trimming the ETS table when it exceeds the event buffer size
defp handle_buffer_overflow do
if :ets.info(:dashboard_telemetry, :size) > @event_buffer_size do
oldest_event = :ets.first(:dashboard_telemetry)
:ets.delete(:dashboard_telemetry, oldest_event)
end
end

@impl true
def init({parent, metrics}) do
Process.flag(:trap_exit, true)
Expand All @@ -81,6 +94,9 @@ defmodule Phoenix.LiveDashboard.TelemetryListener do
:telemetry.attach(id, event_name, &__MODULE__.handle_metrics/4, {parent, metrics})
end

# Initialize ETS table to store telemetry events
:ets.new(:dashboard_telemetry, [:named_table, :public, :ordered_set])

{:ok, %{ref: ref, events: Map.keys(metrics_per_event)}}
end

Expand All @@ -95,6 +111,9 @@ defmodule Phoenix.LiveDashboard.TelemetryListener do
:telemetry.detach({__MODULE__, event, self()})
end

# Clean up ETS table
:ets.delete(:dashboard_telemetry)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to delete it, the table is automatically GCed if the process terminates.


:ok
end
end