Changelog + remove some unneeded comments from the tests
[akkoma] / lib / pleroma / prometheus_exporter.ex
1 defmodule Pleroma.PrometheusExporter do
2 @moduledoc """
3 Exports metrics in Prometheus format.
4 Mostly exists because of https://github.com/beam-telemetry/telemetry_metrics_prometheus_core/issues/52
5 Basically we need to fetch metrics every so often, or the lib will let them pile up and eventually crash the VM.
6 It also sorta acts as a cache so there is that too.
7 """
8
9 use GenServer
10 require Logger
11
12 def start_link(_opts) do
13 GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
14 end
15
16 def init(_opts) do
17 schedule_next()
18 {:ok, ""}
19 end
20
21 defp schedule_next do
22 Process.send_after(self(), :gather, 60_000)
23 end
24
25 # Scheduled function, gather metrics and schedule next run
26 def handle_info(:gather, _state) do
27 schedule_next()
28 state = TelemetryMetricsPrometheus.Core.scrape()
29 {:noreply, state}
30 end
31
32 # Trigger the call dynamically, mostly for testing
33 def handle_call(:gather, _from, _state) do
34 state = TelemetryMetricsPrometheus.Core.scrape()
35 {:reply, state, state}
36 end
37
38 def handle_call(:show, _from, state) do
39 {:reply, state, state}
40 end
41
42 def show do
43 GenServer.call(__MODULE__, :show)
44 end
45
46 def gather do
47 GenServer.call(__MODULE__, :gather)
48 end
49 end