Skip to content

Commit 6022d47

Browse files
authored
ft: implement pluggable storage (#240)
Preliminary work for supporting different storages beyond `telemetry_metrics_prometheus_core`.
1 parent 3e269a8 commit 6022d47

3 files changed

Lines changed: 55 additions & 13 deletions

File tree

lib/prom_ex.ex

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ defmodule PromEx do
106106

107107
alias Telemetry.Metrics.{Counter, Distribution, LastValue, Sum, Summary}
108108

109-
alias TelemetryMetricsPrometheus.Core
110-
111109
@type telemetry_metrics() :: Counter.t() | Distribution.t() | LastValue.t() | Sum.t() | Summary.t()
112110
@type measurements_mfa() :: {module(), atom(), list()}
113111

@@ -122,10 +120,9 @@ defmodule PromEx do
122120
@spec get_metrics(prom_ex_module :: module()) :: String.t() | :prom_ex_down
123121
def get_metrics(prom_ex_module) do
124122
prom_ex_process_name = prom_ex_module.__metrics_collector_name__()
123+
store = prom_ex_module.__store__()
125124

126-
if Process.whereis(prom_ex_process_name),
127-
do: Core.scrape(prom_ex_process_name),
128-
else: :prom_ex_down
125+
PromEx.Storage.scrape(store, prom_ex_process_name)
129126
end
130127

131128
@callback init_opts :: PromEx.Config.t()
@@ -147,6 +144,8 @@ defmodule PromEx do
147144
raise "Failed to initialize #{inspect(calling_module)} due to missing :otp_app option"
148145
end
149146

147+
store = Keyword.get(opts, :store, PromEx.Storage.Core)
148+
150149
# Generate process names under calling module namespace
151150
ets_cron_flusher_name = Module.concat([calling_module, ETSCronFlusher])
152151
manual_metrics_name = Module.concat([calling_module, ManualMetricsManager])
@@ -202,7 +201,7 @@ defmodule PromEx do
202201
children =
203202
[]
204203
|> PromEx.ets_cron_flusher_child_spec(__MODULE__, ets_flush_interval, unquote(ets_cron_flusher_name))
205-
|> PromEx.metrics_collector_child_spec(telemetry_metrics, unquote(metrics_collector_name))
204+
|> PromEx.metrics_collector_child_spec(unquote(store), telemetry_metrics, unquote(metrics_collector_name))
206205
|> PromEx.manual_metrics_child_spec(
207206
manual_metrics,
208207
manual_metrics_start_delay,
@@ -334,6 +333,9 @@ defmodule PromEx do
334333
@doc false
335334
def __ets_cron_flusher_name__, do: unquote(ets_cron_flusher_name)
336335

336+
@doc false
337+
def __store__, do: unquote(store)
338+
337339
defoverridable PromEx
338340
end
339341
end
@@ -372,13 +374,8 @@ defmodule PromEx do
372374
end
373375

374376
@doc false
375-
def metrics_collector_child_spec(acc, metrics, process_name) do
376-
spec = {
377-
Core,
378-
name: process_name, metrics: metrics, require_seconds: false, consistent_units: true, start_async: false
379-
}
380-
381-
[spec | acc]
377+
def metrics_collector_child_spec(acc, store, metrics, process_name) do
378+
[PromEx.Storage.child_spec(store, process_name, metrics) | acc]
382379
end
383380

384381
@doc false

lib/prom_ex/storage.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
defmodule PromEx.Storage do
2+
@moduledoc """
3+
Storage definition behaviour.
4+
"""
5+
6+
@doc """
7+
Gather metrics for given collector with given `name`.
8+
"""
9+
@callback scrape(name :: atom()) :: iodata() | :prom_ex_down
10+
11+
@doc """
12+
Define child specs for gatherer process.
13+
"""
14+
@callback child_spec(atom(), Telemetry.Metrics.metrics()) :: Supervisor.child_spec()
15+
16+
def scrape(mod, name), do: mod.scrape(name)
17+
18+
def child_spec(mod, name, metrics),
19+
do: mod.child_spec(name, metrics)
20+
end

lib/prom_ex/storage/core.ex

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule PromEx.Storage.Core do
2+
@behaviour PromEx.Storage
3+
4+
alias TelemetryMetricsPrometheus.Core
5+
6+
@impl true
7+
def scrape(name) do
8+
if Process.whereis(name),
9+
do: Core.scrape(name),
10+
else: :prom_ex_down
11+
end
12+
13+
@impl true
14+
def child_spec(name, metrics) do
15+
opts = [
16+
name: name,
17+
metrics: metrics,
18+
require_seconds: false,
19+
consistent_units: true,
20+
start_async: false
21+
]
22+
23+
Core.child_spec(opts)
24+
end
25+
end

0 commit comments

Comments
 (0)