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

feat: use Telemetry in MetricsCleaner #534

Merged
merged 1 commit into from
Jan 8, 2025
Merged
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
68 changes: 39 additions & 29 deletions lib/supavisor/metrics_cleaner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,32 @@ defmodule Supavisor.MetricsCleaner do

def init(_args) do
Logger.info("Starting MetricsCleaner")

:telemetry.attach(
{__MODULE__, :report},
[:supavisor, :metrics_cleaner, :stop],
&__MODULE__.__report_long_cleanups__/4,
[]
)

{:ok, %{check_ref: check()}}
end

def handle_info(:check, state) do
Process.cancel_timer(state.check_ref)

start = System.monotonic_time(:millisecond)
loop_and_cleanup_metrics_table()
exec_time = System.monotonic_time(:millisecond) - start
@doc false
def __report_long_cleanups__(_event_name, %{duration: duration}, _metadata, _config) do
exec_time = :erlang.convert_time_unit(duration, :native, :millisecond)

if exec_time > :timer.seconds(5),
do: Logger.warning("Metrics check took: #{exec_time} ms")
end

def handle_info(:check, state) do
Process.cancel_timer(state.check_ref)

:telemetry.span([:supavisor, :metrics_cleaner], %{}, fn ->
count = loop_and_cleanup_metrics_table()
{[], %{orphaned_metrics: count}, %{}}
end)

{:noreply, %{state | check_ref: check()}}
end
Expand All @@ -38,32 +52,28 @@ defmodule Supavisor.MetricsCleaner do
metrics_table = Supavisor.Monitoring.PromEx.Metrics
tenant_registry_table = :syn_registry_by_name_tenants

func = fn
{{_,
%{
type: type,
mode: mode,
user: user,
tenant: tenant,
db_name: db,
search_path: search_path
}} = key, _},
_ ->
case :ets.lookup(tenant_registry_table, {{type, tenant}, user, mode, db, search_path}) do
[] ->
Logger.warning("Found orphaned metric: #{inspect(key)}")
:ets.delete(metrics_table, key)

_ ->
nil
end

_, acc ->
acc
func = fn elem, acc ->
with {{_,
%{
type: type,
mode: mode,
user: user,
tenant: tenant,
db_name: db,
search_path: search_path
}} = key, _} <- elem,
[] <- :ets.lookup(tenant_registry_table, {{type, tenant}, user, mode, db, search_path}) do
Logger.warning("Found orphaned metric: #{inspect(key)}")
:ets.delete(metrics_table, key)

acc + 1
else
_ -> acc
end
end

{_, tids} = Peep.Persistent.storage(Supavisor.Monitoring.PromEx.Metrics)

Enum.each(List.wrap(tids), &:ets.foldl(func, nil, &1))
Enum.each(List.wrap(tids), &:ets.foldl(func, 0, &1))
end
end
Loading