Skip to content

Commit

Permalink
Remove job id from metrics (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler authored Jan 2, 2025
1 parent 6aeb0a1 commit acaf24d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
5 changes: 1 addition & 4 deletions Sources/Jobs/JobMetricsHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ internal enum JobMetricsHelper {
/// Update job metrics
/// - Parameters:
/// - name: String Job name
/// - jobID: String is only used for the completed meter
/// - startTime: UInt64 when the job started
/// - error: Error? job error
/// - retrying: Bool if the job is being retried
///
static func updateMetrics(
for name: String,
jobID: String,
startTime: UInt64,
error: Error? = nil,
retrying: Bool = false
Expand All @@ -56,8 +54,7 @@ internal enum JobMetricsHelper {
Meter(
label: JobMetricsHelper.meterLabel,
dimensions: [
("status", JobMetricsHelper.JobStatus.completed.rawValue),
("jobID", jobID),
("status", JobMetricsHelper.JobStatus.completed.rawValue)
]
).increment()

Expand Down
3 changes: 1 addition & 2 deletions Sources/Jobs/JobQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public struct JobQueue<Queue: JobQueueDriver>: Service {
Meter(
label: JobMetricsHelper.meterLabel,
dimensions: [
("status", JobMetricsHelper.JobStatus.queued.rawValue),
("jobID", id.description),
("status", JobMetricsHelper.JobStatus.queued.rawValue)
]
).increment()
self.logger.debug(
Expand Down
27 changes: 10 additions & 17 deletions Sources/Jobs/JobQueueHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,22 @@ final class JobQueueHandler<Queue: JobQueueDriver>: Sendable {
Meter(
label: JobMetricsHelper.meterLabel,
dimensions: [
("status", JobMetricsHelper.JobStatus.queued.rawValue),
("jobID", queuedJob.id.description),
("status", JobMetricsHelper.JobStatus.queued.rawValue)
]
).decrement()

Meter(
label: JobMetricsHelper.meterLabel,
dimensions: [
("status", JobMetricsHelper.JobStatus.processing.rawValue),
("jobID", queuedJob.id.description),
("status", JobMetricsHelper.JobStatus.processing.rawValue)
]
).increment()

defer {
Meter(
label: JobMetricsHelper.meterLabel,
dimensions: [
("status", JobMetricsHelper.JobStatus.processing.rawValue),
("jobID", queuedJob.id.description),
("status", JobMetricsHelper.JobStatus.processing.rawValue)
]
).decrement()
}
Expand All @@ -104,8 +101,7 @@ final class JobQueueHandler<Queue: JobQueueDriver>: Sendable {
Meter(
label: JobMetricsHelper.discardedMeter,
dimensions: [
("reason", "INVALID_JOB_ID"),
("jobID", queuedJob.id.description),
("reason", "INVALID_JOB_ID")
]
).increment()
return
Expand All @@ -115,8 +111,7 @@ final class JobQueueHandler<Queue: JobQueueDriver>: Sendable {
Meter(
label: JobMetricsHelper.discardedMeter,
dimensions: [
("reason", "DECODE_FAILED"),
("jobID", queuedJob.id.description),
("reason", "DECODE_FAILED")
]
).increment()
return
Expand Down Expand Up @@ -144,7 +139,6 @@ final class JobQueueHandler<Queue: JobQueueDriver>: Sendable {
try await self.queue.failed(jobId: queuedJob.id, error: error)
JobMetricsHelper.updateMetrics(
for: job.name,
jobID: queuedJob.id.description,
startTime: startTime,
error: error
)
Expand All @@ -155,7 +149,6 @@ final class JobQueueHandler<Queue: JobQueueDriver>: Sendable {
try await self.queue.failed(jobId: queuedJob.id, error: error)
JobMetricsHelper.updateMetrics(
for: job.name,
jobID: queuedJob.id.description,
startTime: startTime,
error: error
)
Expand All @@ -181,20 +174,20 @@ final class JobQueueHandler<Queue: JobQueueDriver>: Sendable {
Meter(
label: JobMetricsHelper.meterLabel,
dimensions: [
("status", JobMetricsHelper.JobStatus.queued.rawValue),
("jobID", newJobId.description),
("status", JobMetricsHelper.JobStatus.queued.rawValue)
]
).increment()

JobMetricsHelper.updateMetrics(
for: job.name,
jobID: queuedJob.id.description,
startTime: startTime,
retrying: true
)
logger.debug(
"Retrying Job",
metadata: [
"JobID": .stringConvertible(newJobId),
"JobName": .string(job.name),
"attempts": .stringConvertible(attempts),
"delayedUntil": .stringConvertible(delay),
]
Expand All @@ -203,10 +196,10 @@ final class JobQueueHandler<Queue: JobQueueDriver>: Sendable {
}
logger.debug("Finished Job")
try await self.queue.finished(jobId: queuedJob.id)
JobMetricsHelper.updateMetrics(for: job.name, jobID: queuedJob.id.description, startTime: startTime)
JobMetricsHelper.updateMetrics(for: job.name, startTime: startTime)
} catch {
logger.debug("Failed to set job status")
JobMetricsHelper.updateMetrics(for: job.name, jobID: queuedJob.id.description, startTime: startTime, error: error)
JobMetricsHelper.updateMetrics(for: job.name, startTime: startTime, error: error)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/JobsTests/MetricsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ final class MetricsTests: XCTestCase {

let processingMeter = try XCTUnwrap(Self.testMetrics.meters.withLockedValue { $0 }["swift.jobs.meter"] as? TestMeter)
XCTAssertEqual(processingMeter.values.withLockedValue { $0 }.count, 1)
XCTAssertEqual(processingMeter.dimensions.count, 2)
XCTAssertEqual(processingMeter.dimensions.count, 1)
XCTAssertEqual(processingMeter.dimensions[0].0, "status")
XCTAssertEqual(processingMeter.dimensions[0].1, "processing")
}
Expand Down Expand Up @@ -347,7 +347,7 @@ final class MetricsTests: XCTestCase {
}

let queuedMeter = try XCTUnwrap(Self.testMetrics.meters.withLockedValue { $0 }["swift.jobs.discarded"] as? TestMeter)
XCTAssertEqual(queuedMeter.dimensions.count, 2)
XCTAssertEqual(queuedMeter.dimensions.count, 1)
XCTAssertEqual(queuedMeter.dimensions[0].0, "reason")
XCTAssertEqual(queuedMeter.dimensions[0].1, "DECODE_FAILED")
}
Expand Down

0 comments on commit acaf24d

Please sign in to comment.