Skip to content

Commit

Permalink
Add Gauge for in-progress jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
ptoffy committed Nov 25, 2024
1 parent c502c4a commit f15eeee
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions Sources/Queues/QueueWorker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension Queue {
/// The worker that runs ``Job``s.
public struct QueueWorker: Sendable {
let queue: any Queue

/// Run the queue until there is no more work to be done.
/// This is a thin wrapper for ELF-style callers.
public func run() -> EventLoopFuture<Void> {
Expand All @@ -35,6 +35,11 @@ public struct QueueWorker: Sendable {
logger[metadataKey: "queue"] = "\(self.queue.queueName.string)"
logger.trace("Popping job from queue")

Gauge(
label: "jobs.in.progress.gauge",
dimensions: [("queueName", self.queue.queueName.string)]
).record(1)

guard let id = try await self.queue.pop().get() else {
// No job found, go around again.
logger.trace("No pending jobs")
Expand Down Expand Up @@ -122,17 +127,15 @@ public struct QueueWorker: Sendable {
queue: any Queue,
error: (any Error)? = nil
) {
// Checks how long the job took to complete
Timer(
label: "\(jobName).jobDurationTimer",
label: "\(jobName).duration.timer",
dimensions: [
("success", error == nil ? "true" : "false"),
("jobName", jobName),
],
preferredDisplayUnit: .seconds
preferredDisplayUnit: .milliseconds
).recordNanoseconds(DispatchTime.now().uptimeNanoseconds - startTime)

// Adds the completed job to a different counter depending on its result
if error != nil {
Counter(
label: "error.completed.jobs.counter",
Expand All @@ -144,5 +147,10 @@ public struct QueueWorker: Sendable {
dimensions: [("queueName", queue.queueName.string)]
).increment()
}

Gauge(
label: "jobs.in.progress.gauge",
dimensions: [("queueName", queue.queueName.string)]
).record(-1)
}
}

0 comments on commit f15eeee

Please sign in to comment.