class Invoca::Metrics::Prometheus::Configuration

Constants

AVAILABLE_METRIC_TYPES
AVAILABLE_MODES

Attributes

client[R]

The client to use for exporting Prometheus metrics.

@return [PrometheusExporter::Client]

host[RW]

The host to use for the Prometheus server. This setting is only used when in :push mode. Defaults to `localhost`.

@return [String]

mode[R]

The mode to use for exporting Prometheus metrics. Valid modes are:

- `:direct` (default):  Exports metrics directly using a threaded webserver.
- `:push`: Exports metrics using a pushgateway.

@return [Symbol]

port[RW]

The port to use when exporting metrics. When in :direct mode this is used to specify the port to listen on. When in :push mode this is used to specify the port to push to. Defaults to 9394.

@return [Integer]

server[R]

The webserver that is exposed when in :direct mode.

@return [PrometheusExporter::Server::WebServer]

Public Class Methods

new() click to toggle source
# File lib/invoca/metrics/prometheus/configuration.rb, line 45
def initialize
  @host = 'localhost'
  @port = 9394
end

Public Instance Methods

freeze() click to toggle source

Initializes the client to use for exporting Prometheus metrics and freezes the configuration object so no more changes can be made to it.

Calls superclass method
# File lib/invoca/metrics/prometheus/configuration.rb, line 57
def freeze
  validate_configuration!

  case mode
  when :direct
    initialize_direct_client
  when :push
    initialize_push_client
  end

  PrometheusExporter::Client.default = @client

  super
end
mode=(new_mode) click to toggle source
# File lib/invoca/metrics/prometheus/configuration.rb, line 50
def mode=(new_mode)
  AVAILABLE_MODES.include?(new_mode) or raise ArgumentError, "Invalid mode #{new_mode.inspect}. Allowed modes: #{AVAILABLE_MODES}"
  @mode = new_mode
end
register_metric(type, name, help, opts = nil) click to toggle source

Registers a metric against the current Configuration. Once the metric object is registered, metrics can be reported and exported through the metric object.

@param type [Symbol] The type of metric to register. Valid types are: :counter, :gauge, :histogram @param name [String] The name of the metric. @param help [String] The help text for the metric. @param opts [Hash] A hash of options to pass to the metric object. This is different depending on the

type of metrics being registered.

@return metric [PrometheusExporter::Metric]

# File lib/invoca/metrics/prometheus/configuration.rb, line 82
def register_metric(type, name, help, opts = nil)
  AVAILABLE_METRIC_TYPES.include?(type) or raise ArgumentError, "Invalid metric type #{type.inspect}. Allowed types: #{AVAILABLE_METRIC_TYPES}"
  client.register(type, name, help, opts)
end

Private Instance Methods

initialize_direct_client() click to toggle source
# File lib/invoca/metrics/prometheus/configuration.rb, line 89
def initialize_direct_client
  @server = PrometheusExporter::Server::WebServer.new(bind: '0.0.0.0', port: port)
  @server.start
  @client = PrometheusExporter::LocalClient.new(collector: @server.collector)
end
initialize_push_client() click to toggle source
# File lib/invoca/metrics/prometheus/configuration.rb, line 95
def initialize_push_client
  @client = PrometheusExporter::Client.new(host: host, port: port)
end
validate_configuration!() click to toggle source
# File lib/invoca/metrics/prometheus/configuration.rb, line 99
def validate_configuration!
  mode.present? or raise ArgumentError, "Mode must be provided. Valid modes: #{AVAILABLE_MODES}"
  port.present? or raise ArgumentError, "Port must be provided when in #{mode.inspect} mode"
  if mode == :push
    host.present? or raise ArgumentError, "Host must be provided when in #{mode.inspect} mode"
  end
end