Skip to content

Commit db681ab

Browse files
committed
delayed_jobs_ready metric added to DelayedJob plugin
1 parent 83a8979 commit db681ab

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ end
538538
| Counter | `delayed_jobs_total` | Total number of delayed jobs executed | `job_name` |
539539
| Gauge | `delayed_jobs_enqueued` | Number of enqueued delayed jobs | - |
540540
| Gauge | `delayed_jobs_pending` | Number of pending delayed jobs | - |
541+
| Gauge | `delayed_jobs_ready` | Number of ready delayed jobs      | - |
541542
| Counter | `delayed_failed_jobs_total` | Total number failed delayed jobs executed | `job_name` |
542543
| Counter | `delayed_jobs_max_attempts_reached_total` | Total number of delayed jobs that reached max attempts | - |
543544
| Summary | `delayed_job_duration_seconds_summary` | Summary of the time it takes jobs to execute | `status` |
@@ -884,7 +885,7 @@ prometheus_exporter -p 8080 \
884885
--prefix 'foo_'
885886
```
886887

887-
You can use `-b` option to bind the `prometheus_exporter` web server to any IPv4 interface with `-b 0.0.0.0`,
888+
You can use `-b` option to bind the `prometheus_exporter` web server to any IPv4 interface with `-b 0.0.0.0`,
888889
any IPv6 interface with `-b ::`, or `-b ANY` to any IPv4/IPv6 interfaces available on your host system.
889890

890891
#### Enabling Basic Authentication

lib/prometheus_exporter/instrumentation/delayed_job.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module PrometheusExporter::Instrumentation
44
class DelayedJob
55
JOB_CLASS_REGEXP = %r{job_class: (\w+:{0,2})+}.freeze
66

7+
RuntimeMetric = Struct.new(:max_attempts, :enqueued_count, :pending_count, :ready_count)
8+
79
class << self
810
def register_plugin(client: nil)
911
instrumenter = self.new(client: client)
@@ -15,7 +17,8 @@ def register_plugin(client: nil)
1517
max_attempts = Delayed::Worker.max_attempts
1618
enqueued_count = Delayed::Job.where(queue: job.queue).count
1719
pending_count = Delayed::Job.where(attempts: 0, locked_at: nil, queue: job.queue).count
18-
instrumenter.call(job, max_attempts, enqueued_count, pending_count, *args, &block)
20+
ready_count = Delayed::Job.where(queue: job.queue, run_at: ..Time.current).count
21+
instrumenter.call(job, max_attempts, enqueued_count, pending_count, ready_count, *args, &block)
1922
end
2023
end
2124
end
@@ -28,7 +31,7 @@ def initialize(client: nil)
2831
@client = client || PrometheusExporter::Client.default
2932
end
3033

31-
def call(job, max_attempts, enqueued_count, pending_count, *args, &block)
34+
def call(job, max_attempts, enqueued_count, pending_count, ready_count, *args, &block)
3235
success = false
3336
start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
3437
latency = Time.current - job.run_at
@@ -49,7 +52,8 @@ def call(job, max_attempts, enqueued_count, pending_count, *args, &block)
4952
attempts: attempts,
5053
max_attempts: max_attempts,
5154
enqueued: enqueued_count,
52-
pending: pending_count
55+
pending: pending_count,
56+
ready_count: ready_count
5357
)
5458
end
5559
end

lib/prometheus_exporter/server/delayed_job_collector.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def initialize
1313
@delayed_job_attempts_summary = nil
1414
@delayed_jobs_enqueued = nil
1515
@delayed_jobs_pending = nil
16+
@delayed_jobs_ready = nil
1617
end
1718

1819
def type
@@ -36,6 +37,7 @@ def collect(obj)
3637
@delayed_job_attempts_summary.observe(obj["attempts"], counter_labels) if obj["success"]
3738
@delayed_jobs_enqueued.observe(obj["enqueued"], gauge_labels)
3839
@delayed_jobs_pending.observe(obj["pending"], gauge_labels)
40+
@delayed_jobs_ready.observe(obj["ready"], gauge_labels)
3941
end
4042

4143
def metrics
@@ -73,6 +75,11 @@ def ensure_delayed_job_metrics
7375
PrometheusExporter::Metric::Gauge.new(
7476
"delayed_jobs_pending", "Number of pending delayed jobs.")
7577

78+
@delayed_jobs_ready =
79+
PrometheusExporter::Metric::Gauge.new(
80+
"delayed_jobs_ready", "Number of ready delayed jobs."
81+
)
82+
7683
@delayed_failed_jobs_total =
7784
PrometheusExporter::Metric::Counter.new(
7885
"delayed_failed_jobs_total", "Total number failed delayed jobs executed.")

0 commit comments

Comments
 (0)