Class: DelayedJobMetrics::Exporter

Inherits:
Prometheus::Middleware::Exporter
  • Object
show all
Defined in:
lib/delayed_job_metrics/exporter.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Exporter.



7
8
9
10
11
12
13
14
# File 'lib/delayed_job_metrics/exporter.rb', line 7

def initialize(app, options = {})
  @app = app
  @registry = options[:registry] || Prometheus::Client.registry
  @path = options[:path] || '/metrics'
  @acceptable = build_dictionary(FORMATS, FALLBACK)

  init_delayed_jobs_metrics
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/delayed_job_metrics/exporter.rb', line 16

def call(env)
  if env['PATH_INFO'] == @path
    if ENV['HTAUTH_METRICS_USER'] && ENV['HTAUTH_METRICS_PASSWORD']
      http_auth_call(env, :expose_metrics)
    else
      expose_metrics(env)
    end
  else
    @app.call(env)
  end
end

#collect_metricsObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/delayed_job_metrics/exporter.rb', line 53

def collect_metrics
  @dj_total_count.set(Delayed::Job.count)
  @dj_total_pending_count.set(
    Delayed::Job.where(attempts: 0, locked_at: nil).count
  )

  Delayed::Job.group(:queue, :priority, :attempts)
              .count.each do |data, count|
    @dj_count.set(count, labels: {
                    queue: data[0],
                    priority: data[1],
                    attempts: data[2]
                  })
    next unless (data[2]).zero?

    @dj_pending_count.set(count, labels: {
                            queue: data[0],
                            priority: data[1]
                          })
  end

  Delayed::Job.where.not(last_error: nil)
              .where(failed_at: nil).group(
                :queue,
                :priority,
                :attempts
              ).count.each do |data, count|
    @dj_error_count.set(count, labels: {
                          queue: data[0],
                          priority: data[1],
                          attempts: data[2]
                        })
  end

  Delayed::Job.where.not(failed_at: nil)
              .group(:queue,
                     :priority).count.each do |data, count|
    @dj_failed_count.set(count, labels: {
                           queue: data.first,
                           priority: data.last
                         })
  end

  Delayed::Job.where(failed_at: nil)
              .where('DATE(run_at) = DATE(?)', Time.now)
              .group(:queue, :priority, :attempts).count
              .each do |data, count|
    @dj_to_be_executed_today_count.set(count, labels: {
                                         queue: data[0],
                                         priority: data[1],
                                         attempts: data[2]
                                       })
  end

  Delayed::Job.where.not(failed_at: nil)
              .where('DATE(run_at) = DATE(?)', Time.now)
              .group(:queue, :priority).count
              .each do |data, count|
    @dj_failed_today_count.set(count, labels: {
                                 queue: data[0],
                                 priority: data[1]
                               })
  end

  jobs_handler('failed_at is NULL').each do |data, count|
    @dj_handler_count.set(count, labels: {
                            queue: data[0],
                            priority: data[1],
                            attempts: data[2],
                            handler: data[3]
                          })
  end

  jobs_handler('last_error is not NULL').each do |data, count|
    @dj_handler_error_count.set(count, labels: {
                                  queue: data[0],
                                  priority: data[1],
                                  attempts: data[2],
                                  handler: data[3]
                                })
  end

  jobs_methods(false).each do |data, count|
    @dj_performable_count.set(count, labels: {
                                queue: data[0],
                                priority: data[1],
                                attempts: data[2],
                                handler: data[3],
                                object: data[4],
                                method_name: data[5]
                              })
  end

  jobs_methods(true).each do |data, count|
    @dj_performable_failed_count.set(count, labels: {
                                       queue: data[0],
                                       priority: data[1],
                                       attempts: data[2],
                                       handler: data[3],
                                       object: data[4],
                                       method_name: data[5]
                                     })
  end
end

#expose_metrics(env) ⇒ Object



36
37
38
39
# File 'lib/delayed_job_metrics/exporter.rb', line 36

def expose_metrics(env)
  format = negotiate(env, @acceptable)
  format ? process_mertics_request(format) : not_acceptable(FORMATS)
end

#http_auth_call(env, callback) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/delayed_job_metrics/exporter.rb', line 28

def http_auth_call(env, callback)
  auth = BasicAuth.new(env) do |u, p|
    u == ENV['HTAUTH_METRICS_USER'] && p == ENV['HTAUTH_METRICS_PASSWORD']
  end

  auth.call(env, method(callback))
end

#init_delayed_jobs_metricsObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/delayed_job_metrics/exporter.rb', line 195

