-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
expose job stats as prometheus metrics endpoint
show information about how many jobs of each kind are scheduled
- Loading branch information
Showing
5 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'prometheus/middleware/exporter' | ||
require 'prometheus/job_stats' | ||
|
||
prometheus = Prometheus::Client.registry | ||
|
||
scheduled_job_stats = Prometheus::JobStats.new(:scheduled_jobs, 'Que Jobs to be executed') | ||
scheduled_job_stats | ||
prometheus.register(scheduled_job_stats) | ||
|
||
retried_job_stats = Prometheus::JobStats.new(:retried_jobs, 'Que Jobs to retried') | ||
retried_job_stats.filter('retries > 0') | ||
prometheus.register(retried_job_stats) | ||
|
||
|
||
pending_job_stats = Prometheus::JobStats.new(:pending_jobs, 'Que Jobs that should be already running') | ||
pending_job_stats.filter('run_at < now()') | ||
prometheus.register(pending_job_stats) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
require 'prometheus/client/metric' | ||
|
||
module Prometheus | ||
|
||
# Prometheus metric to get job stats from Que. | ||
class JobStats < Prometheus::Client::Metric | ||
|
||
def initialize(*) | ||
super | ||
@filter = nil | ||
end | ||
|
||
CTE = <<~SQL | ||
WITH jobs AS ( | ||
SELECT *, args::jsonb -> 0 AS options FROM que_jobs | ||
), | ||
jobs_list AS ( | ||
SELECT options->>'job_class' AS job, | ||
options->>'job_id' AS job_uuid, | ||
(options->>'executions')::integer AS retries, | ||
options->'arguments' AS arguments, | ||
run_at, job_id FROM jobs | ||
) | ||
SQL | ||
|
||
def type | ||
:job_stats | ||
end | ||
|
||
def filter(sql) | ||
@filter = sql | ||
end | ||
|
||
def values | ||
synchronize do | ||
job_stats.map do |summary| | ||
[ { job: summary.fetch('job') }, summary.fetch('count') ] | ||
end.to_h | ||
end | ||
end | ||
|
||
protected | ||
|
||
def job_stats | ||
filter = "WHERE #{@filter}" if @filter | ||
sql = <<~SQL | ||
SELECT job, COUNT(*) FROM jobs_list #{filter} GROUP BY job | ||
SQL | ||
Que.execute(CTE + sql) | ||
end | ||
end | ||
end |