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: cleanup Peep metrics #499

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
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
17 changes: 11 additions & 6 deletions lib/supavisor/monitoring/prom_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule Supavisor.Monitoring.PromEx do
]
end

@spec remove_metrics(S.id()) :: non_neg_integer
@spec remove_metrics(S.id()) :: :ok
def remove_metrics({{type, tenant}, user, mode, db_name, search_path} = id) do
Logger.debug("Removing metrics for #{inspect(id)}")

Expand All @@ -42,11 +42,16 @@ defmodule Supavisor.Monitoring.PromEx do
search_path: search_path
}

Supavisor.Monitoring.PromEx.Metrics
|> :ets.select_delete([
{{{:_, meta}, :_}, [], [true]},
{{{:_, meta, :_}, :_}, [], [true]}
])
{_, tids} = Peep.Persistent.storage(Supavisor.Monitoring.PromEx.Metrics)

tids
|> List.wrap()
|> Enum.each(
&:ets.select_delete(&1, [
{{{:_, meta}, :_}, [], [true]},
{{{:_, meta, :_}, :_}, [], [true]}
])
)
end

@spec set_metrics_tags() :: map()
Expand Down
14 changes: 7 additions & 7 deletions test/integration/proxy_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ defmodule Supavisor.Integration.ProxyTest do
end

test "too many clients in session mode" do
Process.flag(:trap_exit, true)
db_conf = Application.get_env(:supavisor, Repo)
port = Application.get_env(:supavisor, :proxy_port_session)

Expand All @@ -166,8 +165,8 @@ defmodule Supavisor.Integration.ProxyTest do
port: port
)

{:ok, pid1} = single_connection(connection_opts)
{:ok, pid2} = single_connection(connection_opts)
assert {:ok, _} = single_connection(connection_opts)
assert {:ok, _} = single_connection(connection_opts)

:timer.sleep(1000)

Expand All @@ -182,8 +181,6 @@ defmodule Supavisor.Integration.ProxyTest do
pg_code: "XX000"
}
}} = single_connection(connection_opts)

for pid <- [pid1, pid2], do: :gen_statem.stop(pid)
end

test "http to proxy server returns 200 OK" do
Expand Down Expand Up @@ -379,15 +376,18 @@ defmodule Supavisor.Integration.ProxyTest do
defp single_connection(db_conf, c_port \\ nil) when is_list(db_conf) do
port = c_port || db_conf[:port]

[
opts = [
hostname: db_conf[:hostname],
port: port,
database: db_conf[:database],
password: db_conf[:password],
username: db_conf[:username],
pool_size: 1
]
|> SingleConnection.connect()

with {:error, {error, _}} <- start_supervised({SingleConnection, opts}) do
{:error, error}
end
end

defp parse_uri(uri) do
Expand Down
19 changes: 7 additions & 12 deletions test/supavisor/prom_ex_test.exs
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
defmodule Supavisor.PromExTest do
use ExUnit.Case, async: true
use Supavisor.DataCase
use Supavisor.DataCase, async: true

import Supavisor.Asserts

alias Supavisor.Monitoring.PromEx
@subject Supavisor.Monitoring.PromEx

@tenant "prom_tenant"

# These tests are known to be flaky, and while these do not affect users
# directly we can run them independently when needed. In future we probably
# should make them pass "regularly", but for now that is easier to filter them
# out.
@moduletag flaky: true

setup do
db_conf = Application.get_env(:supavisor, Repo)

Expand All @@ -34,12 +27,14 @@ defmodule Supavisor.PromExTest do
end

test "remove tenant tag upon termination", %{proxy: proxy, user: user, db_name: db_name} do
assert PromEx.get_metrics() =~ "tenant=\"#{@tenant}\""
assert @subject.get_metrics() =~ "tenant=\"#{@tenant}\""

:ok = GenServer.stop(proxy)
:ok = Supavisor.stop({{:single, @tenant}, user, :transaction, db_name, nil})

refute_eventually(10, fn -> PromEx.get_metrics() =~ "tenant=\"#{@tenant}\"" end)
Process.sleep(1000)

refute_eventually(10, fn -> @subject.get_metrics() =~ "tenant=\"#{@tenant}\"" end)
end

test "clean_string/1 removes extra spaces from metric string" do
Expand All @@ -49,6 +44,6 @@ defmodule Supavisor.PromExTest do
expected_output =
"db_name=\"postgres\",mode=\"transaction\",tenant=\"dev_tenant\",type=\"single\",user=\"postgres\""

assert expected_output == PromEx.clean_string(input)
assert expected_output == @subject.clean_string(input)
end
end
8 changes: 8 additions & 0 deletions test/support/fixtures/single_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ defmodule SingleConnection do

@behaviour P.SimpleConnection

def child_spec(conf) do
%{
id: {__MODULE__, System.unique_integer()},
start: {__MODULE__, :connect, [conf]},
restart: :temporary
}
end

def connect(conf) do
P.SimpleConnection.start_link(__MODULE__, [], conf)
end
Expand Down
Loading