abstract class Crometheus::Metric
- Crometheus::Metric
- Reference
- Object
Overview
Metric
is the base class for individual metrics types.
If you want to create your own custom metric types, you'll need to
subclass Metric
.
#samples
is the only abstract method you'll need to override; then
you will also need to implement your custom instrumentation.
You'll probably also want to define .type
on your new class; it
should return a member of enum Type
.
The following is a perfectly serviceable, if useless, metric type:
require "crometheus/metric"
class Randometric < Crometheus::Metric
def self.type
Type::Gauge
end
def samples(&block : Crometheus::Sample -> Nil)
yield Crometheus::Sample.new my_value_method
end
def my_value_method
rand(10).to_f64
end
end
See the source to the Counter
, Gauge
, Histogram
, and Summary
classes for more detailed examples of how to subclass Metric
.
Direct Known Subclasses
- Crometheus::Counter
- Crometheus::Gauge
- Crometheus::Histogram
- Crometheus::Metric::LabeledMetric(LabelType, MetricType)
- Crometheus::StandardExports
- Crometheus::Summary
Defined in:
crometheus/metric.crcrometheus/standard_exports.cr
Constructors
-
.new(name : Symbol, docstring : String, register_with : Crometheus::Registry | Nil = Crometheus.default_registry)
Initializes
#name
and#docstring
, then passesself
toregister_with.register
if notnil
.
Class Method Summary
-
.type
Returns the type of Prometheus metric this class represents.
-
.valid_label?(label : Symbol) : Bool
Called by
#initialize
to validate that thisMetric
's labels do not violate any of Prometheus' naming rules.
Instance Method Summary
-
#docstring : String
The docstring that appears in the
HELP
line of the exported metric. -
#name : Symbol
The name of the metric.
-
#samples(&block : Sample -> Nil) : Nil
Yields one
Sample
for each time series this metric represents.
Macro Summary
-
[](*labels)
Convenience macro for generating a
LabeledMetric
with appropriate type parameters.
Constructor Detail
Initializes #name
and #docstring
, then passes self
to
register_with.register
if not nil
.
Raises an ArgumentError
if #name
does not conform to
Prometheus' standards.
Class Method Detail
Returns the type of Prometheus metric this class represents.
Should be overridden to return the appropriate member of Type
.
Called by Registry
to determine metric type to report to
Prometheus.
Users generally do not need to call this.
Called by #initialize
to validate that this Metric
's labels
do not violate any of Prometheus' naming rules. Returns false
under any of these conditions:
- the label is
:job
or:instance
- the label starts with
__
- the label is not alphanumeric with underscores
This generally does not need to be called manually.
Instance Method Detail
The name of the metric. This will be converted to a String
and
exported to Prometheus.
Yields one Sample
for each time series this metric represents.
Called by Registry
to collect data for exposition.
Users generally do not need to call this.
Macro Detail
Convenience macro for generating a LabeledMetric
with
appropriate type parameters.
Takes any number of Symbol
s as arguments, returning a
LabeledMetric
class object with those arguments as label names.
Note that this macro causes type inference to fail when used with
class or instance variables; see Crometheus.alias
for that use
case.
require "crometheus/gauge"
ages = Crometheus::Gauge[:first_name, last_name].new(:age, "Age of person")
ages[first_name: "Jane", last_name: "Doe"].set 32
ages[first_name: "Sally", last_name: "Generic"].set 49
# ages[first_name: "Jay", middle_initial: "R", last_name: "Hacker"].set 46
# => compiler error; label names don't match.