Skip to content

Commit f1f1c44

Browse files
haulethakoutmos
andauthored
ft: add Peep storage (#241)
* ft: implement pluggable storage Preliminary work for supporting different storages beyond `telemetry_metrics_prometheus_core`. * ft: add Peep storage --------- Co-authored-by: Alexander Koutmos <akoutmos@gmail.com>
1 parent 6022d47 commit f1f1c44

5 files changed

Lines changed: 108 additions & 2 deletions

File tree

lib/prom_ex/metric_types/event.ex

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ defmodule PromEx.MetricTypes.Event do
99
- `metrics`: A list of Telemetry Metrics structs that define the metrics.
1010
"""
1111

12+
alias Telemetry.Metrics.Distribution
13+
1214
@type t :: %__MODULE__{
1315
group_name: atom(),
1416
metrics: list(PromEx.telemetry_metrics())
@@ -25,7 +27,43 @@ defmodule PromEx.MetricTypes.Event do
2527
def build(group_name, metrics) do
2628
%__MODULE__{
2729
group_name: group_name,
28-
metrics: metrics
30+
metrics: build_buckets(group_name, metrics)
2931
}
3032
end
33+
34+
defp build_buckets(name, metrics) do
35+
if Code.ensure_loaded?(Peep.Buckets) do
36+
build_buckets_modules(name, metrics)
37+
else
38+
metrics
39+
end
40+
end
41+
42+
defp build_buckets_modules(name, metrics) do
43+
metrics
44+
|> Enum.with_index()
45+
|> Enum.map(&build_bucket(name, &1))
46+
end
47+
48+
defp build_bucket(name, {%Distribution{} = dist, idx}) do
49+
reporter_options =
50+
Keyword.put_new_lazy(dist.reporter_options, :peep_bucket_calculator, fn ->
51+
buckets = Keyword.fetch!(dist.reporter_options, :buckets)
52+
53+
{:module, name, _, _} =
54+
Module.create(
55+
Module.concat(name, "Bucket_#{idx}"),
56+
quote do
57+
use Peep.Buckets.Custom, buckets: unquote(buckets)
58+
end,
59+
__ENV__
60+
)
61+
62+
name
63+
end)
64+
65+
%Distribution{dist | reporter_options: reporter_options}
66+
end
67+
68+
defp build_bucket(_name, {other, _}), do: other
3169
end

lib/prom_ex/storage/core.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# TODO: For version 2.0.0 have all of the supported adapters be
2+
# optional deps.
3+
# if Code.ensure_loaded?(TelemetryMetricsPrometheus.Core) do
14
defmodule PromEx.Storage.Core do
25
@behaviour PromEx.Storage
36

@@ -22,4 +25,4 @@ defmodule PromEx.Storage.Core do
2225

2326
Core.child_spec(opts)
2427
end
25-
end
28+
end

lib/prom_ex/storage/peep.ex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
if Code.ensure_loaded?(Peep) do
2+
defmodule PromEx.Storage.Peep do
3+
@behaviour PromEx.Storage
4+
5+
@impl true
6+
def scrape(name) do
7+
Peep.get_all_metrics(name)
8+
|> Peep.Prometheus.export()
9+
|> IO.iodata_to_binary()
10+
end
11+
12+
@impl true
13+
def child_spec(name, metrics) do
14+
opts = [
15+
name: name,
16+
metrics: metrics
17+
]
18+
19+
Peep.child_spec(opts)
20+
end
21+
end
22+
end

mix.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ defmodule PromEx.MixProject do
6060
{:telemetry_poller, "~> 1.1"},
6161
{:telemetry_metrics, "~> 1.0"},
6262
{:telemetry_metrics_prometheus_core, "~> 1.2"},
63+
{:peep, "~> 2.0 or ~> 3.0", optional: true},
6364
{:plug_cowboy, ">= 2.6.0"},
6465
{:octo_fetch, "~> 0.4"},
6566

0 commit comments

Comments
 (0)