Skip to content

Commit

Permalink
ft: add Peep storage (#241)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
hauleth and akoutmos committed Aug 8, 2024
1 parent 6022d47 commit f1f1c44
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
40 changes: 39 additions & 1 deletion lib/prom_ex/metric_types/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ defmodule PromEx.MetricTypes.Event do
- `metrics`: A list of Telemetry Metrics structs that define the metrics.
"""

alias Telemetry.Metrics.Distribution

@type t :: %__MODULE__{
group_name: atom(),
metrics: list(PromEx.telemetry_metrics())
Expand All @@ -25,7 +27,43 @@ defmodule PromEx.MetricTypes.Event do
def build(group_name, metrics) do
%__MODULE__{
group_name: group_name,
metrics: metrics
metrics: build_buckets(group_name, metrics)
}
end

defp build_buckets(name, metrics) do
if Code.ensure_loaded?(Peep.Buckets) do
build_buckets_modules(name, metrics)
else
metrics
end
end

defp build_buckets_modules(name, metrics) do
metrics
|> Enum.with_index()
|> Enum.map(&build_bucket(name, &1))
end

defp build_bucket(name, {%Distribution{} = dist, idx}) do
reporter_options =
Keyword.put_new_lazy(dist.reporter_options, :peep_bucket_calculator, fn ->
buckets = Keyword.fetch!(dist.reporter_options, :buckets)

{:module, name, _, _} =
Module.create(
Module.concat(name, "Bucket_#{idx}"),
quote do
use Peep.Buckets.Custom, buckets: unquote(buckets)
end,
__ENV__
)

name
end)

%Distribution{dist | reporter_options: reporter_options}
end

defp build_bucket(_name, {other, _}), do: other
end
5 changes: 4 additions & 1 deletion lib/prom_ex/storage/core.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# TODO: For version 2.0.0 have all of the supported adapters be
# optional deps.
# if Code.ensure_loaded?(TelemetryMetricsPrometheus.Core) do
defmodule PromEx.Storage.Core do
@behaviour PromEx.Storage

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

Core.child_spec(opts)
end
end
end
22 changes: 22 additions & 0 deletions lib/prom_ex/storage/peep.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
if Code.ensure_loaded?(Peep) do
defmodule PromEx.Storage.Peep do
@behaviour PromEx.Storage

@impl true
def scrape(name) do
Peep.get_all_metrics(name)
|> Peep.Prometheus.export()
|> IO.iodata_to_binary()
end

@impl true
def child_spec(name, metrics) do
opts = [
name: name,
metrics: metrics
]

Peep.child_spec(opts)
end
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ defmodule PromEx.MixProject do
{:telemetry_poller, "~> 1.1"},
{:telemetry_metrics, "~> 1.0"},
{:telemetry_metrics_prometheus_core, "~> 1.2"},
{:peep, "~> 2.0 or ~> 3.0", optional: true},
{:plug_cowboy, ">= 2.6.0"},
{:octo_fetch, "~> 0.4"},

Expand Down
Loading

0 comments on commit f1f1c44

Please sign in to comment.