Sha256: fe2a9cc3836802d856c4e4f62aa345f34f10544a4132a427297062546ff35749

Contents?: true

Size: 1.2 KB

Versions: 10

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require 'prometheus_exporter/instrumentation/method_profiler'
require 'prometheus_exporter/client'

class PrometheusExporter::Middleware
  MethodProfiler = PrometheusExporter::Instrumentation::MethodProfiler

  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
    end
  end

  def call(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,
      action: action,
      controller: controller,
      status: status
    )
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
prometheus_exporter-0.1.16 lib/prometheus_exporter/middleware.rb
prometheus_exporter-0.1.15 lib/prometheus_exporter/middleware.rb
prometheus_exporter-0.1.14 lib/prometheus_exporter/middleware.rb
prometheus_exporter-0.1.13 lib/prometheus_exporter/middleware.rb
prometheus_exporter-0.1.12 lib/prometheus_exporter/middleware.rb
prometheus_exporter-0.1.11 lib/prometheus_exporter/middleware.rb
prometheus_exporter-0.1.10 lib/prometheus_exporter/middleware.rb
prometheus_exporter-0.1.9 lib/prometheus_exporter/middleware.rb
prometheus_exporter-0.1.8 lib/prometheus_exporter/middleware.rb
prometheus_exporter-0.1.7 lib/prometheus_exporter/middleware.rb