Skip to content

Commit

Permalink
feat: add prometheus metrics to worker
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Feb 24, 2024
1 parent 0fcf778 commit bea7450
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
33 changes: 30 additions & 3 deletions app/lib/worker/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,15 @@ def work(thread)
JOBS.each do |job_class|
capture_errors do
job = job_class.new(logger: logger)
job.call

time = Benchmark.realtime { job.call }

observe_prometheus_histogram :postal_worker_job_runtime,
time,
labels: {
thread: thread,
job: job_class.to_s.split("::").last
}

if job.work_completed?
completed_work += 1
Expand Down Expand Up @@ -222,7 +230,18 @@ def run_task(task)

logger.info "running task"

capture_errors { task.new(logger: logger).call }
time = 0
capture_errors do
time = Benchmark.realtime do
task.new(logger: logger).call
end

observe_prometheus_histogram :postal_worker_task_runtime,
time,
labels: {
task: task.to_s.split("::").last
}
end

next_run_after = task.next_run_after
logger.info "scheduling task to next run at #{next_run_after}"
Expand Down Expand Up @@ -254,12 +273,20 @@ def capture_errors

def setup_prometheus
register_prometheus_counter :postal_worker_job_executions,
docstring: "The number of jobs worked by a worker",
docstring: "The number of jobs worked by a worker where work was completed",
labels: [:thread, :job]

register_prometheus_histogram :postal_worker_job_runtime,
docstring: "The time taken to process jobs",
labels: [:thread, :job]

register_prometheus_counter :postal_worker_errors,
docstring: "The number of errors encountered while processing jobs",
labels: [:error]

register_prometheus_histogram :postal_worker_task_runtime,
docstring: "The time taken to process tasks",
labels: [:task]
end

end
Expand Down
12 changes: 12 additions & 0 deletions app/util/has_prometheus_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ def register_prometheus_counter(name, **kwargs)
registry.register(counter)
end

def register_prometheus_histogram(name, **kwargs)
histogram = Prometheus::Client::Histogram.new(name, **kwargs)
registry.register(histogram)
end

def increment_prometheus_counter(name, labels: {})
counter = registry.get(name)
return if counter.nil?

counter.increment(labels: labels)
end

def observe_prometheus_histogram(name, time, labels: {})
histogram = registry.get(name)
return if histogram.nil?

histogram.observe(time, labels: labels)
end

private

def registry
Expand Down

0 comments on commit bea7450

Please sign in to comment.