Prometheus.ex v1.0.0-alpha3 Prometheus.Metric.Counter

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()/ 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/, the difference is that new/ will raise Prometheus.Error.MFAlreadyExists 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_total_requests,
                     help: "Total requests.",
                     labels: [:caller]])
  end

  def inc(caller) do
    Counter.inc([name: :my_service_total_requests,
                 labels: [caller]])
  end

end

Summary

Macros

Creates a counter using spec. If a counter with the same spec exists returns false

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

Increments the counter identified by spec by value

Creates a counter using spec

Resets the value of the counter identified by spec

Returns the value of the counter identified by spec. If there is no counter for given labels combination, returns :undefined

Macros

declare(spec)

Creates a counter using spec. If a counter with the same spec exists returns false.

Raises Prometheus.Error.MissingMetricSpecKey if required spec key is missing.
Raises Prometheus.Error.InvalidMetricName if metric name is invalid.
Raises Prometheus.Error.InvalidMetricHelp if help is invalid.
Raises Prometheus.Error.InvalidMetricLabels if labels isn’t a list.
Raises Prometheus.Error.InvalidMetricName if label name is invalid.

dinc(spec, value \\ 1)

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.

Raises Prometheus.Error.InvalidValue exception if value isn’t a positive number.
Raises Prometheus.Error.UnknownMetric exception if a counter for spec can’t be found.
Raises Prometheus.Error.InvalidMetricArity exception if labels count mismatch.

inc(spec, value \\ 1)

Increments the counter identified by spec by value.

Raises Prometheus.Error.InvalidValue exception if value isn’t a positive integer.
Raises Prometheus.Error.UnknownMetric exception if a counter for spec can’t be found.
Raises Prometheus.Error.InvalidMetricArity exception if labels count mismatch.

new(spec)

Creates a counter using spec.

Raises Prometheus.Error.MissingMetricSpecKey if required spec key is missing.
Raises Prometheus.Error.InvalidMetricName if metric name is invalid.
Raises Prometheus.Error.InvalidMetricHelp if help is invalid.
Raises Prometheus.Error.InvalidMetricLabels if labels isn’t a list.
Raises Prometheus.Error.InvalidMetricName if label name is invalid.
Raises Prometheus.Error.MFAlreadyExists if a counter with the same spec already exists.

reset(spec)

Resets the value of the counter identified by spec.

Raises Prometheus.Error.UnknownMetric exception if a counter for spec can’t be found.
Raises Prometheus.Error.InvalidMetricArity exception if labels count mismatch.

value(spec)

Returns the value of the counter identified by spec. If there is no counter for given labels combination, returns :undefined.

Raises Prometheus.Error.UnknownMetric exception if a counter for spec can’t be found.
Raises Prometheus.Error.InvalidMetricArity exception if labels count mismatch.