Skip to content

Commit 971ba26

Browse files
authored
add increment and decrement public API to Meter (#132)
motivation: expose increment and decrement public APIs changes: * add increment and decrement public API to Meter * improve the implementation of AccumulatingMeter and TestMeter * add tests for new APIs * refactor/modernize other tests
1 parent bf7ea93 commit 971ba26

File tree

6 files changed

+299
-61
lines changed

6 files changed

+299
-61
lines changed

Sources/CoreMetrics/Metrics.swift

+66
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,36 @@ public final class Meter {
231231
public func set<DataType: BinaryFloatingPoint>(_ value: DataType) {
232232
self._handler.set(Double(value))
233233
}
234+
235+
/// Increment the Meter.
236+
///
237+
/// - parameters:
238+
/// - by: Amount to increment by.
239+
@inlinable
240+
public func increment<DataType: BinaryFloatingPoint>(by amount: DataType) {
241+
self._handler.increment(by: Double(amount))
242+
}
243+
244+
/// Increment the Meter by one.
245+
@inlinable
246+
public func increment() {
247+
self.increment(by: 1.0)
248+
}
249+
250+
/// Decrement the Meter.
251+
///
252+
/// - parameters:
253+
/// - by: Amount to decrement by.
254+
@inlinable
255+
public func decrement<DataType: BinaryFloatingPoint>(by amount: DataType) {
256+
self._handler.decrement(by: Double(amount))
257+
}
258+
259+
/// Decrement the Meter by one.
260+
@inlinable
261+
public func decrement() {
262+
self.decrement(by: 1.0)
263+
}
234264
}
235265

236266
extension Meter {
@@ -787,6 +817,24 @@ internal final class AccumulatingMeter: MeterHandler {
787817
}
788818

789819
func increment(by amount: Double) {
820+
// Drop illegal values
821+
// - cannot increment by NaN
822+
guard !amount.isNaN else {
823+
return
824+
}
825+
// - cannot increment by infinite quantities
826+
guard !amount.isInfinite else {
827+
return
828+
}
829+
// - cannot increment by negative values
830+
guard amount.sign == .plus else {
831+
return
832+
}
833+
// - cannot increment by zero
834+
guard !amount.isZero else {
835+
return
836+
}
837+
790838
let newValue: Double = self.lock.withLock {
791839
self.value += amount
792840
return self.value
@@ -795,6 +843,24 @@ internal final class AccumulatingMeter: MeterHandler {
795843
}
796844

797845
func decrement(by amount: Double) {
846+
// Drop illegal values
847+
// - cannot decrement by NaN
848+
guard !amount.isNaN else {
849+
return
850+
}
851+
// - cannot decrement by infinite quantities
852+
guard !amount.isInfinite else {
853+
return
854+
}
855+
// - cannot decrement by negative values
856+
guard amount.sign == .plus else {
857+
return
858+
}
859+
// - cannot decrement by zero
860+
guard !amount.isZero else {
861+
return
862+
}
863+
798864
let newValue: Double = self.lock.withLock {
799865
self.value -= amount
800866
return self.value

Sources/MetricsTestKit/TestMetrics.swift

+44-8
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public final class TestMetrics: MetricsFactory {
5757
// nothing to do
5858
}
5959

60-
/// Reset method to destroy all created ``TestCounter``, ``TestRecorder`` and ``TestTimer``.
60+
/// Reset method to destroy all created ``TestCounter``, ``TestMeter``, ``TestRecorder`` and ``TestTimer``.
6161
/// Invoke this method in between test runs to verify that Counters are created as needed.
6262
public func reset() {
6363
self.lock.withLock {
@@ -361,7 +361,7 @@ public final class TestMeter: TestMetric, MeterHandler, Equatable {
361361
private var _values = [(Date, Double)]()
362362

363363
init(label: String, dimensions: [(String, String)]) {
364-
self.id = NSUUID().uuidString
364+
self.id = UUID().uuidString
365365
self.label = label
366366
self.dimensions = dimensions
367367
}
@@ -378,18 +378,54 @@ public final class TestMeter: TestMetric, MeterHandler, Equatable {
378378
}
379379

380380
public func increment(by amount: Double) {
381+
// Drop illegal values
382+
// - cannot increment by NaN
383+
guard !amount.isNaN else {
384+
return
385+
}
386+
// - cannot increment by infinite quantities
387+
guard !amount.isInfinite else {
388+
return
389+
}
390+
// - cannot increment by negative values
391+
guard amount.sign == .plus else {
392+
return
393+
}
394+
// - cannot increment by zero
395+
guard !amount.isZero else {
396+
return
397+
}
398+
381399
self.lock.withLock {
382-
let lastValue = self._values.last?.1 ?? 0
383-
let newValue = lastValue - amount
384-
_values.append((Date(), Double(newValue)))
400+
let lastValue: Double = self._values.last?.1 ?? 0
401+
let newValue = lastValue + amount
402+
_values.append((Date(), newValue))
385403
}
386404
}
387405

388406
public func decrement(by amount: Double) {
407+
// Drop illegal values
408+
// - cannot decrement by NaN
409+
guard !amount.isNaN else {
410+
return
411+
}
412+
// - cannot decrement by infinite quantities
413+
guard !amount.isInfinite else {
414+
return
415+
}
416+
// - cannot decrement by negative values
417+
guard amount.sign == .plus else {
418+
return
419+
}
420+
// - cannot decrement by zero
421+
guard !amount.isZero else {
422+
return
423+
}
424+
389425
self.lock.withLock {
390-
let lastValue = self._values.last?.1 ?? 0
426+
let lastValue: Double = self._values.last?.1 ?? 0
391427
let newValue = lastValue - amount
392-
_values.append((Date(), Double(newValue)))
428+
_values.append((Date(), newValue))
393429
}
394430
}
395431

@@ -428,7 +464,7 @@ public final class TestRecorder: TestMetric, RecorderHandler, Equatable {
428464
private var _values = [(Date, Double)]()
429465

430466
init(label: String, dimensions: [(String, String)], aggregate: Bool) {
431-
self.id = NSUUID().uuidString
467+
self.id = UUID().uuidString
432468
self.label = label
433469
self.dimensions = dimensions
434470
self.aggregate = aggregate

Tests/MetricsTests/CoreMetricsTests+XCTest.swift

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ extension MetricsTests {
4646
("testGaugeBlock", testGaugeBlock),
4747
("testMeter", testMeter),
4848
("testMeterBlock", testMeterBlock),
49+
("testMeterInt", testMeterInt),
50+
("testMeterFloat", testMeterFloat),
51+
("testMeterIncrement", testMeterIncrement),
52+
("testMeterDecrement", testMeterDecrement),
53+
("testDefaultMeterIgnoresNan", testDefaultMeterIgnoresNan),
54+
("testDefaultMeterIgnoresInfinity", testDefaultMeterIgnoresInfinity),
55+
("testDefaultMeterIgnoresNegativeValues", testDefaultMeterIgnoresNegativeValues),
56+
("testDefaultMeterIgnoresZero", testDefaultMeterIgnoresZero),
4957
("testMUX_Counter", testMUX_Counter),
5058
("testMUX_Meter", testMUX_Meter),
5159
("testMUX_Recorder", testMUX_Recorder),

0 commit comments

Comments
 (0)