From 0e88458a91ef0882bbdb17d3163d03e41e8643a3 Mon Sep 17 00:00:00 2001 From: Maciej Trybilo Date: Thu, 12 Sep 2024 12:51:10 +0200 Subject: [PATCH] Fix usage of the Metrics Timer. --- Sources/Queues/QueueWorker.swift | 10 +++++----- Tests/QueuesTests/MetricsTests.swift | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Sources/Queues/QueueWorker.swift b/Sources/Queues/QueueWorker.swift index f1a5bad..96d4970 100644 --- a/Sources/Queues/QueueWorker.swift +++ b/Sources/Queues/QueueWorker.swift @@ -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() } @@ -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) { @@ -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) diff --git a/Tests/QueuesTests/MetricsTests.swift b/Tests/QueuesTests/MetricsTests.swift index 74673f7..cfef6f8 100644 --- a/Tests/QueuesTests/MetricsTests.swift +++ b/Tests/QueuesTests/MetricsTests.swift @@ -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()) }