def init_delayed_jobs_metrics
  @dj_total_count = @registry.gauge(
    :delayed_jobs_total_count,
    docstring: 'The Delayed Jobs total count.'
  )

  @dj_total_pending_count = @registry.gauge(
    :delayed_jobs_pending_total_count,
    docstring: 'The Pending Delayed Jobs total '\
               'count (Jobs with 0 attempts).'
  )

  @dj_count = @registry.gauge(
    :delayed_jobs_queue_total_count,
    docstring: 'The Delayed Jobs total count Per Queue.',
    labels: i[queue priority attempts]
  )

  @dj_pending_count = @registry.gauge(
    :delayed_jobs_queue_pending_total_count,
    docstring: 'The Pending Delayed Jobs total count Per Queue.',
    labels: i[queue priority]
  )

  @dj_error_count = @registry.gauge(
    :delayed_jobs_queue_error_total_count,
    docstring: 'The total count of delayed jobs with '\
               'errors (Terminated Jobs do not count).',
    labels: i[queue priority attempts]
  )

  @dj_failed_count = @registry.gauge(
    :delayed_jobs_queue_failed_total_count,
    docstring: 'The total count of the failed delayed '\
               'jobs with errors (Jobs will not be retried anymore).',
    labels: i[queue priority]
  )

  @dj_to_be_executed_today_count = @registry.gauge(
    :delayed_jobs_to_be_executed_today_count,
    docstring: 'The total count of the delayed jobs '\
               'that should be executed today).',
    labels: i[queue priority attempts]
  )

  @dj_failed_today_count = @registry.gauge(
    :delayed_jobs_failed_today_count,
    docstring: 'The total count of the delayed jobs that failed today).',
    labels: i[queue priority]
  )

  @dj_handler_count = @registry.gauge(
    :delayed_jobs_handler_count,
    docstring: 'The total count of the active delayed '\
               'jobs per handler class).',
    labels: i[queue priority attempts handler]
  )

  @dj_handler_error_count = @registry.gauge(
    :delayed_jobs_handler_error_count,
    docstring: 'The total count of the delayed jobs '\
               'with errors per handler class).',
    labels: i[queue priority attempts handler]
  )

  @dj_performable_count = @registry.gauge(
    :delayed_jobs_performable_count,
    docstring: 'The total count of the delayed jobs '\
               'for the performable actions).',
    labels: i[queue priority attempts handler object method_name]
  )

  @dj_performable_failed_count = @registry.gauge(
    :delayed_jobs_performable_failed_count,
    docstring: 'The total count of the delayed jobs '\
               'for the performable actions).',
    labels: i[queue priority attempts handler object method_name]
  )
end

#jobs_handler(condition) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/delayed_job_metrics/exporter.rb', line 158

def jobs_handler(condition)
  Delayed::Job.where(condition.to_s)
              .each_with_object(Hash.new(0)) do |dj, counts|
    handler = dj.handler.to_s.match(
      %r{!ruby/object:([^\n]+)}
    ).to_a[1].to_s.gsub(/ {}/, '')
    key = [dj.queue, dj.priority, dj.attempts, handler]
    counts[key] += 1
  end
end

#jobs_methods(failed = false) ⇒ Object



169
170
171
172
173
174
175
176
# File 'lib/delayed_job_metrics/exporter.rb', line 169

def jobs_methods(failed = false)
  not_val = 'not ' if failed
  Delayed::Job.where("failed_at is #{not_val}NULL")
              .where("handler like '%Delayed::Performable%'")
              .reduce(Hash.new(0)) do |counts, dj|
    process(dj, counts)
  end
end

#process(job, counts) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/delayed_job_metrics/exporter.rb', line 178

def process(job, counts)
  handler_str = job.handler.to_s
  handler = handler_str.match(
    %r{!ruby/object:([^\n]+)}
  ).to_a[1].to_s.gsub(/ {}/, '')
  object = handler_str.match(
    %r{(serialized_)?object: !ruby/(class '|object:)([^(\n|')]+)}
  ).to_a[3]
  method_name = handler_str.match(/method_name: ([^\n]+)/).to_a[1]

  key = [job.queue, job.priority, job.attempts,
         handler, object, method_name]

  counts[key] += 1
  counts
end

#process_mertics_request(format) ⇒ Object



41
42
43
44
45
# File 'lib/delayed_job_metrics/exporter.rb', line 41

def process_mertics_request(format)
  reset_metrics
  collect_metrics
  respond_with(format)
end

#reset_metricsObject



47
48
49
50
51
# File 'lib/delayed_job_metrics/exporter.rb', line 47

def reset_metrics
  Prometheus::Client.registry.metrics.each do |metric|
    metric.values.keys.each { |key| metric.set(0, labels: key) }
  end
end