class Prometheus::Middleware::Collector

Collector is a Rack middleware that provides a sample implementation of a HTTP tracer.

By default metrics are registered on the global registry. Set the `:registry` option to use a custom registry.

The request counter metric is broken down by code, method and path by default. Set the `:counter_label_builder` option to use a custom label builder.

The request duration metric is broken down by method and path by default. Set the `:duration_label_builder` option to use a custom label builder.

Constants

COUNTER_LB
DURATION_LB

Attributes

app[R]
registry[R]

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/prometheus/middleware/collector.rb, line 21
def initialize(app, options = {})
  @app = app
  @registry = options[:registry] || Client.registry
  @counter_lb = options[:counter_label_builder] || COUNTER_LB
  @duration_lb = options[:duration_label_builder] || DURATION_LB

  init_request_metrics
  init_exception_metrics
end

Protected Instance Methods

init_exception_metrics() click to toggle source
# File lib/prometheus/middleware/collector.rb, line 63
def init_exception_metrics
  @exceptions = @registry.counter(
    :http_server_exceptions_total,
    'The total number of exceptions raised by the Rack application.',
  )
end
init_request_metrics() click to toggle source
# File lib/prometheus/middleware/collector.rb, line 52
def init_request_metrics
  @requests = @registry.counter(
    :http_server_requests_total,
    'The total number of HTTP requests handled by the Rack application.',
  )
  @durations = @registry.histogram(
    :http_server_request_duration_seconds,
    'The HTTP response duration of the Rack application.',
  )
end
trace(env) { || ... } click to toggle source
# File lib/prometheus/middleware/collector.rb, line 70
def trace(env)
  start = Time.now
  yield.tap do |response|
    duration = [(Time.now - start).to_f, 0.0].max
    record(env, response.first.to_s, duration)
  end