Skip to content

Commit

Permalink
Fix usage of the Metrics Timer. (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejtrybilo authored Sep 12, 2024
1 parent be4ac72 commit 2d38cd2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions Sources/Queues/QueueWorker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public struct QueueWorker: Sendable {
try await job._dequeue(self.queue.context, id: id.string, payload: jobData.payload).get()

logger.trace("Job ran successfully", metadata: ["attempts-made": "\(jobData.currentAttempt)"])
self.updateMetrics(for: id, startTime: startTime, queue: self.queue)
self.updateMetrics(for: jobData.jobName, startTime: startTime, queue: self.queue)
await self.queue.sendNotification(of: "success", logger: logger) {
try await $0.success(jobId: id.string, eventLoop: self.queue.context.eventLoop).get()
}
Expand All @@ -86,7 +86,7 @@ public struct QueueWorker: Sendable {
return try await self.retry(id: id, job: job, jobData: jobData, error: error, logger: logger)
} else {
logger.warning("Job failed, no retries remaining", metadata: ["error": "\(error)", "attempts-made": "\(jobData.currentAttempt)"])
self.updateMetrics(for: id, startTime: startTime, queue: self.queue, error: error)
self.updateMetrics(for: jobData.jobName, startTime: startTime, queue: self.queue, error: error)

try await job._error(self.queue.context, id: id.string, error, payload: jobData.payload).get()
await self.queue.sendNotification(of: "failure", logger: logger) {
Expand Down Expand Up @@ -117,17 +117,17 @@ public struct QueueWorker: Sendable {
}

private func updateMetrics(
for id: JobIdentifier,
for jobName: String,
startTime: UInt64,
queue: any Queue,
error: (any Error)? = nil
) {
// Checks how long the job took to complete
Timer(
label: "\(id.string).jobDurationTimer",
label: "\(jobName).jobDurationTimer",
dimensions: [
("success", error == nil ? "true" : "false"),
("id", id.string),
("jobName", jobName),
],
preferredDisplayUnit: .seconds
).recordNanoseconds(DispatchTime.now().uptimeNanoseconds - startTime)
Expand Down
6 changes: 3 additions & 3 deletions Tests/QueuesTests/MetricsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ final class MetricsTests: XCTestCase {

try await self.app.queues.queue.worker.run()

let timer = try XCTUnwrap(self.metrics.timers.first(where: { $0.label == "some-id.jobDurationTimer" }))
let timer = try XCTUnwrap(self.metrics.timers.first(where: { $0.label == "MyAsyncJob.jobDurationTimer" }))
let successDimension = try XCTUnwrap(timer.dimensions.first(where: { $0.0 == "success" }))
let idDimension = try XCTUnwrap(timer.dimensions.first(where: { $0.0 == "id" }))
let idDimension = try XCTUnwrap(timer.dimensions.first(where: { $0.0 == "jobName" }))
XCTAssertEqual(successDimension.1, "true")
XCTAssertEqual(idDimension.1, "some-id")
XCTAssertEqual(idDimension.1, "MyAsyncJob")

try XCTAssertNoThrow(promise.futureResult.wait())
}
Expand Down

0 comments on commit 2d38cd2

Please sign in to comment.