Skip to content

Add Prometheus metrics for jobs and queues - #84

Merged
knadh merged 5 commits into
zerodha:masterfrom
kalbhor:job-metrics
Oct 27, 2026
Merged

knadh merged 5 commits into
zerodha:masterfrom
kalbhor:job-metrics

Conversation

@kalbhor

@kalbhor kalbhor commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Adds Prometheus metrics for jobs and queues at /metrics. Builds on #70 (original commit kept) and addresses the review feedback there. Closes #46.

  • Counts jobs enqueued individually and in groups.
  • Tracks running jobs, attempts by outcome (success, error, canceled), execution time and queue wait time per task.
  • Exposes pending jobs and worker concurrency per queue.
  • Escapes label values so task names with quotes or backslashes don't crash startup.
  • Documents the metrics in the README. Worker-only instances don't run the HTTP server, so their metrics can't be scraped.

shreyaabaranwal and others added 5 commits July 6, 2026 15:34
Exposes job queue/execution metrics (queued, running, success, failed)
per task via /metrics endpoint using VictoriaMetrics client, following
the pattern from zerodha/kaf-relay.

Closes zerodha#46
- Count jobs enqueued in groups and rename the counter to jobs_enqueued_total.
- Escape label values so unusual task names no longer crash startup.
- Replace success/failed counters with job_attempts_total by outcome (success, error, canceled).
- Add execution duration, queue wait, pending jobs and worker concurrency metrics.
- Document the metrics and the worker-only limitation in the README.
@knadh
knadh merged commit 77e800e into zerodha:master Oct 27, 2026
1 check passed

@shreyaabaranwal shreyaabaranwal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went through the final diff after this landed and compared it with my original commit from #70. Thanks for picking it up and keeping the original commit, @kalbhor.

A few things I learned from the changes, mostly noting them for myself:

  • Escaping label values. I hadn't thought about task names with quotes. Since the metrics are created in initQueue(), a bad name would have stopped the server from starting at all.
  • job_attempts_total with an outcome label instead of separate success/failed counters. My callback ran once per attempt, so with retries my counters were really counting attempts anyway. Splitting out canceled also stops deleted jobs from showing up as failures.
  • Checking ctx.Err() instead of comparing with context.Canceled, since pq returns its own error on cancel.
  • Measuring queue wait only on the first attempt, and keeping ReadyAt safe for jobs that older versions enqueued without it.

I also checked the GOMAXPROCS fallback in registerQueueMetrics against tasqueue. It matches the default NewServer sets, so the concurrency gauge is right for tasks without a concurrency value.

One thing I noticed: the pending jobs gauge calls GetPending() on every scrape, which is a full LRANGE on Redis. tasqueue v2.3.0 has GetPendingCount() on the Broker interface, so I've opened #86 to switch to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Debug Shell for dungbeetle

3 participants