class Upfluence::HTTP::Middleware::Prometheus

Constants

LABELS

Public Class Methods

new(app, registry = ::Prometheus::Client.registry) click to toggle source
# File lib/upfluence/http/middleware/prometheus.rb, line 10
def initialize(app, registry = ::Prometheus::Client.registry)
  @registry = registry

  @request_total_count = @registry.get(
    :uhttp_handler_requests_total
  ) || @registry.counter(
    :uhttp_handler_requests_total,
    docstring: 'Histogram of processed items',
    labels:    LABELS + %i[status]
  )

  @request_histogram = @registry.get(
    :uhttp_handler_requests_duration_second
  ) || @registry.histogram(
    :uhttp_handler_requests_duration_second,
    docstring: 'Histogram of processing time',
    labels:    LABELS
  )

  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/upfluence/http/middleware/prometheus.rb, line 32
def call(env)
  trace(env) { @app.call(env) }
end

Private Instance Methods

trace(env) { || ... } click to toggle source
# File lib/upfluence/http/middleware/prometheus.rb, line 38
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