1 defmodule Pleroma.Healthcheck do
3 Module collects metrics about app and assign healthy status.
5 alias Pleroma.Healthcheck
8 defstruct pool_size: 0,
14 @type t :: %__MODULE__{
15 pool_size: non_neg_integer(),
16 active: non_neg_integer(),
17 idle: non_neg_integer(),
18 memory_used: number(),
22 @spec system_info() :: t()
25 memory_used: Float.round(:erlang.memory(:total) / 1024 / 1024, 2)
31 defp assign_db_info(healthcheck) do
32 database = Application.get_env(:pleroma, Repo)[:database]
35 "select state, count(pid) from pg_stat_activity where datname = '#{database}' group by state;"
37 result = Repo.query!(query)
38 pool_size = Application.get_env(:pleroma, Repo)[:pool_size]
41 Enum.reduce(result.rows, %{active: 0, idle: 0}, fn [state, cnt], states ->
42 if state == "active" do
43 Map.put(states, :active, states.active + cnt)
45 Map.put(states, :idle, states.idle + cnt)
48 |> Map.put(:pool_size, pool_size)
50 Map.merge(healthcheck, db_info)
53 @spec check_health(Healthcheck.t()) :: Healthcheck.t()
54 def check_health(%{pool_size: pool_size, active: active} = check)
55 when active >= pool_size do
56 %{check | healthy: false}
59 def check_health(check), do: check