Page MenuHomePhorge

No OneTemporary

Size
9 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/prometheus/collector.ex b/lib/prometheus/collector.ex
index 19b0be5..75021b8 100644
--- a/lib/prometheus/collector.ex
+++ b/lib/prometheus/collector.ex
@@ -1,83 +1,92 @@
defmodule Prometheus.Collector do
@moduledoc """
A collector for a set of metrics.
Normal users should use `Prometheus.Metric.Gauge`, `Prometheus.Metric.Counter`, `Prometheus.Metric.Summary`
and `Prometheus.Metric.Histogram`.
Implementing `:prometheus_collector` behaviour is for advanced uses such as proxying metrics from another monitoring system.
It is the responsibility of the implementer to ensure produced metrics are valid.
You will be working with Prometheus data model directly (see `Prometheus.Model` ).
Callbacks:
- `collect_mf(registry, callback)` - called by exporters and formats.
Should call `callback` for each `MetricFamily` of this collector;
- `collect_metrics(name, data)` - called by `MetricFamily` constructor.
Should return Metric list for each MetricFamily identified by `name`.
`data` is a term associated with MetricFamily by collect_mf.
- `deregister_cleanup(registry)` - called when collector unregistered by
`registry`. If collector is stateful you can put cleanup code here.
- Example (simplified `:prometheus_vm_memory_collector`):
+ Example (simplified [`:prometheus_vm_memory_collector`](https://github.com/deadtrickster/prometheus.erl/blob/master/doc/prometheus_vm_memory_collector.md)):
```
- defmodule Prometheus.VMMemoryCollector do
- use Prometheus.Collector
-
- def collect_mf(_registry, callback) do
- memory = :erlang.memory()
- callback.(create_gauge(
- :erlang_vm_bytes_total,
- '''
- The total amount of memory currently allocated.
- This is the same as the sum of the memory size
- for processes and system."
- ''',
- memory))
- :ok
- end
-
- def collect_metrics(:erlang_vm_bytes_total, memory) do
- Prometheus.Model.gauge_metrics(
- [
- {[kind: :system], memory.system},
- {[kind: :processes], memory.processes}
- ])
- end
-
- defp create_gauge(name, help, data) do
- Prometheus.Model.create_mf(name, help, :gauge, __MODULE__, data)
- end
- end
+ iex(3)> defmodule Prometheus.VMMemoryCollector do
+ ...(3)> use Prometheus.Collector
+ ...(3)>
+ ...(3)> @labels [:processes, :atom, :binary, :code, :ets]
+ ...(3)>
+ ...(3)> def collect_mf(_registry, callback) do
+ ...(3)> memory = :erlang.memory()
+ ...(3)> callback.(create_gauge(
+ ...(3)> :erlang_vm_bytes_total,
+ ...(3)> "The total amount of memory currently allocated.",
+ ...(3)> memory))
+ ...(3)> :ok
+ ...(3)> end
+ ...(3)>
+ ...(3)> def collect_metrics(:erlang_vm_bytes_total, memory) do
+ ...(3)> Prometheus.Model.gauge_metrics(
+ ...(3)> for label <- @labels do
+ ...(3)> {[type: label], memory[label]}
+ ...(3)> end)
+ ...(3)> end
+ ...(3)>
+ ...(3)> defp create_gauge(name, help, data) do
+ ...(3)> Prometheus.Model.create_mf(name, help, :gauge, __MODULE__, data)
+ ...(3)> end
+ ...(3)> end
+ iex(4)> Prometheus.Registry.register_collector(Prometheus.VMMemoryCollector)
+ :ok
+ iex(5)> r = ~r/# TYPE erlang_vm_bytes_total gauge
+ ...(5)> # HELP erlang_vm_bytes_total
+ ...(5)> The total amount of memory currently allocated.
+ ...(5)> erlang_vm_bytes_total{type=\"processes\"} [1-9]
+ ...(5)> erlang_vm_bytes_total{type=\"atom\"} [1-9]
+ ...(5)> erlang_vm_bytes_total{type=\"binary\"} [1-9]
+ ...(5)> erlang_vm_bytes_total{type=\"code\"} [1-9]
+ ...(5)> erlang_vm_bytes_total{type=\"ets\"} [1-9]/
+ iex(6)> Regex.match?(r, Prometheus.Format.Text.format)
+ true
```
"""
defmacro __using__(_opts) do
quote location: :keep do
@behaviour :prometheus_collector
require Prometheus.Error
require Prometheus.Model
def deregister_cleanup(_registry) do
:ok
end
defoverridable [deregister_cleanup: 1]
end
end
use Prometheus.Erlang, :prometheus_collector
@doc """
Calls `callback` for each MetricFamily of this collector.
"""
defmacro collect_mf(registry \\ :default, collector, callback) do
Erlang.call([registry, collector, callback])
end
end
diff --git a/lib/prometheus/model.ex b/lib/prometheus/model.ex
index a59fb61..5c17e3d 100644
--- a/lib/prometheus/model.ex
+++ b/lib/prometheus/model.ex
@@ -1,145 +1,146 @@
defmodule Prometheus.Model do
@moduledoc """
Helpers for working with Prometheus data model. For advanced users.
- Probably will be used with `Prometheus.Collector`.
+
+ `Prometheus.Collector` example demonstrates how to use this module.
"""
use Prometheus.Erlang, :prometheus_model_helpers
@doc """
Creates Metric Family of `type`, `name` and `help`.
`collector.collect_metrics/2` callback will be called and expected to
return individual metrics list.
"""
defmacro create_mf(name, help, type, collector, collector_data) do
Erlang.call([name, help, type, collector, collector_data])
end
@doc """
Creates gauge metrics from `mdata` {label, value} tuple list.
iex(11)> Prometheus.Model.gauge_metrics([{[host: "example.com"], 100}])
[{:Metric, [{:LabelPair, "host", "example.com"}], {:Gauge, 100}, :undefined,
:undefined, :undefined, :undefined, :undefined}]
"""
defmacro gauge_metrics(mdata) do
Erlang.call([mdata])
end
@doc """
Creates gauge metric with `labels` and `value`.
iex(13)> Prometheus.Model.gauge_metric([host: "example.com"], 100)
{:Metric, [{:LabelPair, "host", "example.com"}], {:Gauge, 100}, :undefined,
:undefined, :undefined, :undefined, :undefined}
"""
defmacro gauge_metric(labels \\ [], value) do
Erlang.call([labels, value])
end
@doc """
Creates untyped metrics from `mdata` {label, value} tuple list.
iex(11)> Prometheus.Model.untyped_metrics([{[host: "example.com"], 100}])
[{:Metric, [{:LabelPair, "host", "example.com"}], :undefined,
:undefined, :undefined, {:Untyped, 100}, :undefined, :undefined}]
"""
defmacro untyped_metrics(mdata) do
Erlang.call([mdata])
end
@doc """
Creates untyped metric with `labels` and `value`.
iex(13)> Prometheus.Model.untyped_metric([host: "example.com"], 100)
{:Metric, [{:LabelPair, "host", "example.com"}], :undefined,
:undefined, :undefined, {:Untyped, 100}, :undefined, :undefined}
"""
defmacro untyped_metric(labels \\ [], value) do
Erlang.call([labels, value])
end
@doc """
Creates counter metrics from `mdata` {labels, value} tuple list.
iex(14)> Prometheus.Model.counter_metrics([{[host: "example.com"], 100}])
[{:Metric, [{:LabelPair, "host", "example.com"}], :undefined, {:Counter, 100},
:undefined, :undefined, :undefined, :undefined}]
"""
defmacro counter_metrics(mdata) do
Erlang.call([mdata])
end
@doc """
Creates counter metric with `labels` and `value`.
iex(15)> Prometheus.Model.counter_metric([host: "example.com"], 100)
{:Metric, [{:LabelPair, "host", "example.com"}], :undefined, {:Counter, 100},
:undefined, :undefined, :undefined, :undefined}
"""
defmacro counter_metric(labels \\ [], value) do
Erlang.call([labels, value])
end
@doc """
Creates summary metrics from `mdata` {labels, count, sum} tuple list.
iex(7)> Prometheus.Model.summary_metrics([{[{:method, :get}], 2, 10.5}])
[{:Metric, [{:LabelPair, "method", "get"}], :undefined, :undefined,
{:Summary, 2, 10.5, []}, :undefined, :undefined, :undefined}]
"""
defmacro summary_metrics(mdata) do
Erlang.call([mdata])
end
@doc """
Creates summary metric with `labels`, `count`, and `sum`.
iex(3)> Prometheus.Model.summary_metric([{:method, :get}], 2, 10.5)
{:Metric, [{:LabelPair, "method", "get"}], :undefined, :undefined,
{:Summary, 2, 10.5, []}, :undefined, :undefined, :undefined}
"""
defmacro summary_metric(labels \\ [], count, sum) do
Erlang.call([labels, count, sum])
end
@doc """
Creates histogram metrics from `mdata` {labels, buckets, count, sum} tuple list.
iex(2)> Prometheus.Model.histogram_metrics([{ [{:method, :get}], [{2, 1}, {5, 1}, {:infinity, 2}], 2, 10.5}])
[{:Metric, [{:LabelPair, "method", "get"}], :undefined, :undefined, :undefined,
:undefined,
{:Histogram, 2, 10.5,
[{:Bucket, 1, 2}, {:Bucket, 1, 5}, {:Bucket, 2, :infinity}]}, :undefined}]
"""
defmacro histogram_metrics(mdata) do
Erlang.call([mdata])
end
@doc """
Creates histogram metric with `labels`, `buckets`, `count`, and `sum`.
iex(4)> Prometheus.Model.histogram_metric([{:method, :get}], [{2, 1}, {5, 1}, {:infinity, 2}], 2, 10.5)
{:Metric, [{:LabelPair, "method", "get"}], :undefined, :undefined, :undefined,
:undefined,
{:Histogram, 2, 10.5,
[{:Bucket, 1, 2}, {:Bucket, 1, 5}, {:Bucket, 2, :infinity}]}, :undefined}
Buckets is a list of pairs {upper_bound, cumulative_count}.
Cumulative count is a sum of all cumulative_counts of previous buckets + counter of current bucket.
"""
defmacro histogram_metric(labels \\ [], buckets, count, sum) do
Erlang.call([labels, buckets, count, sum])
end
end

File Metadata

Mime Type
text/x-diff
Expires
Tue, Jan 20, 9:55 AM (1 d, 12 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
972902
Default Alt Text
(9 KB)

Event Timeline