-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
# collects stats from GoodJob | ||
module PrometheusExporter::Instrumentation | ||
class GoodJob < PeriodicStats | ||
def self.start(client: nil, frequency: 30) | ||
good_job_collector = new | ||
client ||= PrometheusExporter::Client.default | ||
|
||
worker_loop do | ||
client.send_json(good_job_collector.collect) | ||
end | ||
|
||
super | ||
end | ||
|
||
def collect | ||
{ | ||
type: "good_job", | ||
scheduled: ::GoodJob::Job.scheduled.size, | ||
retried: ::GoodJob::Job.retried.size, | ||
queued: ::GoodJob::Job.queued.size, | ||
running: ::GoodJob::Job.running.size, | ||
finished: ::GoodJob::Job.finished.size, | ||
succeeded: ::GoodJob::Job.succeeded.size, | ||
discarded: ::GoodJob::Job.discarded.size | ||
} | ||
end | ||
end | ||
end |
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,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module PrometheusExporter::Server | ||
class GoodJobCollector < TypeCollector | ||
MAX_GOOD_JOB_METRIC_AGE = 30 | ||
GOOD_JOB_GAUGES = { | ||
scheduled: "Total number of scheduled GoodJob jobs.", | ||
retried: "Total number of retried GoodJob jobs.", | ||
queued: "Total number of queued GoodJob jobs.", | ||
running: "Total number of running GoodJob jobs.", | ||
finished: "Total number of finished GoodJob jobs.", | ||
succeeded: "Total number of succeeded GoodJob jobs.", | ||
discarded: "Total number of discarded GoodJob jobs." | ||
} | ||
|
||
def initialize | ||
@good_job_metrics = [] | ||
@gauges = {} | ||
end | ||
|
||
def type | ||
"good_job" | ||
end | ||
|
||
def metrics | ||
return [] if good_job_metrics.length == 0 | ||
|
||
good_job_metrics.map do |metric| | ||
labels = metric.fetch("custom_labels", {}) | ||
|
||
GOOD_JOB_GAUGES.map do |name, help| | ||
gauge = gauges[name] ||= PrometheusExporter::Metric::Gauge.new("good_job_#{name}", help) | ||
gauge.observe(metric.fetch(name.to_s), labels) | ||
end | ||
end | ||
|
||
gauges.values | ||
end | ||
|
||
def collect(object) | ||
now = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) | ||
|
||
object["created_at"] = now | ||
good_job_metrics.delete_if { |metric| metric["created_at"] + MAX_GOOD_JOB_METRIC_AGE < now } | ||
good_job_metrics << object | ||
end | ||
|
||
private | ||
|
||
attr_reader :good_job_metrics, :gauges | ||
end | ||
end |