class

Crometheus::Counter

Inherits Crometheus::Metric / Reference / Object

Counter is a Metric type that stores a single value internally. This value can be reset to zero, but otherwises increases monotonically, and only when #inc is called.

require "crometheus/counter"

flowers_planted = Crometheus::Counter.new(
  :flowers_planted, "Number of flowers planted")
flowers_planted.inc 10
flowers_planted.inc
flowers_planted.inc
flowers_planted.get # => 12.0

Class methods

type

Returns Type::Counter. See Metric.type.

Source

Instance methods

count_exceptions

Yields to the block, calling #inc if an exception is raised. The exception is always re-raised.

require "crometheus/counter"
include Crometheus

def risky_code
  x = 1 / [1, 0].sample
end

counter = Counter.new :example, ""
100.times do
  begin
    counter.count_exceptions { risky_code }
  rescue DivisionByZero
  end
end
puts counter.get # approximately 50
Source
get

Fetches the current counter value.

Source
inc(x : Int8 | Int16 | Int32 | Int64 | Int128 | Float32 | Float64 = 1.0)

Increments the counter value by the given number, or 1.0.

Source
reset

Sets the counter value to 0.0.

Source
samples

Yields a single Sample bearing the counter value. See Metric#samples.

Source

Macros

count_exceptions_of_type(counter, ex_type, &block)

Yields to the block, incrementing the given counter when an exception matching the given type is raised. At a future date, this macro will be deprecated and its functionality folded into #count_exceptions. Pending https://github.com/crystal-lang/crystal/issues/2060.

Source