class Crometheus::Registry

Overview

The Registry class is responsible for aggregating samples from all metrics and exposing data to Prometheus via an HTTP server.

Crometheus automatically instantiates a Registry for you, and Metric registers with it by default. This means that for most applications, this is all it takes to get an HTTP server up and serving:

require "crometheus/registry"
Crometheus.default_registry.host = "0.0.0.0" # defaults to "localhost"
Crometheus.default_registry.port = 12345     # defaults to 5000
Crometheus.default_registry.start_server

Defined in:

crometheus/registry.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(include_standard_exports = true) #

Creates a new Registry. Will export standard process statistics by default by calling Crometheus.make_standard_exports. If you for some reason want to avoid this, set include_standard_exports to false.


[View source]

Instance Method Detail

def get_handler #

Returns an HTTP::Handler that generates metrics. If #path is configured, and does not match the context path, passes through to the next handler instead.


[View source]
def host : String #

The host that the server should bind to. Defaults to "localhost".


[View source]
def host=(host : String) #

The host that the server should bind to. Defaults to "localhost".


[View source]
def metrics : Array(Crometheus::Metric) #

A list of all Metric objects being exposed by this registry.


[View source]
def namespace : String #

If non-empty, will be prefixed to all metric names, separated by an underscore.


[View source]
def namespace=(str : String) #

Sets #namespace to str, raising an ArgumentError if str is not legal for a Prometheus metric name.


[View source]
def path : String | Regex | Nil #

If non-nil, will only handle requests with a matching path.


[View source]
def path=(path : String | Regex | Nil) #

If non-nil, will only handle requests with a matching path.


[View source]
def port : Int32 #

The port that the server should bind to. Defaults to 5000.


[View source]
def port=(port : Int32) #

The port that the server should bind to. Defaults to 5000.


[View source]
def register(metric) #

Adds a Metric to this registry. The metric's samples will then show up whenever the server is scraped. Metrics call #register automatically in their constructors, so manual invocation is not usually required.


[View source]
def run_server #

Creates an HTTP::Server object bound to #host and #port and begins serving metrics as per #get_handler. Returns true once the server is stopped. Returns false immediately if this registry is already serving.


[View source]
def start_server #

Spawns a new fiber that serves HTTP connections on #host and #port, then returns true. If the server is already running, returns false without creating a new one.

#start_server is included for convenience, but does not do any exception handling. Serious applications should use #run_server (which runs in the current fiber) instead, or call #get_handler if they need more control over HTTP features..


[View source]
def stop_server #

Stops the HTTP server, then returns true. If the server is not running, returns false instead.


[View source]
def unregister(metric) #

Removes a Metric from the registry. The Metric keeps its state and can be re-registered later.


[View source]