Class: PrometheusExporter::Middleware
- Inherits:
-
Object
- Object
- PrometheusExporter::Middleware
- Defined in:
- lib/prometheus_exporter/middleware.rb
Constant Summary collapse
- MethodProfiler =
PrometheusExporter::Instrumentation::MethodProfiler
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#custom_labels(env) ⇒ Object
allows subclasses to add custom labels based on env.
- #default_labels(env, result) ⇒ Object
-
#initialize(app, config = { instrument: true, client: nil }) ⇒ Middleware
constructor
A new instance of Middleware.
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 |
# 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 obj = { type: "web", timings: info, queue_time: queue_time, default_labels: default_labels(env, result) } labels = custom_labels(env) if labels obj = obj.merge(custom_labels: labels) end @client.send_json(obj) end |
#custom_labels(env) ⇒ Object
allows subclasses to add custom labels based on env
71 72 73 |
# File 'lib/prometheus_exporter/middleware.rb', line 71 def custom_labels(env) nil end |
#default_labels(env, result) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/prometheus_exporter/middleware.rb', line 54 def default_labels(env, result) status = (result && result[0]) || -1 params = env["action_dispatch.request.parameters"] action = controller = nil if params action = params["action"] controller = params["controller"] end { action: action || "other", controller: controller || "other", status: status } end |