Class: PrometheusExporter::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/prometheus_exporter/middleware.rb

Constant Summary collapse

MethodProfiler =
PrometheusExporter::Instrumentation::MethodProfiler

Instance Method Summary collapse

Constructor Details

#initialize(app, config = { instrument: true, client: nil }) ⇒ Middleware

Returns a new instance of Middleware.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/prometheus_exporter/middleware.rb', line 9

def initialize(app, config = { instrument: true, client: nil })
  @app = app
  @client = config[:client] || PrometheusExporter::Client.default

  if config[:instrument]
    if defined? Redis::Client
      MethodProfiler.patch(Redis::Client, [:call, :call_pipeline], :redis)
    end
    if defined? PG::Connection
      MethodProfiler.patch(PG::Connection, [
        :exec, :async_exec, :exec_prepared, :send_query_prepared, :query
      ], :sql)
    end
    if defined? Mysql2::Client
      MethodProfiler.patch(Mysql2::Client, [:query], :sql)
      MethodProfiler.patch(Mysql2::Statement, [:execute], :sql)
      MethodProfiler.patch(Mysql2::Result, [:each], :sql)
    end
  end
end

Instance Method Details

#call(env) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/prometheus_exporter/middleware.rb', line 30

def call(env)
  queue_time = measure_queue_time(env)

  MethodProfiler.start
  result = @app.call(env)
  info = MethodProfiler.stop

  result
ensure
  status = (result && result[0]) || -1
  params = env["action_dispatch.request.parameters"]
  action, controller = nil
  if params
    action = params["action"]
    controller = params["controller"]
  end

  @client.send_json(
    type: "web",
    timings: info,
    queue_time: queue_time,
    action: action,
    controller: controller,
    status: status
  )
end