Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix usage of the Metrics Timer. #135

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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