class Crometheus::Counter

Overview

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

Defined in:

crometheus/counter.cr

Class Method Summary

Instance Method Summary

Macro Summary

Instance methods inherited from class Crometheus::Metric

docstring : String docstring, name : Symbol name, samples(&block : Sample -> Nil) : Nil samples

Constructor methods inherited from class Crometheus::Metric

new(name : Symbol, docstring : String, register_with : Crometheus::Registry? = Crometheus.default_registry) new

Class methods inherited from class Crometheus::Metric

type type, valid_label?(label : Symbol) : Bool valid_label?

Class Method Detail

def self.type #

[View source]

Instance Method Detail

def count_exceptions(&block) #

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

[View source]
def get : Float64 #

Fetches the current counter value.


[View source]
def inc(x : Int | Float = 1.0) #

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


[View source]
def reset #

Sets the counter value to 0.0.


[View source]
def samples(&block : Sample -> Nil) : Nil #

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


[View source]

Macro Detail

macro count_exceptions_of_type(counter, ex_type) #

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.


[View source]