Skip to content

Commit

Permalink
Propagate displayUnit when using MultiplexMetricsHandler (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjcairo authored Feb 1, 2023
1 parent cbfde65 commit e8bced7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Sources/CoreMetrics/Metrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,10 @@ public final class MultiplexMetricsHandler: MetricsFactory {
func recordNanoseconds(_ duration: Int64) {
self.timers.forEach { $0.recordNanoseconds(duration) }
}

func preferDisplayUnit(_ unit: TimeUnit) {
self.timers.forEach { $0.preferDisplayUnit(unit) }
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion Tests/MetricsTests/CoreMetricsTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ extension MetricsTests {
("testTimerHandlesUnsignedOverflow", testTimerHandlesUnsignedOverflow),
("testGauge", testGauge),
("testGaugeBlock", testGaugeBlock),
("testMUX", testMUX),
("testMUX_Counter", testMUX_Counter),
("testMUX_Recorder", testMUX_Recorder),
("testMUX_Timer", testMUX_Timer),
("testCustomFactory", testCustomFactory),
("testDestroyingGauge", testDestroyingGauge),
("testDestroyingCounter", testDestroyingCounter),
Expand Down
44 changes: 40 additions & 4 deletions Tests/MetricsTests/CoreMetricsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,28 +361,64 @@ class MetricsTests: XCTestCase {
XCTAssertEqual(recorder.values[0].1, value, "expected value to match")
}

func testMUX() throws {
func testMUX_Counter() throws {
// bootstrap with our test metrics
let factories = [TestMetrics(), TestMetrics(), TestMetrics()]
MetricsSystem.bootstrapInternal(MultiplexMetricsHandler(factories: factories))
// run the test
let name = NSUUID().uuidString
let value = Int.random(in: Int.min ... Int.max)
let mux = Counter(label: name)
mux.increment(by: value)
let muxCounter = Counter(label: name)
muxCounter.increment(by: value)
factories.forEach { factory in
let counter = factory.counters.first?.1 as! TestCounter
XCTAssertEqual(counter.label, name, "expected label to match")
XCTAssertEqual(counter.values.count, 1, "expected number of entries to match")
XCTAssertEqual(counter.values[0].1, Int64(value), "expected value to match")
}
mux.reset()
muxCounter.reset()
factories.forEach { factory in
let counter = factory.counters.first?.1 as! TestCounter
XCTAssertEqual(counter.values.count, 0, "expected number of entries to match")
}
}

func testMUX_Recorder() throws {
// bootstrap with our test metrics
let factories = [TestMetrics(), TestMetrics(), TestMetrics()]
MetricsSystem.bootstrapInternal(MultiplexMetricsHandler(factories: factories))
// run the test
let name = NSUUID().uuidString
let value = Double.random(in: 0 ... 1)
let muxRecorder = Recorder(label: name)
muxRecorder.record(value)
factories.forEach { factory in
let recorder = factory.recorders.first?.1 as! TestRecorder
XCTAssertEqual(recorder.label, name, "expected label to match")
XCTAssertEqual(recorder.values.count, 1, "expected number of entries to match")
XCTAssertEqual(recorder.values[0].1, value, "expected value to match")
}
}

func testMUX_Timer() throws {
// bootstrap with our test metrics
let factories = [TestMetrics(), TestMetrics(), TestMetrics()]
MetricsSystem.bootstrapInternal(MultiplexMetricsHandler(factories: factories))
// run the test
let name = NSUUID().uuidString
let seconds = Int.random(in: 1 ... 10)
let muxTimer = Timer(label: name, preferredDisplayUnit: .minutes)
muxTimer.recordSeconds(seconds)
factories.forEach { factory in
let timer = factory.timers.first?.1 as! TestTimer
XCTAssertEqual(timer.label, name, "expected label to match")
XCTAssertEqual(timer.values.count, 1, "expected number of entries to match")
XCTAssertEqual(timer.values[0].1, Int64(seconds * 1_000_000_000), "expected value to match")
XCTAssertEqual(timer.displayUnit, .minutes, "expected value to match")
XCTAssertEqual(timer.retrieveValueInPreferredUnit(atIndex: 0), Double(seconds) / 60.0, "seconds should be returned as minutes")
}
}

func testCustomFactory() {
final class CustomHandler: CounterHandler {
func increment<DataType>(by: DataType) where DataType: BinaryInteger {}
Expand Down

0 comments on commit e8bced7

Please sign in to comment.