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.
By default metrics all have the prefix “http_server”. Set ‘:metrics_prefix` to something else if you like.
The request counter metric is broken down by code, method and path. The request duration metric is broken down by method and path.
Attributes
Public Class Methods
Source
# File lib/prometheus/middleware/collector.rb, line 21 def initialize(app, options = {}) @app = app @registry = options[:registry] || Client.registry @metrics_prefix = options[:metrics_prefix] || 'http_server' init_request_metrics init_exception_metrics end
Protected Instance Methods
Source
# File lib/prometheus/middleware/collector.rb, line 95 def generate_path(env) full_path = [env['SCRIPT_NAME'], env['PATH_INFO']].join strip_ids_from_path(full_path) end
Source
# File lib/prometheus/middleware/collector.rb, line 56 def init_exception_metrics @exceptions = @registry.counter( :"#{@metrics_prefix}_exceptions_total", docstring: 'The total number of exceptions raised by the Rack application.', labels: [:exception] ) end
Source
# File lib/prometheus/middleware/collector.rb, line 42 def init_request_metrics @requests = @registry.counter( :"#{@metrics_prefix}_requests_total", docstring: 'The total number of HTTP requests handled by the Rack application.', labels: %i[code method path] ) @durations = @registry.histogram( :"#{@metrics_prefix}_request_duration_seconds", docstring: 'The HTTP response duration of the Rack application.', labels: %i[method path] ) end
Source
# File lib/prometheus/middleware/collector.rb, line 36 def realtime start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) yield Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time end
Source
# File lib/prometheus/middleware/collector.rb, line 74 def record(env, code, duration) path = generate_path(env) counter_labels = { code: code, method: env['REQUEST_METHOD'].downcase, path: path, } duration_labels = { method: env['REQUEST_METHOD'].downcase, path: path, } @requests.increment(labels: counter_labels) @durations.observe(duration, labels: duration_labels) rescue # TODO: log unexpected exception during request recording nil end
Source
# File lib/prometheus/middleware/collector.rb, line 101 def strip_ids_from_path(path) path .gsub(%r{/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}(?=/|$)}, '/:uuid\\1') .gsub(%r{/\d+(?=/|$)}, '/:id\\1') end
Source
# File lib/prometheus/middleware/collector.rb, line 64 def trace(env) response = nil duration = realtime { response = yield } record(env, response.first.to_s, duration) return response rescue => exception @exceptions.increment(labels: { exception: exception.class.name }) raise end