Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6KB

  1. defmodule Pleroma.Healthcheck do
  2. @moduledoc """
  3. Module collects metrics about app and assign healthy status.
  4. """
  5. alias Pleroma.Healthcheck
  6. alias Pleroma.Repo
  7. defstruct pool_size: 0,
  8. active: 0,
  9. idle: 0,
  10. memory_used: 0,
  11. healthy: true
  12. @type t :: %__MODULE__{
  13. pool_size: non_neg_integer(),
  14. active: non_neg_integer(),
  15. idle: non_neg_integer(),
  16. memory_used: number(),
  17. healthy: boolean()
  18. }
  19. @spec system_info() :: t()
  20. def system_info do
  21. %Healthcheck{
  22. memory_used: Float.round(:erlang.memory(:total) / 1024 / 1024, 2)
  23. }
  24. |> assign_db_info()
  25. |> check_health()
  26. end
  27. defp assign_db_info(healthcheck) do
  28. database = Application.get_env(:pleroma, Repo)[:database]
  29. query =
  30. "select state, count(pid) from pg_stat_activity where datname = '#{database}' group by state;"
  31. result = Repo.query!(query)
  32. pool_size = Application.get_env(:pleroma, Repo)[:pool_size]
  33. db_info =
  34. Enum.reduce(result.rows, %{active: 0, idle: 0}, fn [state, cnt], states ->
  35. if state == "active" do
  36. Map.put(states, :active, states.active + cnt)
  37. else
  38. Map.put(states, :idle, states.idle + cnt)
  39. end
  40. end)
  41. |> Map.put(:pool_size, pool_size)
  42. Map.merge(healthcheck, db_info)
  43. end
  44. @spec check_health(Healthcheck.t()) :: Healthcheck.t()
  45. def check_health(%{pool_size: pool_size, active: active} = check)
  46. when active >= pool_size do
  47. %{check | healthy: false}
  48. end
  49. def check_health(check), do: check
  50. end