Class: Insights::API::Common::Middleware::WebServerMetrics

Inherits:
Object
  • Object
show all
Defined in:
lib/insights/api/common/middleware/web_server_metrics.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ WebServerMetrics

Returns a new instance of WebServerMetrics.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/insights/api/common/middleware/web_server_metrics.rb', line 6

def initialize(app, options = {})
  @app            = app
  @metrics_prefix = options[:metrics_prefix] || "http_server"

  require 'prometheus_exporter/client'
  require 'prometheus_exporter/metric'

  PrometheusExporter::Metric::Base.default_prefix = "#{@metrics_prefix}_"

  @puma_busy_threads = PrometheusExporter::Client.default.register(:gauge, "puma_busy_threads", "The number of threads currently handling HTTP requests.")
  @puma_max_threads  = PrometheusExporter::Client.default.register(:gauge, "puma_max_threads", "The total number of threads able to handle HTTP requests.")
  @request_counter   = PrometheusExporter::Client.default.register(:counter, "requests_total", "The total number of HTTP requests handled by the Rack application.")
  @request_histogram = PrometheusExporter::Client.default.register(:histogram, "request_duration_seconds", "The HTTP response duration of the Rack application.")
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/insights/api/common/middleware/web_server_metrics.rb', line 21

def call(env)
  @puma_busy_threads.increment

  result = nil
  duration = Benchmark.realtime { result = @app.call(env) }
  result
rescue => error
  @error = error
  raise
ensure
  duration_labels = {
    :method => env['REQUEST_METHOD'].downcase,
    :path   => strip_ids_from_path(env['PATH_INFO']),
  }

  counter_labels = duration_labels.merge(:code => result.first.to_s).tap do |labels|
    labels[:exception] = @error.class.name if @error
  end

  @request_counter.increment(counter_labels)
  @request_histogram.observe(duration, duration_labels)

  @puma_max_threads.observe(puma_stats["max_threads"])
  @puma_busy_threads.decrement
end