Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ptoffy committed Aug 28, 2024
1 parent 2b04e88 commit cbc2196
Show file tree
Hide file tree
Showing 7 changed files with 612 additions and 175 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "4.101.1"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.65.0"),
.package(url: "https://github.com/apple/swift-metrics.git", "2.0.0"),
.package(url: "https://github.com/apple/swift-metrics.git", from: "2.5.0"),
],
targets: [
.target(
Expand Down
4 changes: 3 additions & 1 deletion Package@swift-5.9.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ let package = Package(
],
products: [
.library(name: "Queues", targets: ["Queues"]),
.library(name: "XCTQueues", targets: ["XCTQueues"])
.library(name: "XCTQueues", targets: ["XCTQueues"]),
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "4.101.1"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.65.0"),
.package(url: "https://github.com/apple/swift-metrics.git", from: "2.5.0"),
],
targets: [
.target(
name: "Queues",
dependencies: [
.product(name: "Vapor", package: "vapor"),
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "Metrics", package: "swift-metrics"),
],
swiftSettings: swiftSettings
),
Expand Down
5 changes: 4 additions & 1 deletion Sources/Queues/AsyncQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ extension Queue {
logger.trace("Pusing job to queue")
try await self.push(id).get()
logger.info("Dispatched job")
Counter(label: "dispatched.jobs.counter", dimensions: [("queueName", self.queueName.string)]).increment()
Counter(label: "dispatched.jobs.counter", dimensions: [
("queueName", self.queueName.string),
("jobName", J.name),
]).increment()

await self.sendNotification(of: "dispatch", logger: logger) {
try await $0.dispatched(job: .init(id: id.string, queueName: self.queueName.string, jobData: storage), eventLoop: self.eventLoop).get()
Expand Down
11 changes: 5 additions & 6 deletions Sources/Queues/Queue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,11 @@ extension Queue {
logger.trace("Pusing job to queue")
return self.push(id)
}.flatMapWithEventLoop { _, eventLoop in
Counter(label: "dispatched.jobs.counter", dimensions: [("queueName", self.queueName.string)]).increment()
self.logger.info("Dispatched queue job", metadata: [
"job_id": .string(id.string),
"job_name": .string(job.name),
"queue": .string(self.queueName.string),
])
Counter(label: "dispatched.jobs.counter", dimensions: [
("queueName", self.queueName.string),
("jobName", J.name),
]).increment()
self.logger.info("Dispatched queue job")
return self.sendNotification(of: "dispatch", logger: logger) {
$0.dispatched(job: .init(id: id.string, queueName: self.queueName.string, jobData: storage), eventLoop: eventLoop)
}
Expand Down
14 changes: 8 additions & 6 deletions Tests/QueuesTests/MetricsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import XCTVapor

final class MetricsTests: XCTestCase {
var app: Application!
var metrics: CapturingMetricsSystem!
var metrics: TestMetrics!

override func setUp() async throws {
self.metrics = CapturingMetricsSystem()
self.metrics = TestMetrics()
MetricsSystem.bootstrapInternal(self.metrics)

self.app = try await Application.make(.testing)
Expand All @@ -38,7 +38,7 @@ final class MetricsTests: XCTestCase {

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

let timer = try XCTUnwrap(self.metrics.timers["some-id.jobDurationTimer"] as? TestTimer)
let timer = try XCTUnwrap(self.metrics.timers.first(where: { $0.label == "some-id.jobDurationTimer" }))
let successDimension = try XCTUnwrap(timer.dimensions.first(where: { $0.0 == "success" }))
let idDimension = try XCTUnwrap(timer.dimensions.first(where: { $0.0 == "id" }))
XCTAssertEqual(successDimension.1, "true")
Expand All @@ -62,7 +62,7 @@ final class MetricsTests: XCTestCase {
}

try await self.app.queues.queue.worker.run()
let counter = try XCTUnwrap(self.metrics.counters["success.completed.jobs.counter"] as? TestCounter)
let counter = try XCTUnwrap(self.metrics.counters.first(where: { $0.label == "success.completed.jobs.counter" }))
let queueNameDimension = try XCTUnwrap(counter.dimensions.first(where: { $0.0 == "queueName" }))
XCTAssertEqual(queueNameDimension.1, self.app.queues.queue.queueName.string)
}
Expand All @@ -82,7 +82,7 @@ final class MetricsTests: XCTestCase {
}

try await self.app.queues.queue.worker.run()
let counter = try XCTUnwrap(self.metrics.counters["error.completed.jobs.counter"] as? TestCounter)
let counter = try XCTUnwrap(self.metrics.counters.first(where: { $0.label == "error.completed.jobs.counter" }))
let queueNameDimension = try XCTUnwrap(counter.dimensions.first(where: { $0.0 == "queueName" }))
XCTAssertEqual(queueNameDimension.1, self.app.queues.queue.queueName.string)
}
Expand All @@ -104,8 +104,10 @@ final class MetricsTests: XCTestCase {

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

let counter = try XCTUnwrap(self.metrics.counters["dispatched.jobs.counter"] as? TestCounter)
let counter = try XCTUnwrap(self.metrics.counters.first(where: { $0.label == "dispatched.jobs.counter" }))
let queueNameDimension = try XCTUnwrap(counter.dimensions.first(where: { $0.0 == "queueName" }))
let jobNameDimension = try XCTUnwrap(counter.dimensions.first(where: { $0.0 == "jobName" }))
XCTAssertEqual(queueNameDimension.1, self.app.queues.queue.queueName.string)
XCTAssertEqual(jobNameDimension.1, MyAsyncJob.name)
}
}
160 changes: 0 additions & 160 deletions Tests/QueuesTests/Utilities/CapturingMetricsSystem.swift

This file was deleted.

Loading

0 comments on commit cbc2196

Please sign in to comment.