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.

65 lines
1.7KB

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