lib/prometheus/client/rack/collector.rb in prometheus-client-0.4.2 vs lib/prometheus/client/rack/collector.rb in prometheus-client-0.5.0

- old
+ new

@@ -12,16 +12,11 @@ attr_reader :app, :registry def initialize(app, options = {}, &label_builder) @app = app @registry = options[:registry] || Client.registry - @label_builder = label_builder || proc do |env| - { - method: env['REQUEST_METHOD'].downcase, - path: env['PATH_INFO'].to_s, - } - end + @label_builder = label_builder || DEFAULT_LABEL_BUILDER init_request_metrics init_exception_metrics end @@ -29,21 +24,28 @@ trace(env) { @app.call(env) } end protected + DEFAULT_LABEL_BUILDER = proc do |env| + { + method: env['REQUEST_METHOD'].downcase, + host: env['HTTP_HOST'].to_s, + path: env['PATH_INFO'].to_s, + } + end + def init_request_metrics @requests = @registry.counter( :http_requests_total, 'A counter of the total number of HTTP requests made.') @requests_duration = @registry.counter( - :http_request_durations_total_microseconds, - 'The total amount of time spent answering HTTP requests ' \ - '(microseconds).') + :http_request_duration_total_seconds, + 'The total amount of time spent answering HTTP requests.') @durations = @registry.summary( - :http_request_durations_microseconds, - 'A histogram of the response latency (microseconds).') + :http_request_duration_seconds, + 'A histogram of the response latency.') end def init_exception_metrics @exceptions = @registry.counter( :http_exceptions_total, @@ -51,10 +53,10 @@ end def trace(env) start = Time.now yield.tap do |response| - duration = ((Time.now - start) * 1_000_000).to_i + duration = (Time.now - start).to_f record(labels(env, response), duration) end rescue => exception @exceptions.increment(exception: exception.class.name) raise