Sha256: a3f250d4c7be52a5a75d53152c3309fb218b213617c59832a6c7169268996d1f
Contents?: true
Size: 1.32 KB
Versions: 4
Compression:
Stored size: 1.32 KB
Contents
# frozen_string_literal: true require "prometheus_aggregator" module PrometheusAggregator class RackMiddleware def initialize(app, options = {}) @app = app @client = options[:client] raise ArgumentError, ":client option is required" unless @client end def call(env) start_time = Time.now response = @app.call(env) duration = Time.now - start_time begin @client.counter( name: "http_server_requests_total", help: "The total number of HTTP requests handled by the Rack app", value: 1, labels: labels(env).merge(code: response.first.to_s) ) @client.histogram( name: "http_server_request_duration_seconds", help: "The HTTP response duration of the Rack application", value: duration, labels: labels(env) ) rescue => err # rubocop:disable Style/RescueStandardError # Let's be ultra defensive. Metrics should never break the app. PrometheusAggregator.logger.error("RackMiddleware: #{err}") end response end def labels(env) { method: env["REQUEST_METHOD"].downcase, path: clean_path(env["PATH_INFO"]) } end def clean_path(path) path.gsub(%r{/\d+/}, "/:id/").gsub(%r{/\d+$}, "/:id") end end end
Version data entries
4 entries across 4 versions & 1 rubygems