more salts logging (#5471)

* more salts logging

* even more logs

* wording
This commit is contained in:
ruslandoga 2025-06-04 14:13:29 +03:00 committed by GitHub
parent 595e71a399
commit 0a2ed563dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 18 additions and 3 deletions

View File

@ -9,6 +9,8 @@ defmodule Plausible.Session.Salts do
@impl true @impl true
def init(opts) do def init(opts) do
Process.flag(:trap_exit, true)
name = opts[:name] || __MODULE__ name = opts[:name] || __MODULE__
now = opts[:now] || DateTime.utc_now() now = opts[:now] || DateTime.utc_now()
clean_old_salts(now) clean_old_salts(now)
@ -27,11 +29,9 @@ defmodule Plausible.Session.Salts do
state = state =
case salts do case salts do
[current, prev] -> [current, prev] ->
Logger.warning("[salts] current salt hash: #{:erlang.phash2(current)}")
%{previous: prev, current: current} %{previous: prev, current: current}
[current] -> [current] ->
Logger.warning("[salts] current salt hash: #{:erlang.phash2(current)}")
%{previous: nil, current: current} %{previous: nil, current: current}
[] -> [] ->
@ -39,6 +39,7 @@ defmodule Plausible.Session.Salts do
%{previous: nil, current: new} %{previous: nil, current: new}
end end
log_state("init", state)
true = :ets.insert(name, {:state, state}) true = :ets.insert(name, {:state, state})
{:ok, name} {:ok, name}
end end
@ -58,6 +59,7 @@ defmodule Plausible.Session.Salts do
previous: current previous: current
} }
log_state("rotated", state)
true = :ets.insert(name, {:state, state}) true = :ets.insert(name, {:state, state})
{:reply, :ok, name} {:reply, :ok, name}
end end
@ -70,7 +72,7 @@ defmodule Plausible.Session.Salts do
defp generate_and_persist_new_salt(now) do defp generate_and_persist_new_salt(now) do
salt = :crypto.strong_rand_bytes(16) salt = :crypto.strong_rand_bytes(16)
Logger.warning("[salts] generated salt hash: #{:erlang.phash2(salt)}") Logger.warning("[salts] generated #{:erlang.phash2(salt)}")
Repo.insert_all("salts", [%{salt: salt, inserted_at: now}]) Repo.insert_all("salts", [%{salt: salt, inserted_at: now}])
salt salt
end end
@ -81,4 +83,17 @@ defmodule Plausible.Session.Salts do
Repo.delete_all(from s in "salts", where: s.inserted_at < ^h48_ago) Repo.delete_all(from s in "salts", where: s.inserted_at < ^h48_ago)
end end
@impl true
def terminate(_reason, name) do
log_state("terminating", fetch(name))
end
defp log_state(stage, state) do
%{current: current, previous: previous} = state
Logger.warning(
"[salts] stage=#{stage} current=#{:erlang.phash2(current)} previous=#{:erlang.phash2(previous)}"
)
end
end end