1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Healthcheck do
7 Module collects metrics about app and assign healthy status.
9 alias Pleroma.Healthcheck
13 defstruct pool_size: 0,
19 @type t :: %__MODULE__{
20 pool_size: non_neg_integer(),
21 active: non_neg_integer(),
22 idle: non_neg_integer(),
23 memory_used: number(),
27 @spec system_info() :: t()
30 memory_used: Float.round(:erlang.memory(:total) / 1024 / 1024, 2)
36 defp assign_db_info(healthcheck) do
37 database = Pleroma.Config.get([Repo, :database])
40 "select state, count(pid) from pg_stat_activity where datname = '#{database}' group by state;"
42 result = Repo.query!(query)
43 pool_size = Pleroma.Config.get([Repo, :pool_size])
46 Enum.reduce(result.rows, %{active: 0, idle: 0}, fn [state, cnt], states ->
47 if state == "active" do
48 Map.put(states, :active, states.active + cnt)
50 Map.put(states, :idle, states.idle + cnt)
53 |> Map.put(:pool_size, pool_size)
55 Map.merge(healthcheck, db_info)
58 @spec check_health(Healthcheck.t()) :: Healthcheck.t()
59 def check_health(%{pool_size: pool_size, active: active} = check)
60 when active >= pool_size do
61 %{check | healthy: false}
64 def check_health(check), do: check