class Crometheus::Counter
- Crometheus::Counter
- Crometheus::Metric
- Reference
- Object
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.crClass Method Summary
-
.type
Returns
Type::Counter
.
Instance Method Summary
-
#count_exceptions(&)
Yields to the block, calling #inc if an exception is raised.
-
#get : Float64
Fetches the current counter value.
-
#inc(x : Number::Primitive = 1.0)
Increments the counter value by the given number, or
1.0
. -
#reset
Sets the counter value to
0.0
. -
#samples(&block : Sample -> Nil) : Nil
Yields a single Sample bearing the counter value.
Macro Summary
-
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.
Instance methods inherited from class Crometheus::Metric
docstring : String
docstring,
name : Symbol
name,
samples(&block : Sample -> Nil)
samples
Constructor methods inherited from class Crometheus::Metric
new(name : Symbol, docstring : String, register_with : Crometheus::Registry | Nil = Crometheus.default_registry)
new
Class methods inherited from class Crometheus::Metric
type
type,
valid_label?(label : Symbol) : Bool
valid_label?
Class Method Detail
Instance Method Detail
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
Yields a single Sample bearing the counter value. See
Metric#samples
.
Macro Detail
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.