abstract class Crometheus::Metric

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

Defined in:

crometheus/standard_exports.cr
crometheus/metric.cr

Constructors

Class Method Summary

Instance Method Summary

Macro Summary

Constructor Detail

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

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.


[View source]

Class Method Detail

def self.type #

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.


[View source]
def self.valid_label?(label : Symbol) : Bool #

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.


[View source]

Instance Method Detail

def docstring : String #

The docstring that appears in the HELP line of the exported metric.


[View source]
def name : Symbol #

The name of the metric. This will be converted to a String and exported to Prometheus.


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

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.


[View source]

Macro Detail

macro [](*labels) #

Convenience macro for generating a LabeledMetric with appropriate type parameters. Takes any number of Symbols 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.

[View source]