Page MenuHomePhorge

No OneTemporary

Size
126 KB
Referenced Files
None
Subscribers
None
diff --git a/.credo.exs b/.credo.exs
new file mode 100644
index 0000000..37bca19
--- /dev/null
+++ b/.credo.exs
@@ -0,0 +1,64 @@
+%{
+ configs: [
+ %{
+ name: "default",
+ check_for_updates: false,
+ files: %{
+ included: ["lib/", "src/", "web/", "apps/", "test/"],
+ excluded: [~r"/_build/", ~r"/deps/"]
+ },
+ checks: [
+ {Credo.Check.Consistency.ExceptionNames},
+ {Credo.Check.Consistency.LineEndings},
+ {Credo.Check.Consistency.SpaceAroundOperators},
+ {Credo.Check.Consistency.SpaceInParentheses},
+ {Credo.Check.Consistency.TabsOrSpaces},
+
+ {Credo.Check.Design.AliasUsage, false},
+
+ {Credo.Check.Design.DuplicatedCode, excluded_macros: [:schema, :setup, :test]},
+
+ {Credo.Check.Design.TagTODO, exit_status: 0},
+ {Credo.Check.Design.TagFIXME, exit_status: 0},
+
+ {Credo.Check.Readability.FunctionNames},
+ {Credo.Check.Readability.LargeNumbers},
+ {Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 90, exit_status: 0},
+ {Credo.Check.Readability.ModuleAttributeNames},
+ {Credo.Check.Readability.ModuleDoc, false},
+ {Credo.Check.Readability.ModuleNames},
+ {Credo.Check.Readability.ParenthesesInCondition},
+ {Credo.Check.Readability.PredicateFunctionNames},
+ {Credo.Check.Readability.TrailingBlankLine},
+ {Credo.Check.Readability.TrailingWhiteSpace},
+ {Credo.Check.Readability.VariableNames},
+
+ {Credo.Check.Refactor.ABCSize},
+ {Credo.Check.Refactor.CondStatements},
+ {Credo.Check.Refactor.FunctionArity},
+ {Credo.Check.Refactor.MatchInCondition},
+ {Credo.Check.Refactor.PipeChainStart},
+ {Credo.Check.Refactor.CyclomaticComplexity},
+ {Credo.Check.Refactor.NegatedConditionsInUnless},
+ {Credo.Check.Refactor.NegatedConditionsWithElse},
+ {Credo.Check.Refactor.Nesting},
+ {Credo.Check.Refactor.UnlessWithElse},
+
+ {Credo.Check.Warning.IExPry},
+ {Credo.Check.Warning.IoInspect},
+ {Credo.Check.Warning.NameRedeclarationByAssignment},
+ {Credo.Check.Warning.NameRedeclarationByCase},
+ {Credo.Check.Warning.NameRedeclarationByDef},
+ {Credo.Check.Warning.NameRedeclarationByFn},
+ {Credo.Check.Warning.OperationOnSameValues},
+ {Credo.Check.Warning.BoolOperationOnSameValues},
+ {Credo.Check.Warning.UnusedEnumOperation},
+ {Credo.Check.Warning.UnusedKeywordOperation},
+ {Credo.Check.Warning.UnusedListOperation},
+ {Credo.Check.Warning.UnusedStringOperation},
+ {Credo.Check.Warning.UnusedTupleOperation},
+ {Credo.Check.Warning.OperationWithConstantResult},
+ ]
+ }
+ ]
+}
diff --git a/.dir-locals.el b/.dir-locals.el
new file mode 100644
index 0000000..51120c5
--- /dev/null
+++ b/.dir-locals.el
@@ -0,0 +1,4 @@
+((nil . ((indent-tabs-mode . nil)
+ (fill-column . 90)
+ (eval . (turn-on-fci-mode))))
+ )
diff --git a/.gitignore b/.gitignore
index 6e1db0f..6674b25 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,17 +1,19 @@
# The directory Mix will write compiled artifacts to.
/_build
# If you run "mix test --cover", coverage assets end up here.
/cover
# The directory Mix downloads your dependencies sources to.
/deps
# Where 3rd-party dependencies like ExDoc output generated docs.
/doc
# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump
# Also ignore archive artifacts (built via "mix archive.build").
*.ez
+
+.\#*
diff --git a/.travis.yml b/.travis.yml
index 66b945c..c1aa5c7 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,12 +1,12 @@
language: elixir
matrix:
include:
- otp_release: 18.3
elixir: 1.3.2
- otp_release: 19.0
elixir: 1.3.2
sudo: false
before_script:
- mix deps.get --only test
script:
- - "MIX_ENV=test mix coveralls.travis"
+ - "MIX_ENV=test mix credo --strict && MIX_ENV=test mix coveralls.travis"
diff --git a/lib/prometheus.ex b/lib/prometheus.ex
index 2f1ca5e..1445c55 100644
--- a/lib/prometheus.ex
+++ b/lib/prometheus.ex
@@ -1,108 +1,121 @@
defmodule Prometheus do
-
@moduledoc"""
- [Prometheus.io](http://prometheus.io) client library powered by [prometheus.erl](https://hexdocs.pm/prometheus)
+ [Prometheus.io](http://prometheus.io) client library powered by
+ [prometheus.erl](https://hexdocs.pm/prometheus)
- Prometheus.ex is a thin, mostly macro-based wrapper around prometheus.erl. While it's pretty straightforward
- to use prometheus.erl from Elixir, you might prefer prometheus.ex because it gives you:
+ Prometheus.ex is a thin, mostly macro-based wrapper around prometheus.erl.
+ While it's pretty straightforward to use prometheus.erl from Elixir,
+ you might prefer prometheus.ex because it gives you:
- native Elixir syntax;
- native Elixir exceptions;
- - configuration helpers that are really handy if you plan to write your custom instrumenter.
+ - configuration helpers that are really handy if you plan to write your custom
+ instrumenter.
```elixir
defmodule ExampleInstrumenter do
use Prometheus ## require common Prometheus modules, also alias metrics.
def setup do
Histogram.new([name: :http_request_duration_milliseconds,
labels: [:method],
buckets: [100, 300, 500, 750, 1000],
help: "Http Request execution time"])
end
def instrument(%{time: time, method: method}) do
- Histogram.observe([name: :http_request_duration_milliseconds, labels: [method]], time)
+ Histogram.observe([name: :http_request_duration_milliseconds,
+ labels: [method]],
+ time)
end
end
```
## Integrations
- [Ecto Instrumenter](https://hex.pm/packages/prometheus_ecto);
- [Elixir plugs Instrumenters and Exporter](https://hex.pm/packages/prometheus_plugs);
- [Fuse plugin](https://github.com/jlouis/fuse#fuse_stats_prometheus)
- - [OS process info Collector](https://hex.pm/packages/prometheus_process_collector) (linux-only);
+ - [OS process info Collector](https://hex.pm/packages/prometheus_process_collector)
+ (linux-only);
- [Phoenix Instrumenter](https://hex.pm/packages/prometheus_phoenix);
- [RabbitMQ Exporter](https://github.com/deadtrickster/prometheus_rabbitmq_exporter).
## Erlang VM Collectors
- [Memory Collector](vm-memory-collector.html);
- [Statistics Collector](vm-statistics-collector.html);
- [System Information Collector](vm-system-info-collector.html).
## API
API can be grouped like this:
### Standard Metrics & Registry
- - `Prometheus.Metric.Counter` - counter metric, to track counts of events or running totals;
+ - `Prometheus.Metric.Counter` - counter metric, to track counts of events or running
+ totals;
- `Prometheus.Metric.Gauge` - histogram metric, to track distributions of events;
- `Prometheus.Metric.Histogram` - gauge metric, to report instantaneous values;
- `Prometheus.Metric.Summary` - summary metric, to track the size of events;
- `Prometheus.Registry` - working with Prometheus registries.
- All metrics created via `new/1` or `declare/1` macros. The difference is that `new/1` actually wants metric to be
- new and raises `Prometheus.MFAlreadyExistsError` if it isn't.
+ All metrics created via `new/1` or `declare/1` macros. The difference is that `new/1`
+ actually wants metric to be new and raises `Prometheus.MFAlreadyExistsError`
+ if it isn't.
- Both `new/1` and `declare/1` accept options as [Keyword](http://elixir-lang.org/docs/stable/elixir/Keyword.html).
+ Both `new/1` and `declare/1` accept options as
+ [Keyword](http://elixir-lang.org/docs/stable/elixir/Keyword.html).
Common options are:
- name - metric name, can be an atom or a string (required);
- help - metric help, string (required);
- labels - metric labels, label can be an atom or a string (default is []);
- registry - Prometheus registry for the metric, can be any term. (default is :default)
- Histogram also accepts `buckets` option. Please refer to respective modules docs for the more information.
+ Histogram also accepts `buckets` option. Please refer to respective modules docs
+ for the more information.
### General Helpers
- `Prometheus.Buckets` - linear or exponential bucket generators;
- `Prometheus.Contrib.HTTP` - helpers for HTTP instrumenters.
### Integration Helpers
- - `Prometheus.Config` - provides standard configuration mechanism for custom instrumenters/exporters.
+ - `Prometheus.Config` - provides standard configuration mechanism
+ for custom instrumenters/exporters.
### Exposition Formats
- - `Prometheus.Format.Text` - renders metrics for a given registry (default is `:default`) in text format;
- - `Prometheus.Format.Protobuf` - renders metrics for a given registry (default is `:default`) in protobuf v2 format.
+ - `Prometheus.Format.Text` - renders metrics for a given registry
+ (default is `:default`) in text format;
+ - `Prometheus.Format.Protobuf` - renders metrics for a given registry
+ (default is `:default`) in protobuf v2 format.
### Advanced
- You will need this modules only if you're writing custom collector for app/lib that can't be instrumented directly.
+ You will need this modules only if you're writing custom collector for app/lib
+ that can't be instrumented directly.
- `Prometheus.Collector` - exports macros for managing/creating collectors;
- `Prometheus.Model` - provides API for working with underlying Prometheus models.
You'll use that if you want to create custom collector.
"""
defmacro __using__(_opts) do
quote do
require Prometheus.Collector
require Prometheus.Registry
require Prometheus.Buckets
use Prometheus.Metric
require Prometheus.Contrib.HTTP
require Prometheus.Contrib.Mnesia
end
end
end
diff --git a/lib/prometheus/buckets.ex b/lib/prometheus/buckets.ex
index a470805..ff85427 100644
--- a/lib/prometheus/buckets.ex
+++ b/lib/prometheus/buckets.ex
@@ -1,52 +1,53 @@
defmodule Prometheus.Buckets do
@moduledoc """
Histogram buckets generators.
"""
use Prometheus.Erlang, :prometheus_buckets
@doc """
Default histogram buckets:
iex(2)> Prometheus.Buckets.default()
[0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10]
- Please note these buckets are floats and represent seconds so you'll have to use `Prometheus.Metric.Histogram.dobserve/2` or
- configure duration_unit as `:seconds`.
+ Please note these buckets are floats and represent seconds so you'll have to use
+ `Prometheus.Metric.Histogram.dobserve/2` or configure duration_unit as `:seconds`.
"""
defmacro default do
Erlang.call()
end
@doc """
Creates `count` buckets, where the lowest bucket has an
upper bound of `start` and each following bucket's upper bound is `factor`
times the previous bucket's upper bound. The returned list is meant to be
used for the `:buckets` key of histogram constructors options.
iex(2)> Prometheus.Buckets.exponential(100, 1.2, 3)
[100, 120, 144]
The function raises `Prometheus.InvalidValueError` if `count` is 0 or negative,
if `start` is 0 or negative, or if `factor` is less than or equals to 1.
"""
defmacro exponential(start, factor, count) do
Erlang.call([start, factor, count])
end
@doc """
Creates `count` buckets, each `width` wide, where the lowest
bucket has an upper bound of `start`. The returned list is meant to be
used for the `:buckets` key of histogram constructors options.
iex(2)> Prometheus.Buckets.linear(10, 5, 6)
[10, 15, 20, 25, 30, 35]
- The function raises `Prometheus.InvalidValueError` exception if `count` is zero or negative.
+ The function raises `Prometheus.InvalidValueError` exception
+ if `count` is zero or negative.
"""
defmacro linear(start, step, count) do
Erlang.call([start, step, count])
end
end
diff --git a/lib/prometheus/collector.ex b/lib/prometheus/collector.ex
index 75021b8..9071cc0 100644
--- a/lib/prometheus/collector.ex
+++ b/lib/prometheus/collector.ex
@@ -1,92 +1,94 @@
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`
+ 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.
+ 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`](https://github.com/deadtrickster/prometheus.erl/blob/master/doc/prometheus_vm_memory_collector.md)):
```
iex(3)> defmodule Prometheus.VMMemoryCollector do
...(3)> use Prometheus.Collector
- ...(3)>
+ ...(3)>
...(3)> @labels [:processes, :atom, :binary, :code, :ets]
- ...(3)>
+ ...(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)>
...(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)>
...(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)> # 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/config.ex b/lib/prometheus/config.ex
index 8dc6590..cc879a1 100644
--- a/lib/prometheus/config.ex
+++ b/lib/prometheus/config.ex
@@ -1,88 +1,92 @@
defmodule Prometheus.Config do
@moduledoc """
Configuration templates for custom collectors/exporters.
When `use`ed, generates accessor for each configuration option:
iex(4)> defmodule MyInstrumenter do
...(4)> use Prometheus.Config, [:required_option,
...(4)> registry: :default]
...(4)> end
iex(5)> MyInstrumenter.Config.registry(MyInstrumenter)
:default
iex(6)> MyInstrumenter.Config.required_option!(MyInstrumenter)
** (Prometheus.Config.KeyNotFoundError) mandatory option :required_option not found in PrometheusTest.MyInstrumenter instrumenter/collector config
- iex(7)> Application.put_env(:prometheus, MyInstrumenter, [required_option: "Hello world!"])
+ iex(7)> Application.put_env(:prometheus, MyInstrumenter,
+ ...(7)> [required_option: "Hello world!"])
:ok
iex(8)> MyInstrumenter.Config.required_option!(MyInstrumenter)
"Hello world!"
"""
defmodule KeyNotFoundError do
@moduledoc """
Raised when mandatory configuration option not found in app env.
"""
defexception [:option, :key]
def message(%{option: option, key: key}) do
friendly_key_name = String.replace_leading("#{key}", "Elixir.", "")
- "mandatory option :#{option} not found in #{friendly_key_name} instrumenter/collector config"
+ "mandatory option :#{option} not found" <>
+ " in #{friendly_key_name} instrumenter/collector config"
end
end
defmacro __using__(default_config) do
keyword_default_config = Enum.reject(default_config, fn(option) ->
case option do
{_, _} -> false
_ -> true
end
end)
quote do
defmodule Config do
@moduledoc false
def config(key) do
Application.get_env(:prometheus, key, unquote(keyword_default_config))
end
defp config(key, option, default) do
- config(key)
+ key
+ |> config()
|> Keyword.get(option, default)
end
defp config(key, option) do
try do
- config(key)
+ key
+ |> config()
|> Keyword.fetch!(option)
rescue
e in KeyError -> raise %KeyNotFoundError{key: key, option: option}
end
end
unquote do
for config <- default_config do
case config do
{option, default} ->
quote do
def unquote(option)(key) do
config(key, unquote(option), unquote(default))
end
end
option ->
quote do
def unquote(:"#{option}!")(key) do
config(key, unquote(option))
end
end
end
end
end
end
end
end
end
diff --git a/lib/prometheus/contrib/http.ex b/lib/prometheus/contrib/http.ex
index 25d441b..26986d7 100644
--- a/lib/prometheus/contrib/http.ex
+++ b/lib/prometheus/contrib/http.ex
@@ -1,34 +1,34 @@
defmodule Prometheus.Contrib.HTTP do
@moduledoc """
HTTP instrumentation helpers
"""
@doc """
Returns default microseconds buckets for measuring http requests duration:
iex(6)> Prometheus.Contrib.HTTP.microseconds_duration_buckets
[10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 25000, 50000,
100000, 250000, 500000, 1000000, 2500000, 5000000, 10000000]
"""
defmacro microseconds_duration_buckets do
:prometheus_http.microseconds_duration_buckets
end
@doc """
Returns class of the http status code:
iex(7)> Prometheus.Contrib.HTTP.status_class(202)
'success'
Raises `Prometheus.InvalidValueError` exception if `code` is not a positive integer.
"""
defmacro status_class(code) do
quote do
require Prometheus.Error
Prometheus.Error.with_prometheus_error(
- :prometheus_http.status_class(unquote(code))
+ :prometheus_http.status_class(unquote(code))
)
end
end
end
diff --git a/lib/prometheus/erlang.ex b/lib/prometheus/erlang.ex
index 2e7ec5f..d15e2cb 100644
--- a/lib/prometheus/erlang.ex
+++ b/lib/prometheus/erlang.ex
@@ -1,130 +1,135 @@
defmodule Prometheus.Erlang do
@moduledoc false
require Prometheus.Metric
alias Prometheus.Metric
defmacro __using__(erlang_module) do
quote do
@erlang_module unquote(erlang_module)
alias Prometheus.Erlang
end
end
defmacro call(mf \\ false, arguments \\ []) do
{module, function, arguments} = parse_mfa(__CALLER__, mf, arguments)
quote do
Prometheus.Erlang.call_body(unquote(module), unquote(function), unquote(arguments))
end
end
def call_body(module, function, arguments) do
quote do
require Prometheus.Error
Prometheus.Error.with_prometheus_error(
unquote(module).unquote(function)(unquote_splicing(arguments)))
end
end
defmacro metric_call(mf_or_spec, spec \\ false, arguments \\ []) do
{mf, spec, arguments} = parse_metric_call_args(mf_or_spec, spec, arguments)
{module, function, arguments} = parse_mfa(__CALLER__, mf, arguments)
quote do
Prometheus.Erlang.metric_call_body(unquote(module), unquote(function),
unquote(spec), unquote(arguments))
end
end
def metric_call_body(module, function, spec, arguments) do
case spec do
_ when Metric.ct_parsable_spec?(spec) ->
{registry, name, labels} = Prometheus.Metric.parse_spec(spec)
quote do
require Prometheus.Error
Prometheus.Error.with_prometheus_error(
- unquote(module).unquote(function)(unquote(registry), unquote(name), unquote(labels),
- unquote_splicing(arguments)))
+ unquote(module).unquote(function)(unquote(registry), unquote(name),
+ unquote(labels), unquote_splicing(arguments)))
end
_ ->
quote do
require Prometheus.Error
{registry, name, labels} = Metric.parse_spec(unquote(spec))
Prometheus.Error.with_prometheus_error(
unquote(module).unquote(function)(registry, name, labels,
unquote_splicing(arguments)))
end
end
end
defp parse_metric_call_args(mf_or_spec, spec, arguments) do
case mf_or_spec do
- {_,_} -> {mf_or_spec, spec, arguments} ## Erlang.metric_call({:prometheus_counter, :dinc}, spec, [value])
- _ when is_atom(mf_or_spec) -> {mf_or_spec, spec, arguments} ## Erlang.metric_call(:inc, spec, [value])
+ ## Erlang.metric_call({:prometheus_counter, :dinc}, spec, [value])
+ {_,_} -> {mf_or_spec, spec, arguments}
+ ## Erlang.metric_call(:inc, spec, [value])
+ _ when is_atom(mf_or_spec) -> {mf_or_spec, spec, arguments}
_ ->
- [] = arguments ## args are 'shifted' to left
+ ## args are 'shifted' to left
+ [] = arguments
if spec == false do
- {false, mf_or_spec, []} ## only spec is needed, e.g. Erlang.metric_call(spec)
+ ## only spec is needed, e.g. Erlang.metric_call(spec)
+ {false, mf_or_spec, []}
else
- {false, mf_or_spec, spec} ## Erlang.metric_call(spec, [value])
+ ## Erlang.metric_call(spec, [value])
+ {false, mf_or_spec, spec}
end
end
end
defp parse_mfa(__CALLER__, mf, arguments) do
arguments = case mf do
_ when is_list(mf) ->
[] = arguments
mf
_ ->
arguments
end
{module, function} = case mf do
false ->
{f, _arity} = __CALLER__.function
{Module.get_attribute(__CALLER__.module, :erlang_module), f}
_ when is_list(mf) ->
{f, _arity} = __CALLER__.function
{Module.get_attribute(__CALLER__.module, :erlang_module), f}
{_,_} ->
mf
_ when is_atom(mf) ->
{Module.get_attribute(__CALLER__.module, :erlang_module), mf}
end
{module, function, arguments}
end
def ensure_fn(var) do
case var do
[do: block] ->
quote do
fn ->
unquote(block)
end
end
fun ->
quote do
unquote(fun)
end
end
end
end
diff --git a/lib/prometheus/error.ex b/lib/prometheus/error.ex
index 906fee6..501c25c 100644
--- a/lib/prometheus/error.ex
+++ b/lib/prometheus/error.ex
@@ -1,191 +1,195 @@
defmodule Prometheus.InvalidValueError do
@moduledoc """
- Raised when given `value` is invalid i.e. when you pass a negative number to `Prometheus.Metric.Counter.inc/2`.
+ Raised when given `value` is invalid i.e. when you pass a negative number to
+ `Prometheus.Metric.Counter.inc/2`.
"""
defexception [:value, :orig_message]
def message(%{value: value, orig_message: message}) do
"Invalid value: #{value} (#{message})."
end
end
defmodule Prometheus.InvalidMetricNameError do
@moduledoc """
- Raised when given metric `name` is invalid i.e. can't be represented as printable utf-8 string that matches
- `^[a-zA-Z_:][a-zA-Z0-9_:]*$` regular expression.
+ Raised when given metric `name` is invalid i.e. can't be represented as printable utf-8
+ string that matches `^[a-zA-Z_:][a-zA-Z0-9_:]*$` regular expression.
"""
defexception [:name]
def message(%{name: name}) do
"Invalid metric name: #{name}."
end
end
defmodule Prometheus.InvalidMetricLabelsError do
@moduledoc """
Raised when `labels` isn't a list.
"""
defexception [:labels]
def message(%{labels: labels}) do
"Invalid metric labels: #{labels}."
end
end
defmodule Prometheus.InvalidMetricHelpError do
@moduledoc """
Raised when given metric `help` is invalid i.e. isn't a printable utf-8 string.
"""
defexception [:help]
def message(%{help: help}) do
"Invalid metric help: #{help}."
end
end
defmodule Prometheus.InvalidMetricArityError do
@moduledoc """
Raised when metric arity is invalid e.g. counter metric was created with two labels but
only one label value is passed to `Prometheus.Metric.Counter.inc/2`.
"""
defexception [:present, :expected]
def message(%{present: present, expected: expected}) do
"Invalid metric arity: got #{present}, expected #{expected}."
end
end
defmodule Prometheus.UnknownMetricError do
defexception [:registry, :name]
def message(%{registry: registry, name: name}) do
"Unknown metric {registry: #{registry}, name: #{name}}."
end
end
defmodule Prometheus.InvalidLabelNameError do
@moduledoc """
- Raised when label `name` is invalid i.e. can't be represented as printable utf-8 string that matches
- `^[a-zA-Z_][a-zA-Z0-9_]*$` regular expression or starts with `__`.
+ Raised when label `name` is invalid i.e. can't be represented as printable utf-8 string
+ that matches `^[a-zA-Z_][a-zA-Z0-9_]*$` regular expression or starts with `__`.
Metric can impose further restrictions on label names.
"""
defexception [:name, :orig_message]
def message(%{name: name, orig_message: message}) do
"Invalid label name: #{name} (#{message})."
end
end
defmodule Prometheus.MFAlreadyExistsError do
@moduledoc """
Raised when one tries to create metric in `registry` with `name` it already exists.
"""
defexception [:registry, :name]
def message(%{registry: registry, name: name}) do
"Metric #{registry}:#{name} already exists."
end
end
defmodule Prometheus.HistogramNoBucketsError do
@moduledoc """
Raised by histogram constructors when buckets can't be found in spec, or
found value is empty list.
"""
defexception [:buckets]
def message(%{buckets: buckets}) do
"Invalid histogram buckets: #{buckets}."
end
end
defmodule Prometheus.HistogramInvalidBucketsError do
@moduledoc """
Raised by histogram constructors when buckets are invalid i.e. not sorted in increasing
order or generator spec is unknown.
"""
defexception [:buckets, :orig_message]
def message(%{buckets: buckets, orig_message: message}) do
buckets = :io_lib.format("~p", [buckets])
"Invalid histogram buckets: #{buckets} (#{message})."
end
end
defmodule Prometheus.HistogramInvalidBoundError do
@moduledoc """
Raised by histogram constructors when bucket bound isn't a number.
"""
defexception [:bound]
def message(%{bound: bound}) do
"Invalid histogram bound: #{bound}."
end
end
defmodule Prometheus.MissingMetricSpecKeyError do
@moduledoc """
Raised when required metric `spec` `key` is missing. All metrics
require at least `name` and when metric created `help`.
Metrics can have their specific required keys.
"""
defexception [:key, :spec]
def message(%{key: key}) do
"Required key #{key} is missing from metric spec."
end
end
defmodule Prometheus.Error do
@moduledoc false
+ @lint [{Credo.Check.Refactor.ABCSize, false},
+ {Credo.Check.Refactor.CyclomaticComplexity, false}]
def normalize(erlang_error) do
case erlang_error do
%ErlangError{original: original} ->
case original do
{:invalid_value, value, message} ->
%Prometheus.InvalidValueError{value: value, orig_message: message}
{:invalid_metric_name, name, _message} ->
%Prometheus.InvalidMetricNameError{name: name}
{:invalid_metric_help, help, _message} ->
%Prometheus.InvalidMetricHelpError{help: help}
{:invalid_metric_arity, present, expected} ->
%Prometheus.InvalidMetricArityError{present: present, expected: expected}
{:unknown_metric, registry, name} ->
%Prometheus.UnknownMetricError{registry: registry, name: name}
{:invalid_metric_labels, labels, _message} ->
%Prometheus.InvalidMetricLabelsError{labels: labels}
{:invalid_metric_label_name, name, message} ->
%Prometheus.InvalidLabelNameError{name: name, orig_message: message}
{:mf_already_exists, {registry, name}, _message} ->
%Prometheus.MFAlreadyExistsError{registry: registry, name: name}
{:histogram_no_buckets, buckets} ->
%Prometheus.HistogramNoBucketsError{buckets: buckets}
{:histogram_invalid_buckets, buckets, message} ->
- %Prometheus.HistogramInvalidBucketsError{buckets: buckets, orig_message: message}
+ %Prometheus.HistogramInvalidBucketsError{buckets: buckets,
+ orig_message: message}
{:histogram_invalid_bound, bound} ->
%Prometheus.HistogramInvalidBoundError{bound: bound}
{:missing_metric_spec_key, key, spec} ->
%Prometheus.MissingMetricSpecKeyError{key: key, spec: spec}
_ ->
erlang_error
end
_ ->
erlang_error
end
end
defmacro with_prometheus_error(block) do
quote do
try do
unquote(block)
rescue
e in ErlangError -> reraise Prometheus.Error.normalize(e), System.stacktrace
end
end
end
end
diff --git a/lib/prometheus/metric/counter.ex b/lib/prometheus/metric/counter.ex
index 20ede4e..a724b6f 100644
--- a/lib/prometheus/metric/counter.ex
+++ b/lib/prometheus/metric/counter.ex
@@ -1,140 +1,147 @@
defmodule Prometheus.Metric.Counter do
@moduledoc """
Counter is a Metric that represents a single numerical value that only ever
goes up. That implies that it cannot be used to count items whose number can
also go down, e.g. the number of currently running processes. Those
"counters" are represented by `Prometheus.Metric.Gauge`.
A Counter is typically used to count requests served, tasks completed, errors
occurred, etc.
Example use cases for Counters:
- Number of requests processed;
- Number of items that were inserted into a queue;
- Total amount of data that a system has processed.
Use the [`rate()`](https://prometheus.io/docs/querying/functions/#rate())/
[`irate()`](https://prometheus.io/docs/querying/functions/#irate())
functions in Prometheus to calculate the rate of increase of a Counter.
By convention, the names of Counters are suffixed by `_total`.
To create a counter use either `new/1` or `declare/1`, the difference is that
`new/` will raise `Prometheus.MFAlreadyExistsError` exception if counter with
the same `registry`, `name` and `labels` combination already exists.
Both accept `spec` `Keyword` with the same set of keys:
- `:registry` - optional, default is `:default`;
- `:name` - required, can be an atom or a string;
- `:help` - required, must be a string;
- `:labels` - optional, default is `[]`.
Example:
```
defmodule MyServiceInstrumenter do
use Prometheus.Metric
## to be called at app/supervisor startup.
## to tolerate restarts use declare.
def setup() do
Counter.declare([name: :my_service_requests_total,
help: "Requests count.",
labels: [:caller]])
end
def inc(caller) do
Counter.inc([name: :my_service_requests_total,
labels: [caller]])
end
end
```
"""
use Prometheus.Erlang, :prometheus_counter
@doc """
Creates a counter using `spec`.
Raises `Prometheus.MissingMetricSpecKeyError` if required `spec` key is missing.<br>
Raises `Prometheus.InvalidMetricNameError` if metric name is invalid.<br>
Raises `Prometheus.InvalidMetricHelpError` if help is invalid.<br>
Raises `Prometheus.InvalidMetricLabelsError` if labels isn't a list.<br>
Raises `Prometheus.InvalidLabelNameError` if label name is invalid.<br>
- Raises `Prometheus.MFAlreadyExistsError` if a counter with the same `spec` already exists.
+ Raises `Prometheus.MFAlreadyExistsError` if a counter with
+ the same `spec` already exists.
"""
defmacro new(spec) do
Erlang.call([spec])
end
@doc """
Creates a counter using `spec`.
If a counter with the same `spec` exists returns `false`.
Raises `Prometheus.MissingMetricSpecKeyError` if required `spec` key is missing.<br>
Raises `Prometheus.InvalidMetricNameError` if metric name is invalid.<br>
Raises `Prometheus.InvalidMetricHelpError` if help is invalid.<br>
Raises `Prometheus.InvalidMetricLabelsError` if labels isn't a list.<br>
Raises `Prometheus.InvalidLabelNameError` if label name is invalid.
"""
defmacro declare(spec) do
Erlang.call([spec])
end
@doc """
Increments the counter identified by `spec` by `value`.
Raises `Prometheus.InvalidValueError` exception if `value` isn't a positive integer.<br>
- Raises `Prometheus.UnknownMetricError` exception if a counter for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a counter
+ for `spec` can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro inc(spec, value \\ 1) do
Erlang.metric_call(:inc, spec, [value])
end
@doc """
Increments the counter identified by `spec` by `value`.
- If `value` happened to be a float number even one time(!) you shouldn't use `inc/2` after dinc.
+ If `value` happened to be a float number even one time(!) you
+ shouldn't use `inc/2` after dinc.
Raises `Prometheus.InvalidValueError` exception if `value` isn't a positive number.<br>
- Raises `Prometheus.UnknownMetricError` exception if a counter for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a counter
+ for `spec` can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro dinc(spec, value \\ 1) do
Erlang.metric_call({:prometheus_counter, :dinc}, spec, [value])
end
@doc """
Removes counter series identified by spec.
- Raises `Prometheus.UnknownMetricError` exception if a gauge for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a counter
+ for `spec` can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro remove(spec) do
Erlang.metric_call(spec)
end
@doc """
Resets the value of the counter identified by `spec`.
- Raises `Prometheus.UnknownMetricError` exception if a counter for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a counter
+ for `spec` can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro reset(spec) do
Erlang.metric_call(spec)
end
@doc """
Returns the value of the counter identified by `spec`. If there is no counter for
given labels combination, returns `:undefined`.
- Raises `Prometheus.UnknownMetricError` exception if a counter for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a counter
+ for `spec` can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro value(spec) do
Erlang.metric_call({:prometheus_counter, :value}, spec)
end
end
diff --git a/lib/prometheus/metric/gauge.ex b/lib/prometheus/metric/gauge.ex
index ee4d0ed..b059715 100644
--- a/lib/prometheus/metric/gauge.ex
+++ b/lib/prometheus/metric/gauge.ex
@@ -1,204 +1,220 @@
defmodule Prometheus.Metric.Gauge do
@moduledoc """
Gauge metric, to report instantaneous values.
Gauge is a metric that represents a single numerical value that can
arbitrarily go up and down.
A Gauge is typically used for measured values like temperatures or current
memory usage, but also "counts" that can go up and down, like the number of
running processes.
Example use cases for Gauges:
- Inprogress requests;
- Number of items in a queue;
- Free memory;
- Total memory;
- Temperature.
Example:
```
defmodule MyPoolInstrumenter do
use Prometheus.Metric
## to be called at app/supervisor startup.
## to tolerate restarts use declare.
def setup() do
Gauge.declare([name: :my_pool_size,
help: "Pool size."])
Gauge.declare([name: :my_pool_checked_out,
help: "Number of sockets checked out from the pool"])
end
def set_size(size) do
Gauge.set([name: :my_pool_size], size)
end
def track_checked_out_sockets(checkout_fun) do
Gauge.track_inprogress([name: :my_pool_checked_out], checkout_fun)
end
end
```
"""
use Prometheus.Erlang, :prometheus_gauge
@doc """
Creates a gauge using `spec`.
Raises `Prometheus.MissingMetricSpecKeyError` if required `spec` key is missing.<br>
Raises `Prometheus.InvalidMetricNameError` if metric name is invalid.<br>
Raises `Prometheus.InvalidMetricHelpError` if help is invalid.<br>
Raises `Prometheus.InvalidMetricLabelsError` if labels isn't a list.<br>
Raises `Prometheus.InvalidMetricNameError` if label name is invalid.<br>
- Raises `Prometheus.InvalidValueError` exception if duration_unit is unknown or doesn't match metric name.<br>
+ Raises `Prometheus.InvalidValueError` exception if duration_unit is unknown or
+ doesn't match metric name.<br>
Raises `Prometheus.MFAlreadyExistsError` if a gauge with the same `spec` exists.
"""
defmacro new(spec) do
Erlang.call([spec])
end
@doc """
Creates a gauge using `spec`.
If a gauge with the same `spec` exists returns `false`.
Raises `Prometheus.MissingMetricSpecKeyError` if required `spec` key is missing.<br>
Raises `Prometheus.InvalidMetricNameError` if metric name is invalid.<br>
Raises `Prometheus.InvalidMetricHelpError` if help is invalid.<br>
Raises `Prometheus.InvalidMetricLabelsError` if labels isn't a list.<br>
Raises `Prometheus.InvalidMetricNameError` if label name is invalid.<br>
- Raises `Prometheus.InvalidValueError` exception if duration_unit is unknown or doesn't match metric name.
+ Raises `Prometheus.InvalidValueError` exception if duration_unit is unknown or
+ doesn't match metric name.
"""
defmacro declare(spec) do
Erlang.call([spec])
end
@doc """
Sets the gauge identified by `spec` to `value`.
- Raises `Prometheus.InvalidValueError` exception if `value` isn't a number or `:undefined`.<br>
- Raises `Prometheus.UnknownMetricError` exception if a gauge for `spec` can't be found.<br>
+ Raises `Prometheus.InvalidValueError` exception if `value` isn't
+ a number or `:undefined`.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a gauge for `spec`
+ can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro set(spec, value) do
Erlang.metric_call(spec, [value])
end
@doc """
Increments the gauge identified by `spec` by `value`.
Raises `Prometheus.InvalidValueError` exception if `value` isn't an integer.<br>
- Raises `Prometheus.UnknownMetricError` exception if a counter for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a gauge for `spec`
+ can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro inc(spec, value \\ 1) do
Erlang.metric_call(spec, [value])
end
@doc """
Decrements the gauge identified by `spec` by `value`.
Raises `Prometheus.InvalidValueError` exception if `value` isn't an integer.<br>
- Raises `Prometheus.UnknownMetricError` exception if a counter for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a gauge for `spec`
+ can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro dec(spec, value \\ 1) do
Erlang.metric_call(spec, [value])
end
@doc """
Increments the gauge identified by `spec` by `value`.
- If `value` happened to be a float number even one time(!) you shouldn't use `inc/2` or `dec/2` after dinc.
+ If `value` happened to be a float number even one time(!) you shouldn't
+ use `inc/2` or `dec/2` after dinc.
Raises `Prometheus.InvalidValueError` exception if `value` isn't a number.<br>
- Raises `Prometheus.UnknownMetricError` exception if a counter for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a gauge
+ for `spec` can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro dinc(spec, value \\ 1) do
Erlang.metric_call(spec, [value])
end
@doc """
Decrements the gauge identified by `spec` by `value`.
- If `value` happened to be a float number even one time(!) you shouldn't use `inc/2` or `dec/2` after ddec.
+ If `value` happened to be a float number even one time(!) you shouldn't
+ use `inc/2` or `dec/2` after ddec.
Raises `Prometheus.InvalidValueError` exception if `value` isn't a number.<br>
- Raises `Prometheus.UnknownMetricError` exception if a counter for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a gauge
+ for `spec` can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro ddec(spec, value \\ 1) do
Erlang.metric_call(spec, [value])
end
@doc """
Sets the gauge identified by `spec` to the current unixtime.
- Raises `Prometheus.UnknownMetricError` exception if a gauge for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a gauge
+ for `spec` can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro set_to_current_time(spec) do
Erlang.metric_call(spec)
end
@doc """
Sets the gauge identified by `spec` to the number of currently executing `fun`s.
- Raises `Prometheus.UnknownMetricError` exception if a gauge for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a gauge
+ for `spec` can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
Raises `Prometheus.InvalidValueError` exception if fun isn't a function or block.
"""
defmacro track_inprogress(spec, fun) do
Erlang.metric_call(spec, [Erlang.ensure_fn(fun)])
end
@doc """
Tracks the amount of time spent executing `fun`.
- Raises `Prometheus.UnknownMetricError` exception if a gauge for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a gauge
+ for `spec` can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
Raises `Prometheus.InvalidValueError` exception if `fun` isn't a function or block.
"""
defmacro set_duration(spec, fun) do
Erlang.metric_call(spec, [Erlang.ensure_fn(fun)])
end
@doc """
Removes gauge series identified by spec.
- Raises `Prometheus.UnknownMetricError` exception if a gauge for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a gauge
+ for `spec` can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro remove(spec) do
Erlang.metric_call(spec)
end
@doc """
Resets the value of the gauge identified by `spec`.
- Raises `Prometheus.UnknownMetricError` exception if a gauge for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a gauge
+ for `spec` can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro reset(spec) do
Erlang.metric_call(spec)
end
@doc """
Returns the value of the gauge identified by `spec`.
If duration unit set, value will be converted to the duration unit.
[Read more here.](time.html)
- Raises `Prometheus.UnknownMetricError` exception if a gauge for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a gauge
+ for `spec` can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro value(spec) do
Erlang.metric_call(spec)
end
end
diff --git a/lib/prometheus/metric/histogram.ex b/lib/prometheus/metric/histogram.ex
index f560f80..f5b8ba0 100644
--- a/lib/prometheus/metric/histogram.ex
+++ b/lib/prometheus/metric/histogram.ex
@@ -1,153 +1,169 @@
defmodule Prometheus.Metric.Histogram do
@moduledoc """
A Histogram tracks the size and number of events in buckets.
You can use Histograms for aggregatable calculation of quantiles.
Example use cases for Histograms:
- Response latency;
- Request size.
Histogram expects `buckets` key in a metric spec. Buckets can be:
- a list of numbers in increasing order;
- one of the generate specs (shortcuts for `Prometheus.Buckets` macros)
- :default;
- {:linear, start, step, count};
- {:exponential, start, step, count}.
Example:
```
defmodule ExampleInstrumenter do
use Prometheus.Metric
## to be called at app/supervisor startup.
## to tolerate restarts use declare.
def setup do
Histogram.new([name: :http_request_duration_milliseconds,
labels: [:method],
buckets: [100, 300, 500, 750, 1000],
help: "Http Request execution time."])
end
def instrument(%{time: time, method: method}) do
- Histogram.observe([name: :http_request_duration_milliseconds, labels: [method]], time)
+ Histogram.observe([name: :http_request_duration_milliseconds, labels: [method]],
+ time)
end
end
```
"""
use Prometheus.Erlang, :prometheus_histogram
@doc """
Creates a histogram using `spec`.
Histogram cannot have a label named "le".
Raises `Prometheus.MissingMetricSpecKeyError` if required `spec` key is missing.<br>
Raises `Prometheus.InvalidMetricNameError` if metric name is invalid.<br>
Raises `Prometheus.InvalidMetricHelpError` if help is invalid.<br>
Raises `Prometheus.InvalidMetricLabelsError` if labels isn't a list.<br>
Raises `Prometheus.InvalidMetricNameError` if label name is invalid.<br>
- Raises `Prometheus.InvalidValueError` exception if duration_unit is unknown or doesn't match metric name.<br>
+ Raises `Prometheus.InvalidValueError` exception if duration_unit is unknown or
+ doesn't match metric name.<br>
Raises `Prometheus.MFAlreadyExistsError` if a histogram with the same `spec` exists.
Histogram-specific exceptions:
- Raises `Prometheus.HistogramNoBucketsError` if buckets are missing, not a list, empty list or not known buckets spec.<br>
- Raises `Prometheus.HistogramInvalidBucketsError` if buckets aren't in increasing order.<br>
+ Raises `Prometheus.HistogramNoBucketsError` if buckets are missing, not a list,
+ empty list or not known buckets spec.<br>
+ Raises `Prometheus.HistogramInvalidBucketsError` if buckets aren't
+ in increasing order.<br>
Raises `Prometheus.HistogramInvalidBoundError` if bucket bound isn't a number.
"""
defmacro new(spec) do
Erlang.call([spec])
end
@doc """
Creates a histogram using `spec`.
Histogram cannot have a label named "le".
If a histogram with the same `spec` exists returns `false`.
Raises `Prometheus.MissingMetricSpecKeyError` if required `spec` key is missing.<br>
Raises `Prometheus.InvalidMetricNameError` if metric name is invalid.<br>
Raises `Prometheus.InvalidMetricHelpError` if help is invalid.<br>
Raises `Prometheus.InvalidMetricLabelsError` if labels isn't a list.<br>
Raises `Prometheus.InvalidMetricNameError` if label name is invalid.<br>
- Raises `Prometheus.InvalidValueError` exception if duration_unit is unknown or doesn't match metric name.
+ Raises `Prometheus.InvalidValueError` exception if duration_unit is unknown or
+ doesn't match metric name.
Histogram-specific exceptions:
- Raises `Prometheus.HistogramNoBucketsError` if buckets are missing, not a list, empty list or not known buckets spec.<br>
- Raises `Prometheus.HistogramInvalidBucketsError` if buckets aren't in increasing order.<br>
+ Raises `Prometheus.HistogramNoBucketsError` if buckets are missing, not a list,
+ empty list or not known buckets spec.<br>
+ Raises `Prometheus.HistogramInvalidBucketsError` if buckets aren't
+ in increasing order.<br>
Raises `Prometheus.HistogramInvalidBoundError` if bucket bound isn't a number.
"""
defmacro declare(spec) do
Erlang.call([spec])
end
@doc """
Observes the given amount.
- Raises `Prometheus.InvalidValueError` exception if `amount` isn't a positive integer.<br>
- Raises `Prometheus.UnknownMetricError` exception if a histogram for `spec` can't be found.<br>
+ Raises `Prometheus.InvalidValueError` exception if `amount` isn't
+ a positive integer.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a histogram for `spec`
+ can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro observe(spec, amount \\ 1) do
Erlang.metric_call(spec, [amount])
end
@doc """
Observes the given amount.
- If `amount` happened to be a float number even one time(!) you shouldn't use `observe/2` after dobserve.
+ If `amount` happened to be a float number even one time(!) you shouldn't
+ use `observe/2` after dobserve.
- Raises `Prometheus.InvalidValueError` exception if `amount` isn't a positive integer.<br>
- Raises `Prometheus.UnknownMetricError` exception if a histogram for `spec` can't be found.<br>
+ Raises `Prometheus.InvalidValueError` exception if `amount` isn't
+ a positive integer.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a histogram for `spec`
+ can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro dobserve(spec, amount \\ 1) do
Erlang.metric_call(spec, [amount])
end
@doc """
Observes the amount of seconds spent executing `fun`.
- Raises `Prometheus.UnknownMetricError` exception if a histogram for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a histogram for `spec`
+ can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
Raises `Prometheus.InvalidValueError` exception if fun isn't a function or block.
"""
defmacro observe_duration(spec, fun) do
Erlang.metric_call(spec, [Erlang.ensure_fn(fun)])
end
@doc """
Removes histogram series identified by spec.
- Raises `Prometheus.UnknownMetricError` exception if a gauge for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a histogram for `spec`
+ can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro remove(spec) do
Erlang.metric_call(spec)
end
@doc """
Resets the value of the histogram identified by `spec`.
- Raises `Prometheus.UnknownMetricError` exception if a histogram for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a histogram for `spec`
+ can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro reset(spec) do
Erlang.metric_call(spec)
end
@doc """
Returns the value of the histogram identified by `spec`. If there is no histogram for
given labels combination, returns `:undefined`.
- Raises `Prometheus.UnknownMetricError` exception if a histogram for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a histogram for `spec`
+ can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro value(spec) do
Erlang.metric_call(spec)
end
end
diff --git a/lib/prometheus/metric/summary.ex b/lib/prometheus/metric/summary.ex
index 75bb9bc..6e215a1 100644
--- a/lib/prometheus/metric/summary.ex
+++ b/lib/prometheus/metric/summary.ex
@@ -1,141 +1,151 @@
defmodule Prometheus.Metric.Summary do
@moduledoc """
Summary metric, to track the size of events.
Example use cases for Summaries:
- Response latency;
- Request size;
- Response size.
Example:
```
defmodule MyProxyInstrumenter do
use Prometheus.Metric
## to be called at app/supervisor startup.
## to tolerate restarts use declare.
def setup() do
Summary.declare([name: :request_size_bytes,
help: "Request size in bytes."])
Summary.declare([name: :response_size_bytes,
help: "Response size in bytes."])
end
def observe_request(size) do
Summary.observe([name: :request_size_bytes], size)
end
def observe_response(size) do
Summary.observe([name: :response_size_bytes], size)
end
end
```
"""
use Prometheus.Erlang, :prometheus_summary
@doc """
Creates a summary using `spec`.
Summary cannot have a label named "quantile".
Raises `Prometheus.MissingMetricSpecKeyError` if required `spec` key is missing.<br>
Raises `Prometheus.InvalidMetricNameError` if metric name is invalid.<br>
Raises `Prometheus.InvalidMetricHelpError` if help is invalid.<br>
Raises `Prometheus.InvalidMetricLabelsError` if labels isn't a list.<br>
Raises `Prometheus.InvalidMetricNameError` if label name is invalid.<br>
- Raises `Prometheus.InvalidValueError` exception if duration_unit is unknown or doesn't match metric name.<br>
- Raises `Prometheus.MFAlreadyExistsError` if a summary with the same `spec` already exists.
+ Raises `Prometheus.InvalidValueError` exception if duration_unit is unknown or
+ doesn't match metric name.<br>
+ Raises `Prometheus.MFAlreadyExistsError` if a summary with the same `spec`
+ already exists.
"""
defmacro new(spec) do
Erlang.call([spec])
end
@doc """
Creates a summary using `spec`.
Summary cannot have a label named "quantile".
If a summary with the same `spec` exists returns `false`.
Raises `Prometheus.MissingMetricSpecKeyError` if required `spec` key is missing.<br>
Raises `Prometheus.InvalidMetricNameError` if metric name is invalid.<br>
Raises `Prometheus.InvalidMetricHelpError` if help is invalid.<br>
Raises `Prometheus.InvalidMetricLabelsError` if labels isn't a list.<br>
- Raises `Prometheus.InvalidMetricNameError` if label name is invalid;<br>
- Raises `Prometheus.InvalidValueError` exception if duration_unit is unknown or doesn't match metric name.
+ Raises `Prometheus.InvalidMetricNameError` if label name is invalid;<br>
+ Raises `Prometheus.InvalidValueError` exception if duration_unit is unknown or
+ doesn't match metric name.
"""
defmacro declare(spec) do
Erlang.call([spec])
end
@doc """
Observes the given amount.
Raises `Prometheus.InvalidValueError` exception if `amount` isn't an integer.<br>
- Raises `Prometheus.UnknownMetricError` exception if a summary for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a summary for `spec`
+ can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro observe(spec, amount \\ 1) do
Erlang.metric_call(spec, [amount])
end
@doc """
Observes the given amount.
- If `amount` happened to be a float number even one time(!) you shouldn't use `observe/2` after dobserve.
+ If `amount` happened to be a float number even one time(!) you shouldn't use `observe/2`
+ after dobserve.
Raises `Prometheus.InvalidValueError` exception if `amount` isn't a number.<br>
- Raises `Prometheus.UnknownMetricError` exception if a summary for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a summary for `spec`
+ can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro dobserve(spec, amount \\ 1) do
Erlang.metric_call(spec, [amount])
end
@doc """
Observes the amount of seconds spent executing `fun`.
- Raises `Prometheus.UnknownMetricError` exception if a summary for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a summary for `spec`
+ can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
Raises `Prometheus.InvalidValueError` exception if `fun` isn't a function or block.
"""
defmacro observe_duration(spec, fun) do
Erlang.metric_call(spec, [Erlang.ensure_fn(fun)])
end
@doc """
Removes summary series identified by spec.
- Raises `Prometheus.UnknownMetricError` exception if a gauge for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a summary for `spec`
+ can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro remove(spec) do
Erlang.metric_call(spec)
end
@doc """
Resets the value of the summary identified by `spec`.
- Raises `Prometheus.UnknownMetricError` exception if a summary for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a summary for `spec`
+ can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro reset(spec) do
Erlang.metric_call(spec)
end
@doc """
Returns the value of the summary identified by `spec`. If there is no summary for
given labels combination, returns `:undefined`.
If duration unit set, sum will be converted to the duration unit.
[Read more here.](time.html)
- Raises `Prometheus.UnknownMetricError` exception if a summary for `spec` can't be found.<br>
+ Raises `Prometheus.UnknownMetricError` exception if a summary for `spec`
+ can't be found.<br>
Raises `Prometheus.InvalidMetricArityError` exception if labels count mismatch.
"""
defmacro value(spec) do
Erlang.metric_call(spec)
end
end
diff --git a/lib/prometheus/model.ex b/lib/prometheus/model.ex
index 5c17e3d..75637ea 100644
--- a/lib/prometheus/model.ex
+++ b/lib/prometheus/model.ex
@@ -1,146 +1,151 @@
defmodule Prometheus.Model do
@moduledoc """
Helpers for working with Prometheus data model. For advanced users.
-
+
`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}])
+ iex(2)> Prometheus.Model.histogram_metrics([{[{:method, :get}],
+ ...(2)> [{2, 1}, {5, 1}, {:infinity, 2}],
+ ...(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)
+ iex(4)> Prometheus.Model.histogram_metric([{:method, :get}],
+ ...(4)> [{2, 1}, {5, 1}, {:infinity, 2}],
+ ...(4)> 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.
+ 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
diff --git a/mix.exs b/mix.exs
index e929c3c..99f17d2 100644
--- a/mix.exs
+++ b/mix.exs
@@ -1,56 +1,57 @@
defmodule PrometheusEx.Mixfile do
use Mix.Project
@version "1.1.0"
def project do
[app: :prometheus_ex,
version: @version,
elixir: "~> 1.3",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
description: description,
package: package,
name: "Prometheus.ex",
deps: deps(),
test_coverage: [tool: ExCoveralls],
preferred_cli_env: ["coveralls": :test, "coveralls.html": :test],
docs: [main: Prometheus,
source_ref: "v#{@version}",
source_url: "https://github.com/deadtrickster/prometheus.ex",
extras: ["pages/Mnesia Collector.md",
"pages/VM Memory Collector.md",
"pages/VM Statistics Collector.md",
"pages/VM System Info Collector.md",
"pages/Time.md"]]]
end
def application do
[applications: [:logger,
:prometheus]]
end
defp description do
"""
Elixir-friendly Prometheus monitoring system client.
"""
end
defp package do
[maintainers: ["Ilya Khaprov"],
licenses: ["MIT"],
links: %{"GitHub" => "https://github.com/deadtrickster/prometheus.ex",
"Prometheus.erl" => "https://hex.pm/packages/prometheus",
"Ecto Instrumenter" => "https://hex.pm/packages/prometheus_ecto",
"Phoenix Instrumenter" => "https://hex.pm/packages/prometheus_phoenix",
"Plugs Instrumenter/Exporter" => "https://hex.pm/packages/prometheus_plugs",
"Process info Collector" => "https://hex.pm/packages/prometheus_process_collector"}]
end
defp deps do
[{:prometheus, "~> 3.1"},
+ {:credo, git: "https://github.com/rrrene/credo", only: [:dev, :test]},
{:excoveralls, "~> 0.5.6", only: :test},
{:ex_doc, "~> 0.11", only: :dev},
{:earmark, ">= 0.0.0", only: :dev}]
end
end
diff --git a/mix.lock b/mix.lock
index 4c0b79f..c213297 100644
--- a/mix.lock
+++ b/mix.lock
@@ -1,12 +1,14 @@
-%{"certifi": {:hex, :certifi, "0.4.0", "a7966efb868b179023618d29a407548f70c52466bf1849b9e8ebd0e34b7ea11f", [:rebar3], []},
+%{"bunt": {:hex, :bunt, "0.1.6", "5d95a6882f73f3b9969fdfd1953798046664e6f77ec4e486e6fafc7caad97c6f", [:mix], []},
+ "certifi": {:hex, :certifi, "0.4.0", "a7966efb868b179023618d29a407548f70c52466bf1849b9e8ebd0e34b7ea11f", [:rebar3], []},
+ "credo": {:git, "https://github.com/rrrene/credo", "075dc2f81539ee7e901ac33e4bb1ff7b2142c12b", []},
"earmark": {:hex, :earmark, "1.0.2", "a0b0904d74ecc14da8bd2e6e0248e1a409a2bc91aade75fcf428125603de3853", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.14.3", "e61cec6cf9731d7d23d254266ab06ac1decbb7651c3d1568402ec535d387b6f7", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]},
"excoveralls": {:hex, :excoveralls, "0.5.7", "5d26e4a7cdf08294217594a1b0643636accc2ad30e984d62f1d166f70629ff50", [:mix], [{:exjsx, "~> 3.0", [hex: :exjsx, optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, optional: false]}]},
"exjsx": {:hex, :exjsx, "3.2.1", "1bc5bf1e4fd249104178f0885030bcd75a4526f4d2a1e976f4b428d347614f0f", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, optional: false]}]},
"hackney": {:hex, :hackney, "1.6.1", "ddd22d42db2b50e6a155439c8811b8f6df61a4395de10509714ad2751c6da817", [:rebar3], [{:certifi, "0.4.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.0", [hex: :ssl_verify_fun, optional: false]}]},
"idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [:rebar3], []},
"jsx": {:hex, :jsx, "2.8.0", "749bec6d205c694ae1786d62cea6cc45a390437e24835fd16d12d74f07097727", [:mix, :rebar], []},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"prometheus": {:hex, :prometheus, "3.1.0", "32a148a6c906d8d31113cad5cb07d4b61dec3f77c6f6fe2ce837d1ff62e9a049", [:rebar3], []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.0", "edee20847c42e379bf91261db474ffbe373f8acb56e9079acb6038d4e0bf414f", [:rebar, :make], []}}
diff --git a/test/buckets_test.exs b/test/buckets_test.exs
index d62b01f..656bf25 100644
--- a/test/buckets_test.exs
+++ b/test/buckets_test.exs
@@ -1,33 +1,34 @@
defmodule Prometheus.BucketsTest do
use ExUnit.Case
use Prometheus
-
+
doctest Prometheus.Buckets
test "linear buckets generator tests" do
assert_raise Prometheus.InvalidValueError, fn ->
Prometheus.Buckets.linear(-15, 5, 0)
end
assert [-15, -10, -5, 0, 5, 10] == Prometheus.Buckets.linear(-15, 5, 6)
end
test "exponential buckets generator tests" do
assert_raise Prometheus.InvalidValueError, fn ->
Prometheus.Buckets.exponential(-15, 5, 0)
end
assert_raise Prometheus.InvalidValueError, fn ->
Prometheus.Buckets.exponential(-15, 5, 2)
end
assert_raise Prometheus.InvalidValueError, fn ->
Prometheus.Buckets.exponential(15, 0.5, 3)
end
assert [100, 120, 144] == Prometheus.Buckets.exponential(100, 1.2, 3)
end
test "default buckets test" do
- assert [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10] == Prometheus.Buckets.default
+ assert [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10] ==
+ Prometheus.Buckets.default
end
end
diff --git a/test/contrib/http_test.exs b/test/contrib/http_test.exs
index 3736b9e..fdcb7b0 100644
--- a/test/contrib/http_test.exs
+++ b/test/contrib/http_test.exs
@@ -1,23 +1,24 @@
defmodule Prometheus.Contrib.HTTPTest do
use ExUnit.Case
## alias Prometheus.Error %% FIXME: status_class should throw invalid_value.
use Prometheus
-
+
doctest Prometheus.Contrib.HTTP
test "microseconds_duration_buckets" do
- assert [10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 25000, 50000,
- 100000, 250000, 500000, 1000000, 2500000, 5000000, 10000000] == Prometheus.Contrib.HTTP.microseconds_duration_buckets
+ assert [10, 25, 50, 100, 250, 500, 1_000, 2_500, 5_000, 10_000, 25_000, 50_000,
+ 100_000, 250_000, 500_000, 1_000_000, 2_500_000, 5_000_000, 10_000_000] ==
+ Prometheus.Contrib.HTTP.microseconds_duration_buckets
end
test "status_class" do
assert 'unknown' == Prometheus.Contrib.HTTP.status_class(50)
assert 'informational' == Prometheus.Contrib.HTTP.status_class(150)
assert 'success' == Prometheus.Contrib.HTTP.status_class(250)
assert 'redirection' == Prometheus.Contrib.HTTP.status_class(350)
assert 'client-error' == Prometheus.Contrib.HTTP.status_class(450)
assert 'server-error' == Prometheus.Contrib.HTTP.status_class(550)
assert 'unknown' == Prometheus.Contrib.HTTP.status_class(650)
end
end
diff --git a/test/format/protobuf_test.exs b/test/format/protobuf_test.exs
index 4916cd6..9b9959e 100644
--- a/test/format/protobuf_test.exs
+++ b/test/format/protobuf_test.exs
@@ -1,116 +1,125 @@
defmodule Prometheus.Format.ProtobufTest do
use Prometheus.Case
require Prometheus.Format.Protobuf
test "content_type" do
assert "application/vnd.google.protobuf; " <>
"proto=io.prometheus.client.MetricFamily; " <>
"encoding=delimited" == Prometheus.Format.Protobuf.content_type
end
test "gauge" do
Gauge.new([name: :pool_size, help: "MongoDB Connections pool size"])
Gauge.set([name: :pool_size], 365)
assert <<57,10,9,112,111,111,108,95,115,105,122,101,18,29,77,111,
110,103,111,68,66,32,67,111,110,110,101,99,116,105,111,
110,115,32,112,111,111,108,32,115,105,122,101,24,1,34,
11,18,9,9,0,0,0,0,0,208,118,64>> == Prometheus.Format.Protobuf.format()
end
test "counter" do
Counter.new([name: :http_requests_total, help: "Http request count"])
Counter.inc([name: :http_requests_total])
assert <<56,10,19,104,116,116,112,95,114,101,113,117,101,115,116,
115,95,116,111,116,97,108,18,18,72,116,116,112,32,114,
101,113,117,101,115,116,32,99,111,117,110,116,24,0,34,
11,26,9,9,0,0,0,0,0,0,240,63>> == Prometheus.Format.Protobuf.format()
end
test "dcounter" do
Counter.new([name: :dtest, help: "qw\"\\e"])
Counter.dinc([name: :dtest], 1.5)
Counter.dinc([name: :dtest], 3.5)
Counter.dinc([name: :dtest], 1.5)
:timer.sleep(10)
assert <<29,10,5,100,116,101,115,116,18,5,113,119,34,92,101,24,0,
34,11,26,9,9,0,0,0,0,0,0,26,64>> == Prometheus.Format.Protobuf.format()
end
test "summary" do
Summary.new([name: :orders_summary, help: "Track orders count/total sum"])
Summary.observe([name: :orders_summary], 10)
Summary.observe([name: :orders_summary], 15)
assert <<63,10,14,111,114,100,101,114,115,95,115,117,109,109,97,
114,121,18,28,84,114,97,99,107,32,111,114,100,101,114,
115,32,99,111,117,110,116,47,116,111,116,97,108,32,115,
- 117,109,24,2,34,13,34,11,8,2,17,0,0,0,0,0,0,57,64>> == Prometheus.Format.Protobuf.format()
+ 117,109,24,2,34,13,34,11,8,2,17,0,0,0,0,0,0,57,64>> ==
+ Prometheus.Format.Protobuf.format()
end
test "dsummary" do
Summary.new([name: :dsummary, help: "qwe"])
Summary.dobserve([name: :dsummary], 1.5)
Summary.dobserve([name: :dsummary], 2.7)
:timer.sleep(10)
assert <<32,10,8,100,115,117,109,109,97,114,121,18,3,113,119,101,
- 24,2,34,13,34,11,8,2,17,205,204,204,204,204,204,16,64>> == Prometheus.Format.Protobuf.format()
+ 24,2,34,13,34,11,8,2,17,205,204,204,204,204,204,16,64>> ==
+ Prometheus.Format.Protobuf.format()
end
test "histogram" do
Histogram.new([name: :http_request_duration_milliseconds,
labels: [:method],
buckets: [100, 300, 500, 750, 1000],
help: "Http Request execution time",
duration_unit: false])
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 95)
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 100)
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 102)
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 150)
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 250)
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 75)
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 350)
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 550)
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 950)
assert <<175,1,10,34,104,116,116,112,95,114,101,113,117,101,115,
116,95,100,117,114,97,116,105,111,110,95,109,105,108,
108,105,115,101,99,111,110,100,115,18,27,72,116,116,112,
32,82,101,113,117,101,115,116,32,101,120,101,99,117,116,
105,111,110,32,116,105,109,101,24,4,34,106,10,13,10,6,
109,101,116,104,111,100,18,3,103,101,116,58,89,8,9,17,0,
0,0,0,0,124,164,64,26,11,8,3,17,0,0,0,0,0,0,89,64,26,11,
8,6,17,0,0,0,0,0,192,114,64,26,11,8,7,17,0,0,0,0,0,64,
127,64,26,11,8,8,17,0,0,0,0,0,112,135,64,26,11,8,9,17,0,
- 0,0,0,0,64,143,64,26,11,8,9,17,0,0,0,0,0,0,240,127>> == Prometheus.Format.Protobuf.format()
+ 0,0,0,0,64,143,64,26,11,8,9,17,0,0,0,0,0,0,240,127>> ==
+ Prometheus.Format.Protobuf.format()
end
test "dhistogram" do
Histogram.new([name: :http_request_duration_milliseconds,
labels: [:method],
buckets: [100, 300, 500, 750, 1000],
help: "Http Request execution time",
duration_unit: false])
- Histogram.dobserve([name: :http_request_duration_milliseconds, labels: [:post]], 500.2)
- Histogram.dobserve([name: :http_request_duration_milliseconds, labels: [:post]], 150.4)
- Histogram.dobserve([name: :http_request_duration_milliseconds, labels: [:post]], 450.5)
- Histogram.dobserve([name: :http_request_duration_milliseconds, labels: [:post]], 850.3)
- Histogram.dobserve([name: :http_request_duration_milliseconds, labels: [:post]], 750.9)
- Histogram.dobserve([name: :http_request_duration_milliseconds, labels: [:post]], 1650.23)
+ Histogram.dobserve(
+ [name: :http_request_duration_milliseconds, labels: [:post]], 500.2)
+ Histogram.dobserve(
+ [name: :http_request_duration_milliseconds, labels: [:post]], 150.4)
+ Histogram.dobserve(
+ [name: :http_request_duration_milliseconds, labels: [:post]], 450.5)
+ Histogram.dobserve(
+ [name: :http_request_duration_milliseconds, labels: [:post]], 850.3)
+ Histogram.dobserve(
+ [name: :http_request_duration_milliseconds, labels: [:post]], 750.9)
+ Histogram.dobserve(
+ [name: :http_request_duration_milliseconds, labels: [:post]], 1650.23)
:timer.sleep(10)
assert <<176,1,10,34,104,116,116,112,95,114,101,113,117,101,115,
116,95,100,117,114,97,116,105,111,110,95,109,105,108,
108,105,115,101,99,111,110,100,115,18,27,72,116,116,112,
32,82,101,113,117,101,115,116,32,101,120,101,99,117,116,
105,111,110,32,116,105,109,101,24,4,34,107,10,14,10,6,
109,101,116,104,111,100,18,4,112,111,115,116,58,89,8,6,
17,225,122,20,174,135,0,177,64,26,11,8,0,17,0,0,0,0,0,0,
89,64,26,11,8,1,17,0,0,0,0,0,192,114,64,26,11,8,2,17,0,
0,0,0,0,64,127,64,26,11,8,3,17,0,0,0,0,0,112,135,64,26,
11,8,5,17,0,0,0,0,0,64,143,64,26,11,8,6,17,0,0,0,0,0,0,
240,127>> == Prometheus.Format.Protobuf.format()
end
end
diff --git a/test/format/text_test.exs b/test/format/text_test.exs
index 4b1a55d..823718e 100644
--- a/test/format/text_test.exs
+++ b/test/format/text_test.exs
@@ -1,136 +1,142 @@
defmodule Prometheus.Format.TextTest do
use Prometheus.Case
require Prometheus.Format.Text
test "content_type" do
assert "text/plain; version=0.0.4" == Prometheus.Format.Text.content_type
end
test "gauge" do
Gauge.new([name: :pool_size, help: "MongoDB Connections pool size"])
Gauge.set([name: :pool_size], 365)
assert ~s"""
# TYPE pool_size gauge
# HELP pool_size MongoDB Connections pool size
pool_size 365
"""
== Prometheus.Format.Text.format()
end
test "counter" do
Counter.new([name: :http_requests_total, help: "Http request count"])
Counter.inc([name: :http_requests_total])
assert ~s"""
# TYPE http_requests_total counter
# HELP http_requests_total Http request count
http_requests_total 1
""" == Prometheus.Format.Text.format()
end
test "dcounter" do
Counter.new([name: :dtest, help: "qw\"\\e"])
Counter.dinc([name: :dtest], 1.5)
Counter.dinc([name: :dtest], 3.5)
Counter.dinc([name: :dtest], 1.5)
:timer.sleep(10)
assert ~s"""
# TYPE dtest counter
# HELP dtest qw\"\\\\e
dtest 6.5
""" == Prometheus.Format.Text.format()
end
test "summary" do
Summary.new([name: :orders_summary, help: "Track orders count/total sum"])
Summary.observe([name: :orders_summary], 10)
Summary.observe([name: :orders_summary], 15)
assert ~s"""
# TYPE orders_summary summary
# HELP orders_summary Track orders count/total sum
orders_summary_count 2
orders_summary_sum 25
""" == Prometheus.Format.Text.format()
end
test "dsummary" do
Summary.new([name: :dsummary, help: "qwe"])
Summary.dobserve([name: :dsummary], 1.5)
Summary.dobserve([name: :dsummary], 2.7)
:timer.sleep(10)
assert ~s"""
# TYPE dsummary summary
# HELP dsummary qwe
dsummary_count 2
dsummary_sum 4.2
""" == Prometheus.Format.Text.format()
end
test "histogram" do
Histogram.new([name: :http_request_duration_milliseconds,
labels: [:method],
buckets: [100, 300, 500, 750, 1000],
help: "Http Request execution time",
duration_unit: false])
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 95)
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 100)
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 102)
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 150)
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 250)
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 75)
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 350)
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 550)
Histogram.observe([name: :http_request_duration_milliseconds, labels: [:get]], 950)
assert ~s"""
# TYPE http_request_duration_milliseconds histogram
# HELP http_request_duration_milliseconds Http Request execution time
http_request_duration_milliseconds_bucket{method="get",le="100"} 3
http_request_duration_milliseconds_bucket{method="get",le="300"} 6
http_request_duration_milliseconds_bucket{method="get",le="500"} 7
http_request_duration_milliseconds_bucket{method="get",le="750"} 8
http_request_duration_milliseconds_bucket{method="get",le="1000"} 9
http_request_duration_milliseconds_bucket{method="get",le="+Inf"} 9
http_request_duration_milliseconds_count{method="get"} 9
http_request_duration_milliseconds_sum{method="get"} 2622
""" == Prometheus.Format.Text.format()
end
test "dhistogram" do
Histogram.new([name: :http_request_duration_milliseconds,
labels: [:method],
buckets: [100, 300, 500, 750, 1000],
help: "Http Request execution time",
duration_unit: false])
- Histogram.dobserve([name: :http_request_duration_milliseconds, labels: [:post]], 500.2)
- Histogram.dobserve([name: :http_request_duration_milliseconds, labels: [:post]], 150.4)
- Histogram.dobserve([name: :http_request_duration_milliseconds, labels: [:post]], 450.5)
- Histogram.dobserve([name: :http_request_duration_milliseconds, labels: [:post]], 850.3)
- Histogram.dobserve([name: :http_request_duration_milliseconds, labels: [:post]], 750.9)
- Histogram.dobserve([name: :http_request_duration_milliseconds, labels: [:post]], 1650.23)
+ Histogram.dobserve(
+ [name: :http_request_duration_milliseconds, labels: [:post]], 500.2)
+ Histogram.dobserve(
+ [name: :http_request_duration_milliseconds, labels: [:post]], 150.4)
+ Histogram.dobserve(
+ [name: :http_request_duration_milliseconds, labels: [:post]], 450.5)
+ Histogram.dobserve(
+ [name: :http_request_duration_milliseconds, labels: [:post]], 850.3)
+ Histogram.dobserve(
+ [name: :http_request_duration_milliseconds, labels: [:post]], 750.9)
+ Histogram.dobserve(
+ [name: :http_request_duration_milliseconds, labels: [:post]], 1650.23)
:timer.sleep(10)
assert ~s"""
# TYPE http_request_duration_milliseconds histogram
# HELP http_request_duration_milliseconds Http Request execution time
http_request_duration_milliseconds_bucket{method=\"post\",le=\"100\"} 0
http_request_duration_milliseconds_bucket{method=\"post\",le=\"300\"} 1
http_request_duration_milliseconds_bucket{method=\"post\",le=\"500\"} 2
http_request_duration_milliseconds_bucket{method=\"post\",le=\"750\"} 3
http_request_duration_milliseconds_bucket{method=\"post\",le=\"1000\"} 5
http_request_duration_milliseconds_bucket{method=\"post\",le=\"+Inf\"} 6
http_request_duration_milliseconds_count{method=\"post\"} 6
http_request_duration_milliseconds_sum{method=\"post\"} 4352.53
""" == Prometheus.Format.Text.format()
end
end
diff --git a/test/metric/counter_test.exs b/test/metric/counter_test.exs
index cbcd1bd..44dd0b4 100644
--- a/test/metric/counter_test.exs
+++ b/test/metric/counter_test.exs
@@ -1,209 +1,209 @@
defmodule Prometheus.CounterTest do
use Prometheus.Case
test "registration" do
spec = [name: :name,
help: "",
registry: :qwe]
assert true == Counter.declare(spec)
assert false == Counter.declare(spec)
assert_raise Prometheus.MFAlreadyExistsError,
"Metric qwe:name already exists.",
fn ->
Counter.new(spec)
end
end
test "spec errors" do
assert_raise Prometheus.MissingMetricSpecKeyError,
"Required key name is missing from metric spec.",
fn ->
Counter.new([help: ""])
end
assert_raise Prometheus.InvalidMetricNameError,
"Invalid metric name: 12.",
fn ->
Counter.new([name: 12, help: ""])
end
assert_raise Prometheus.InvalidMetricLabelsError,
"Invalid metric labels: 12.",
fn ->
Counter.new([name: "qwe", labels: 12, help: ""])
end
assert_raise Prometheus.InvalidMetricHelpError,
"Invalid metric help: 12.",
fn ->
Counter.new([name: "qwe", help: 12])
end
end
test "counter specific errors" do
spec = [name: :http_requests_total,
help: ""]
## inc
assert_raise Prometheus.InvalidValueError,
"Invalid value: -1 (inc accepts only non-negative integers).",
fn ->
Counter.inc(spec, -1)
end
assert_raise Prometheus.InvalidValueError,
"Invalid value: 1.5 (inc accepts only non-negative integers).",
fn ->
Counter.inc(spec, 1.5)
end
assert_raise Prometheus.InvalidValueError,
"Invalid value: qwe (inc accepts only non-negative integers).",
fn ->
Counter.inc(spec, "qwe")
end
## dinc
assert_raise Prometheus.InvalidValueError,
"Invalid value: -1 (dinc accepts only non-negative numbers).",
fn ->
Counter.dinc(spec, -1)
end
assert_raise Prometheus.InvalidValueError,
"Invalid value: qwe (dinc accepts only non-negative numbers).",
fn ->
Counter.dinc(spec, "qwe")
end
end
test "mf/arity errors" do
spec = [name: :metric_with_label,
labels: [:label],
help: ""]
Counter.declare(spec)
## inc
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Counter.inc(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Counter.inc([name: :metric_with_label, labels: [:l1, :l2]])
end
## dinc
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Counter.dinc(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Counter.dinc([name: :metric_with_label, labels: [:l1, :l2]])
end
## remove
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Counter.remove(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Counter.remove([name: :metric_with_label, labels: [:l1, :l2]])
end
## reset
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Counter.reset(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Counter.reset([name: :metric_with_label, labels: [:l1, :l2]])
end
## value
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Counter.value(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Counter.value([name: :metric_with_label, labels: [:l1, :l2]])
end
end
test "inc" do
spec = [name: :http_requests_total,
labels: [:method],
help: ""]
Counter.new(spec)
Counter.inc(spec)
Counter.inc(spec, 3)
assert 4 == Counter.value(spec)
Counter.reset(spec)
assert 0 == Counter.value(spec)
end
test "dinc" do
spec = [name: :http_requests_total,
help: ""]
Counter.new(spec)
Counter.dinc(spec)
Counter.dinc(spec, 3.5)
- ## dinc is async so lets make sure gen_server processed our increment request
+ ## dinc is async. let's make sure gen_server processed our request
Process.sleep(10)
assert 4.5 == Counter.value(spec)
Counter.reset(spec)
assert 0 == Counter.value(spec)
end
test "undefined value" do
spec = [name: :http_requests_total,
labels: [:method],
help: ""]
Counter.new(spec)
assert :undefined == Counter.value(spec)
end
test "remove" do
spec = [name: :http_requests_total,
labels: [:method],
help: ""]
wl_spec = [name: :simple_counter,
help: ""]
Counter.new(spec)
Counter.new(wl_spec)
Counter.inc(spec)
Counter.inc(wl_spec)
assert 1 == Counter.value(spec)
assert 1 == Counter.value(wl_spec)
assert true == Counter.remove(spec)
assert true == Counter.remove(wl_spec)
assert :undefined == Counter.value(spec)
assert :undefined == Counter.value(wl_spec)
assert false == Counter.remove(spec)
assert false == Counter.remove(wl_spec)
end
end
diff --git a/test/metric/gauge_test.exs b/test/metric/gauge_test.exs
index 2d869db..273a1ec 100644
--- a/test/metric/gauge_test.exs
+++ b/test/metric/gauge_test.exs
@@ -1,451 +1,451 @@
defmodule Prometheus.GaugeTest do
use Prometheus.Case
test "registration" do
spec = [name: :name,
help: "",
registry: :qwe]
assert true == Gauge.declare(spec)
assert false == Gauge.declare(spec)
assert_raise Prometheus.MFAlreadyExistsError,
"Metric qwe:name already exists.",
fn ->
Gauge.new(spec)
end
end
test "spec errors" do
assert_raise Prometheus.MissingMetricSpecKeyError,
"Required key name is missing from metric spec.",
fn ->
Gauge.new([help: ""])
end
assert_raise Prometheus.InvalidMetricNameError,
"Invalid metric name: 12.",
fn ->
Gauge.new([name: 12, help: ""])
end
assert_raise Prometheus.InvalidMetricLabelsError,
"Invalid metric labels: 12.",
fn ->
Gauge.new([name: "qwe", labels: 12, help: ""])
end
assert_raise Prometheus.InvalidMetricHelpError,
"Invalid metric help: 12.",
fn ->
Gauge.new([name: "qwe", help: 12])
end
end
test "gauge specific errors" do
spec = [name: :http_requests_total,
help: ""]
## set
assert_raise Prometheus.InvalidValueError,
"Invalid value: qwe (set accepts only numbers).",
fn ->
Gauge.set(spec, "qwe")
end
## inc
assert_raise Prometheus.InvalidValueError,
"Invalid value: qwe (inc accepts only integers).",
fn ->
Gauge.inc(spec, "qwe")
end
assert_raise Prometheus.InvalidValueError,
"Invalid value: -1.5 (inc accepts only integers).",
fn ->
Gauge.inc(spec, -1.5)
end
## dec
assert_raise Prometheus.InvalidValueError,
"Invalid value: qwe (dec accepts only integers).",
fn ->
Gauge.dec(spec, "qwe")
end
assert_raise Prometheus.InvalidValueError,
"Invalid value: -1.5 (dec accepts only integers).",
fn ->
Gauge.dec(spec, -1.5)
end
## dinc
assert_raise Prometheus.InvalidValueError,
"Invalid value: qwe (dinc accepts only numbers).",
fn ->
Gauge.dinc(spec, "qwe")
end
## ddec
assert_raise Prometheus.InvalidValueError,
"Invalid value: qwe (ddec accepts only numbers).",
fn ->
Gauge.ddec(spec, "qwe")
end
## track_inprogress
assert_raise Prometheus.InvalidValueError,
"Invalid value: qwe (track_inprogress accepts only functions).",
fn ->
Gauge.track_inprogress(spec, "qwe")
end
## set_duration
assert_raise Prometheus.InvalidValueError,
"Invalid value: qwe (set_duration accepts only functions).",
fn ->
Gauge.set_duration(spec, "qwe")
end
end
test "mf/arity errors" do
spec = [name: :metric_with_label,
labels: [:label],
help: ""]
Gauge.declare(spec)
## set
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Gauge.set(:unknown_metric, 1)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Gauge.set([name: :metric_with_label, labels: [:l1, :l2]], 1)
end
## inc
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Gauge.inc(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Gauge.inc([name: :metric_with_label, labels: [:l1, :l2]])
end
## dinc
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Gauge.dinc(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Gauge.dinc([name: :metric_with_label, labels: [:l1, :l2]])
end
## dec
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Gauge.dec(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Gauge.dec([name: :metric_with_label, labels: [:l1, :l2]])
end
## ddec
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Gauge.ddec(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Gauge.ddec([name: :metric_with_label, labels: [:l1, :l2]])
end
## set_to_current_time
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Gauge.set_to_current_time(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Gauge.set_to_current_time([name: :metric_with_label, labels: [:l1, :l2]])
end
## track_inprogress
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Gauge.track_inprogress(:unknown_metric, fn -> 1 end)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Gauge.track_inprogress([name: :metric_with_label, labels: [:l1, :l2]], fn -> 1 end)
end
## set_duration
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Gauge.set_duration(:unknown_metric, fn -> 1 end)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Gauge.set_duration([name: :metric_with_label, labels: [:l1, :l2]], fn -> 1 end)
end
## remove
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Gauge.remove(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Gauge.remove([name: :metric_with_label, labels: [:l1, :l2]])
end
## reset
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Gauge.reset(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Gauge.reset([name: :metric_with_label, labels: [:l1, :l2]])
end
## value
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Gauge.value(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Gauge.value([name: :metric_with_label, labels: [:l1, :l2]])
end
end
test "set" do
spec = [name: :metric_with_label,
labels: [:label],
help: ""]
Gauge.declare(spec)
Gauge.set(spec, 100)
assert 100 == Gauge.value(spec)
Gauge.set(spec, 105)
assert 105 == Gauge.value(spec)
Gauge.reset(spec)
assert 0 == Gauge.value(spec)
end
test "inc" do
spec = [name: :http_requests_total,
labels: [:method],
help: ""]
Gauge.new(spec)
Gauge.inc(spec)
Gauge.inc(spec, 3)
assert 4 == Gauge.value(spec)
Gauge.reset(spec)
assert 0 == Gauge.value(spec)
end
test "dinc" do
spec = [name: :http_requests_total,
help: ""]
Gauge.new(spec)
Gauge.dinc(spec)
Gauge.dinc(spec, 3.5)
- ## dinc is async so lets make sure gen_server processed our increment request
+ ## dinc is async. let's make sure gen_server processed our request
Process.sleep(10)
assert 4.5 == Gauge.value(spec)
Gauge.reset(spec)
assert 0 == Gauge.value(spec)
end
test "dec" do
spec = [name: :http_requests_total,
labels: [:method],
help: ""]
Gauge.new(spec)
Gauge.dec(spec)
Gauge.dec(spec, 3)
assert -4 == Gauge.value(spec)
Gauge.reset(spec)
assert 0 == Gauge.value(spec)
end
test "ddec" do
spec = [name: :http_requests_total,
help: ""]
Gauge.new(spec)
Gauge.ddec(spec)
Gauge.ddec(spec, 3.5)
- ## ddec is async so lets make sure gen_server processed our increment request
+ ## ddec is async. let's make sure gen_server processed our request
Process.sleep(10)
assert -4.5 == Gauge.value(spec)
Gauge.reset(spec)
assert 0 == Gauge.value(spec)
end
test "set_to_current_time" do
spec = [name: :http_requests_total,
labels: [:method],
help: ""]
Gauge.new(spec)
Gauge.set_to_current_time(spec)
assert :os.system_time(:seconds) == Gauge.value(spec)
end
test "test_track_inprogress fn" do
spec = [name: :http_requests_total,
labels: [:method],
help: ""]
Gauge.new(spec)
assert 1 == Gauge.track_inprogress(spec, fn ->
Gauge.value(spec)
end)
assert_raise ErlangError, fn ->
Gauge.track_inprogress(spec, fn ->
:erlang.error({:qwe})
end)
end
assert 0 = Gauge.value(spec)
end
test "test_track_inprogress block" do
spec = [name: :http_requests_total,
labels: [:method],
help: ""]
Gauge.new(spec)
assert 1 == Gauge.track_inprogress(spec, do: Gauge.value(spec))
assert_raise ErlangError, fn ->
Gauge.track_inprogress spec do
:erlang.error({:qwe})
end
end
assert 0 = Gauge.value(spec)
end
test "set_duration fn" do
spec = [name: :http_requests_total,
labels: [:method],
help: "",
duration_unit: :seconds]
Gauge.new(spec)
assert 1 == Gauge.set_duration(spec, fn ->
Process.sleep(1000)
1
end)
assert 1 < Gauge.value(spec) and Gauge.value(spec) < 1.2
assert_raise ErlangError, fn ->
Gauge.set_duration(spec, fn ->
:erlang.error({:qwe})
end)
end
-
+
assert 0.0 < Gauge.value(spec) and Gauge.value(spec) < 0.2
end
test "set_duration block" do
spec = [name: :http_requests_total,
labels: [:method],
help: "",
duration_unit: :seconds]
Gauge.new(spec)
assert :ok == Gauge.set_duration(spec, do: Process.sleep(1000))
assert 1 < Gauge.value(spec) and Gauge.value(spec) < 1.2
assert_raise ErlangError, fn ->
Gauge.set_duration spec do
:erlang.error({:qwe})
end
end
assert 0.0 < Gauge.value(spec) and Gauge.value(spec) < 0.2
end
test "undefined value" do
spec = [name: :http_requests_total,
labels: [:method],
help: ""]
Gauge.new(spec)
assert :undefined == Gauge.value(spec)
end
test "remove" do
spec = [name: :http_requests_total,
labels: [:method],
help: ""]
wl_spec = [name: :simple_gauge,
help: ""]
Gauge.new(spec)
Gauge.new(wl_spec)
Gauge.inc(spec)
Gauge.inc(wl_spec)
assert 1 == Gauge.value(spec)
assert 1 == Gauge.value(wl_spec)
assert true == Gauge.remove(spec)
assert true == Gauge.remove(wl_spec)
assert :undefined == Gauge.value(spec)
assert :undefined == Gauge.value(wl_spec)
assert false == Gauge.remove(spec)
assert false == Gauge.remove(wl_spec)
end
end
diff --git a/test/metric/histogram_test.exs b/test/metric/histogram_test.exs
index 535cbd7..8fc0d8b 100644
--- a/test/metric/histogram_test.exs
+++ b/test/metric/histogram_test.exs
@@ -1,306 +1,307 @@
defmodule Prometheus.HistogramTest do
use Prometheus.Case
test "registration" do
spec = [name: :name,
help: "",
registry: :qwe]
assert true == Histogram.declare(spec)
assert false == Histogram.declare(spec)
assert_raise Prometheus.MFAlreadyExistsError,
"Metric qwe:name already exists.",
fn ->
Histogram.new(spec)
end
end
test "spec errors" do
assert_raise Prometheus.MissingMetricSpecKeyError,
"Required key name is missing from metric spec.",
fn ->
Histogram.new([help: ""])
end
assert_raise Prometheus.InvalidMetricNameError,
"Invalid metric name: 12.",
fn ->
Histogram.new([name: 12, help: ""])
end
assert_raise Prometheus.InvalidMetricLabelsError,
"Invalid metric labels: 12.",
fn ->
Histogram.new([name: "qwe", labels: 12, help: ""])
end
assert_raise Prometheus.InvalidMetricHelpError,
"Invalid metric help: 12.",
fn ->
Histogram.new([name: "qwe", help: 12])
end
assert_raise Prometheus.InvalidLabelNameError,
"Invalid label name: le (histogram cannot have a label named \"le\").",
fn ->
Histogram.new([name: "qwe", help: "", labels: ["le"]])
end
## buckets
assert_raise Prometheus.HistogramNoBucketsError,
"Invalid histogram buckets: .",
fn ->
Histogram.new([name: "qwe", help: "", buckets: []])
end
assert_raise Prometheus.HistogramNoBucketsError,
"Invalid histogram buckets: undefined.",
fn ->
Histogram.new([name: "qwe", help: "", buckets: :undefined])
end
assert_raise Prometheus.HistogramInvalidBucketsError,
"Invalid histogram buckets: 1 (not a list).",
fn ->
Histogram.new([name: "qwe", help: "", buckets: 1])
end
assert_raise Prometheus.HistogramInvalidBucketsError,
"Invalid histogram buckets: [1,3,2] (buckets not sorted).",
fn ->
Histogram.new([name: "qwe", help: "", buckets: [1, 3, 2]])
end
assert_raise Prometheus.HistogramInvalidBoundError,
"Invalid histogram bound: qwe.",
fn ->
Histogram.new([name: "qwe", help: "", buckets: ["qwe"]])
end
end
test "histogram specific errors" do
spec = [name: :http_requests_total,
help: ""]
## observe
assert_raise Prometheus.InvalidValueError,
"Invalid value: qwe (observe accepts only integers).",
fn ->
Histogram.observe(spec, "qwe")
end
assert_raise Prometheus.InvalidValueError,
"Invalid value: 1.5 (observe accepts only integers).",
fn ->
Histogram.observe(spec, 1.5)
end
## dobserve
assert_raise Prometheus.InvalidValueError,
"Invalid value: qwe (dobserve accepts only numbers).",
fn ->
Histogram.dobserve(spec, "qwe")
end
## observe_duration
assert_raise Prometheus.InvalidValueError,
"Invalid value: qwe (observe_duration accepts only functions).",
fn ->
Histogram.observe_duration(spec, "qwe")
end
end
test "mf/arity errors" do
spec = [name: :metric_with_label,
labels: [:label],
help: ""]
Histogram.declare(spec)
## observe
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Histogram.observe(:unknown_metric, 1)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Histogram.observe([name: :metric_with_label, labels: [:l1, :l2]], 1)
end
## dobserve
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Histogram.dobserve(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Histogram.dobserve([name: :metric_with_label, labels: [:l1, :l2]])
end
## observe_duration
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Histogram.observe_duration(:unknown_metric, fn -> 1 end)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
- Histogram.observe_duration([name: :metric_with_label, labels: [:l1, :l2]], fn -> 1 end)
+ Histogram.observe_duration(
+ [name: :metric_with_label, labels: [:l1, :l2]], fn -> 1 end)
end
## remove
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Histogram.remove(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Histogram.remove([name: :metric_with_label, labels: [:l1, :l2]])
end
## reset
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Histogram.reset(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Histogram.reset([name: :metric_with_label, labels: [:l1, :l2]])
end
## value
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Histogram.value(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Histogram.value([name: :metric_with_label, labels: [:l1, :l2]])
end
end
test "observe" do
spec = [name: :http_requests_total,
labels: [:method],
help: ""]
Histogram.new(spec)
Histogram.observe(spec)
Histogram.observe(spec, 3)
assert {[0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0], 4} == Histogram.value(spec)
Histogram.reset(spec)
assert {[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0} == Histogram.value(spec)
end
test "dobserve" do
spec = [name: :http_requests_total,
help: ""]
Histogram.new(spec)
Histogram.dobserve(spec)
Histogram.dobserve(spec, 3.5)
- ## dobserve is async so lets make sure gen_server processed our increment request
+ ## dobserve is async. let's make sure gen_server processed our request
Process.sleep(10)
assert {[0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0], 4.5} == Histogram.value(spec)
Histogram.reset(spec)
assert {[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0} == Histogram.value(spec)
end
test "observe_duration fn" do
spec = [name: :duration_seconds,
labels: [:method],
help: ""]
Histogram.new(spec)
assert 1 == Histogram.observe_duration(spec, fn ->
Process.sleep(1000)
1
end)
- ## observe_duration is async so lets make sure gen_server processed our increment request
+ ## observe_duration is async. let's make sure gen_server processed our request
Process.sleep(10)
{buckets, sum} = Histogram.value(spec)
assert [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0] == buckets
assert 1 < sum and sum < 1.2
assert_raise ErlangError, fn ->
Histogram.observe_duration(spec, fn ->
:erlang.error({:qwe})
end)
end
- ## observe_duration is async so lets make sure gen_server processed our increment request
+ ## observe_duration is async. let's make sure gen_server processed our request
Process.sleep(10)
{buckets, sum} = Histogram.value(spec)
assert [1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0] == buckets
assert 1 < sum and sum < 1.2
end
test "observe_duration block" do
spec = [name: :duration_seconds,
labels: [:method],
help: ""]
Histogram.new(spec)
assert :ok == Histogram.observe_duration(spec, do: Process.sleep(1000))
- ## observe_duration is async so lets make sure gen_server processed our increment request
+ ## observe_duration is async. let's make sure gen_server processed our request
Process.sleep(10)
{buckets, sum} = Histogram.value(spec)
assert [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0] == buckets
assert 1 < sum and sum < 1.2
assert_raise ErlangError, fn ->
Histogram.observe_duration spec do
:erlang.error({:qwe})
end
end
- ## observe_duration is async so lets make sure gen_server processed our increment request
+ ## observe_duration is async. let's make sure gen_server processed our request
Process.sleep(10)
{buckets, sum} = Histogram.value(spec)
assert [1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0] == buckets
assert 1 < sum and sum < 1.2
end
test "undefined value" do
spec = [name: :http_requests_total,
labels: [:method],
help: ""]
Histogram.new(spec)
assert :undefined == Histogram.value(spec)
end
test "remove" do
spec = [name: :http_requests_total,
labels: [:method],
help: ""]
wl_spec = [name: :simple_histogram,
help: ""]
Histogram.new(spec)
Histogram.new(wl_spec)
Histogram.observe(spec)
Histogram.observe(wl_spec)
assert {[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0], 1} == Histogram.value(spec)
assert {[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0], 1} == Histogram.value(wl_spec)
assert true == Histogram.remove(spec)
assert true == Histogram.remove(wl_spec)
assert :undefined == Histogram.value(spec)
assert :undefined == Histogram.value(wl_spec)
assert false == Histogram.remove(spec)
assert false == Histogram.remove(wl_spec)
end
end
diff --git a/test/metric/summary_test.exs b/test/metric/summary_test.exs
index a96acfd..5e49d2d 100644
--- a/test/metric/summary_test.exs
+++ b/test/metric/summary_test.exs
@@ -1,280 +1,281 @@
defmodule Prometheus.SummaryTest do
use Prometheus.Case
test "registration" do
spec = [name: :name,
help: "",
registry: :qwe]
assert true == Summary.declare(spec)
assert false == Summary.declare(spec)
assert_raise Prometheus.MFAlreadyExistsError,
"Metric qwe:name already exists.",
fn ->
Summary.new(spec)
end
end
test "spec errors" do
assert_raise Prometheus.MissingMetricSpecKeyError,
"Required key name is missing from metric spec.",
fn ->
Summary.new([help: ""])
end
assert_raise Prometheus.InvalidMetricNameError,
"Invalid metric name: 12.",
fn ->
Summary.new([name: 12, help: ""])
end
assert_raise Prometheus.InvalidMetricLabelsError,
"Invalid metric labels: 12.",
fn ->
Summary.new([name: "qwe", labels: 12, help: ""])
end
assert_raise Prometheus.InvalidMetricHelpError,
"Invalid metric help: 12.",
fn ->
Summary.new([name: "qwe", help: 12])
end
assert_raise Prometheus.InvalidLabelNameError,
"Invalid label name: quantile (summary cannot have a label named \"quantile\").",
fn ->
Summary.new([name: "qwe", help: "", labels: ["quantile"]])
end
end
test "summary specific errors" do
spec = [name: :http_requests_total,
help: ""]
## observe
assert_raise Prometheus.InvalidValueError,
"Invalid value: qwe (observe accepts only integers).",
fn ->
Summary.observe(spec, "qwe")
end
assert_raise Prometheus.InvalidValueError,
"Invalid value: 1.5 (observe accepts only integers).",
fn ->
Summary.observe(spec, 1.5)
end
## dobserve
assert_raise Prometheus.InvalidValueError,
"Invalid value: qwe (dobserve accepts only numbers).",
fn ->
Summary.dobserve(spec, "qwe")
end
## observe_duration
assert_raise Prometheus.InvalidValueError,
"Invalid value: qwe (observe_duration accepts only functions).",
fn ->
Summary.observe_duration(spec, "qwe")
end
end
test "mf/arity errors" do
spec = [name: :metric_with_label,
labels: [:label],
help: ""]
Summary.declare(spec)
## observe
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Summary.observe(:unknown_metric, 1)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Summary.observe([name: :metric_with_label, labels: [:l1, :l2]], 1)
end
## dobserve
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Summary.dobserve(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Summary.dobserve([name: :metric_with_label, labels: [:l1, :l2]])
end
## observe_duration
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Summary.observe_duration(:unknown_metric, fn -> 1 end)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
- Summary.observe_duration([name: :metric_with_label, labels: [:l1, :l2]], fn -> 1 end)
+ Summary.observe_duration(
+ [name: :metric_with_label, labels: [:l1, :l2]], fn -> 1 end)
end
## remove
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Summary.remove(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Summary.remove([name: :metric_with_label, labels: [:l1, :l2]])
end
## reset
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Summary.reset(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Summary.reset([name: :metric_with_label, labels: [:l1, :l2]])
end
## value
assert_raise Prometheus.UnknownMetricError,
"Unknown metric {registry: default, name: unknown_metric}.",
fn ->
Summary.value(:unknown_metric)
end
assert_raise Prometheus.InvalidMetricArityError,
"Invalid metric arity: got 2, expected 1.",
fn ->
Summary.value([name: :metric_with_label, labels: [:l1, :l2]])
end
end
test "observe" do
spec = [name: :http_requests_total,
labels: [:method],
help: ""]
Summary.new(spec)
Summary.observe(spec)
Summary.observe(spec, 3)
assert {2, 4} == Summary.value(spec)
Summary.reset(spec)
assert {0, 0} == Summary.value(spec)
end
test "dobserve" do
spec = [name: :http_requests_total,
help: ""]
Summary.new(spec)
Summary.dobserve(spec)
Summary.dobserve(spec, 3.5)
- ## dobserve is async so lets make sure gen_server processed our increment request
+ ## dobserve is async. let's make sure gen_server processed our increment request
Process.sleep(10)
assert {2, 4.5} == Summary.value(spec)
Summary.reset(spec)
assert {0, 0} == Summary.value(spec)
end
test "observe_duration fn" do
spec = [name: :duration_seconds,
labels: [:method],
help: ""]
Summary.new(spec)
assert 1 == Summary.observe_duration(spec, fn ->
Process.sleep(1000)
1
end)
- ## observe_duration is async so lets make sure gen_server processed our increment request
+ ## observe_duration is async. let's make sure gen_server processed our request
Process.sleep(10)
{count, sum} = Summary.value(spec)
assert 1 == count
assert 1 < sum and sum < 1.2
assert_raise ErlangError, fn ->
Summary.observe_duration(spec, fn ->
:erlang.error({:qwe})
end)
end
- ## observe_duration is async so lets make sure gen_server processed our increment request
+ ## observe_duration is async. let's make sure gen_server processed our request
Process.sleep(10)
{count, sum} = Summary.value(spec)
assert 2 == count
assert 1 < sum and sum < 1.2
end
test "observe_duration block" do
spec = [name: :duration_seconds,
labels: [:method],
help: ""]
Summary.new(spec)
assert :ok == Summary.observe_duration(spec, do: Process.sleep(1000))
- ## observe_duration is async so lets make sure gen_server processed our increment request
+ ## observe_duration is async. let's make sure gen_server processed our request
Process.sleep(10)
{count, sum} = Summary.value(spec)
assert 1 == count
assert 1 < sum and sum < 1.2
assert_raise ErlangError, fn ->
Summary.observe_duration spec do
:erlang.error({:qwe})
end
end
- ## observe_duration is async so lets make sure gen_server processed our increment request
+ ## observe_duration is async. let's make sure gen_server processed our request
Process.sleep(10)
{count, sum} = Summary.value(spec)
assert 2 == count
assert 1 < sum and sum < 1.2
end
test "undefined value" do
spec = [name: :http_requests_total,
labels: [:method],
help: ""]
Summary.new(spec)
assert :undefined == Summary.value(spec)
end
test "remove" do
spec = [name: :http_requests_total,
labels: [:method],
help: ""]
wl_spec = [name: :simple_summary,
help: ""]
Summary.new(spec)
Summary.new(wl_spec)
Summary.observe(spec)
Summary.observe(wl_spec)
assert {1, 1} == Summary.value(spec)
assert {1, 1} == Summary.value(wl_spec)
assert true == Summary.remove(spec)
assert true == Summary.remove(wl_spec)
assert :undefined == Summary.value(spec)
assert :undefined == Summary.value(wl_spec)
assert false == Summary.remove(spec)
assert false == Summary.remove(wl_spec)
end
end
diff --git a/test/model_test.exs b/test/model_test.exs
index cabc400..49a348c 100644
--- a/test/model_test.exs
+++ b/test/model_test.exs
@@ -1,20 +1,21 @@
defmodule Prometheus.ModelTest do
use Prometheus.Case
require Prometheus.Model
doctest Prometheus.Model
def collect_metrics(:pool_size, _) do
Prometheus.Model.untyped_metric(365)
end
test "create_mf" do
assert {:MetricFamily,<<"pool_size">>,<<"help">>,:UNTYPED,
[{:Metric,[],:undefined,:undefined,:undefined,
{:Untyped,365},
:undefined,:undefined}]} ==
- Prometheus.Model.create_mf(:pool_size, "help", :untyped, Prometheus.ModelTest, :undefined)
+ Prometheus.Model.create_mf(
+ :pool_size, "help", :untyped, Prometheus.ModelTest, :undefined)
end
end
diff --git a/test/registry_test.exs b/test/registry_test.exs
index a5c510f..8e80ffe 100644
--- a/test/registry_test.exs
+++ b/test/registry_test.exs
@@ -1,56 +1,58 @@
defmodule Prometheus.RegistryTest do
use Prometheus.Case
+ alias Prometheus.Registry
+ alias Prometheus.RegistryTest
import ExUnit.CaptureIO
def deregister_cleanup(_), do: :ok
test "default Registry" do
# default registry
- assert :ok == Prometheus.Registry.register_collector(Prometheus.RegistryTest)
- assert [Prometheus.RegistryTest] == Prometheus.Registry.collectors()
- assert true == Prometheus.Registry.collector_registered?(Prometheus.RegistryTest)
- Prometheus.Registry.clear()
- assert [] == Prometheus.Registry.collectors()
- assert false == Prometheus.Registry.collector_registered?(Prometheus.RegistryTest)
-
- assert :ok == Prometheus.Registry.register_collector(Prometheus.RegistryTest)
- Prometheus.Registry.deregister_collector(Prometheus.RegistryTest)
- assert [] == Prometheus.Registry.collectors()
- assert false == Prometheus.Registry.collector_registered?(Prometheus.RegistryTest)
+ assert :ok == Prometheus.Registry.register_collector(RegistryTest)
+ assert [RegistryTest] == Registry.collectors()
+ assert true == Registry.collector_registered?(RegistryTest)
+ Registry.clear()
+ assert [] == Registry.collectors()
+ assert false == Registry.collector_registered?(RegistryTest)
+
+ assert :ok == Registry.register_collector(RegistryTest)
+ Registry.deregister_collector(RegistryTest)
+ assert [] == Registry.collectors()
+ assert false == Registry.collector_registered?(RegistryTest)
## custom registry
- assert :ok == Prometheus.Registry.register_collector(:custom_collector, Prometheus.RegistryTest)
- assert [Prometheus.RegistryTest] == Prometheus.Registry.collectors(:custom_collector)
- assert true == Prometheus.Registry.collector_registered?(:custom_collector, Prometheus.RegistryTest)
- Prometheus.Registry.clear(:custom_collector)
- assert [] == Prometheus.Registry.collectors(:custom_collector)
- assert false == Prometheus.Registry.collector_registered?(:custom_collector, Prometheus.RegistryTest)
-
- assert :ok == Prometheus.Registry.register_collector(:custom_collector, Prometheus.RegistryTest)
- Prometheus.Registry.deregister_collector(:custom_collector, Prometheus.RegistryTest)
- assert [] == Prometheus.Registry.collectors(:custom_collector)
- assert false == Prometheus.Registry.collector_registered?(:custom_collector, Prometheus.RegistryTest)
+ assert :ok == Registry.register_collector(:custom_collector, RegistryTest)
+ assert [RegistryTest] == Registry.collectors(:custom_collector)
+ assert true == Registry.collector_registered?(:custom_collector, RegistryTest)
+ Registry.clear(:custom_collector)
+ assert [] == Registry.collectors(:custom_collector)
+ assert false == Registry.collector_registered?(:custom_collector, RegistryTest)
+
+ assert :ok == Registry.register_collector(:custom_collector, RegistryTest)
+ Registry.deregister_collector(:custom_collector, RegistryTest)
+ assert [] == Registry.collectors(:custom_collector)
+ assert false == Registry.collector_registered?(:custom_collector, RegistryTest)
## register_collectors && collect; default registry
- assert :ok == Prometheus.Registry.register_collectors([Prometheus.RegistryTest])
- assert [Prometheus.RegistryTest] == Prometheus.Registry.collectors()
+ assert :ok == Registry.register_collectors([RegistryTest])
+ assert [RegistryTest] == Registry.collectors()
assert capture_io(fn ->
- Prometheus.Registry.collect(fn (:default, collector) ->
+ Registry.collect(fn (:default, collector) ->
:io.format("~p", [collector])
end) ==
- "Elixir.Prometheus.RegistryTest"
+ "Elixir.RegistryTest"
end)
## register_collectors && collect; custom registry
- assert :ok == Prometheus.Registry.register_collectors(:custom_collector, [Prometheus.RegistryTest])
- assert [Prometheus.RegistryTest] == Prometheus.Registry.collectors(:custom_collector)
+ assert :ok == Registry.register_collectors(:custom_collector, [RegistryTest])
+ assert [RegistryTest] == Registry.collectors(:custom_collector)
assert capture_io(fn ->
- Prometheus.Registry.collect(fn (:custom_collector, collector) ->
+ Registry.collect(fn (:custom_collector, collector) ->
:io.format("~p", [collector])
end, :custom_collector) ==
- "Elixir.Prometheus.RegistryTest"
+ "Elixir.RegistryTest"
end)
end
end

File Metadata

Mime Type
text/x-diff
Expires
Sun, Dec 1, 4:56 PM (1 d, 14 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41755
Default Alt Text
(126 KB)

Event Timeline