-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
manual_metrics_manager.ex
79 lines (62 loc) · 2 KB
/
manual_metrics_manager.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
defmodule PromEx.ManualMetricsManager do
@moduledoc """
This GenServer is responsible to keeping track of all the manual
metrics from your configured plugins. It will fetch metrics initially
when starting (either immediately or after a configured delay). At that
point if you would like to refresh your metrics data points call the
`refresh_metrics/1` function.
"""
use GenServer
require Logger
@doc """
Used to start the `PromEx.ManualMetricsManager` process
"""
@spec start_link(opts :: keyword()) :: GenServer.on_start()
def start_link(opts) do
{name, remaining_opts} = Keyword.pop(opts, :name)
state = Map.new(remaining_opts)
GenServer.start_link(__MODULE__, state, name: name)
end
@doc """
This function calls into the GenServer and refreshes all of the configured
manual metrics data points.
"""
@spec refresh_metrics(prom_ex_module :: module()) :: :ok
def refresh_metrics(prom_ex_module) do
prom_ex_module.__manual_metrics_name__()
|> GenServer.cast(:refresh_metrics)
end
@impl true
def init(%{metrics: []}) do
Logger.debug("No PromEx ManualMetrics have been configured. Stopping PromEx.ManualMetricsManager")
:ignore
end
def init(%{delay_manual_start: :no_delay} = state) do
{:ok, state, {:continue, :refresh_metrics}}
end
def init(%{delay_manual_start: delay_duration} = state) when delay_duration > 0 do
Process.send_after(self(), :refresh_metrics, delay_duration)
{:ok, state}
end
@impl true
def handle_continue(:refresh_metrics, state) do
internal_refresh_metrics(state)
{:noreply, state}
end
@impl true
def handle_info(:refresh_metrics, state) do
internal_refresh_metrics(state)
{:noreply, state}
end
@impl true
def handle_cast(:refresh_metrics, state) do
internal_refresh_metrics(state)
{:noreply, state}
end
defp internal_refresh_metrics(state) do
state.metrics
|> Enum.each(fn {module, function, args} ->
apply(module, function, args)
end)
end
end