rename Counter::increment "value" parameter to "by" (#4)
* rename Counter::increment "value" parameter to "by" motivation: nicer API signature changes: rename Counter::increment and CounterHandler::increment "value" parameter to "by"
This commit is contained in:
parent
585a41d684
commit
4729bac3a1
|
|
@ -79,7 +79,7 @@ The API supports four metric types:
|
|||
`Counter`: A counter is a cumulative metric that represents a single monotonically increasing counter whose value can only increase or be reset to zero on restart. For example, you can use a counter to represent the number of requests served, tasks completed, or errors.
|
||||
|
||||
```swift
|
||||
counter.increment(100)
|
||||
counter.increment(by: 100)
|
||||
```
|
||||
|
||||
`Recorder`: A recorder collects observations within a time window (usually things like response sizes) and *can* provide aggregated information about the data sample, for example count, sum, min, max and various quantiles.
|
||||
|
|
@ -128,7 +128,7 @@ The `MetricsFactory` is responsible for instantiating the concrete metrics class
|
|||
|
||||
```swift
|
||||
public protocol CounterHandler: AnyObject {
|
||||
func increment<DataType: BinaryInteger>(_ value: DataType)
|
||||
func increment(by: Int64)
|
||||
}
|
||||
```
|
||||
|
||||
|
|
@ -173,9 +173,9 @@ class SimpleMetricsLibrary: MetricsFactory {
|
|||
|
||||
let lock = NSLock()
|
||||
var value: Int64 = 0
|
||||
func increment(_ value: Int64) {
|
||||
func increment(by amount: Int64) {
|
||||
self.lock.withLock {
|
||||
self.value += value
|
||||
self.value += amount
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,8 +27,8 @@ public protocol CounterHandler: AnyObject {
|
|||
/// Increment the counter.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - value: Amount to increment by.
|
||||
func increment(_ value: Int64)
|
||||
/// - by: Amount to increment by.
|
||||
func increment(by: Int64)
|
||||
/// Reset the counter back to zero.
|
||||
func reset()
|
||||
}
|
||||
|
|
@ -60,16 +60,16 @@ public class Counter {
|
|||
/// Increment the counter.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - value: Amount to increment by.
|
||||
/// - by: Amount to increment by.
|
||||
@inlinable
|
||||
public func increment<DataType: BinaryInteger>(_ value: DataType) {
|
||||
self.handler.increment(Int64(value))
|
||||
public func increment<DataType: BinaryInteger>(by amount: DataType) {
|
||||
self.handler.increment(by: Int64(amount))
|
||||
}
|
||||
|
||||
/// Increment the counter by one.
|
||||
@inlinable
|
||||
public func increment() {
|
||||
self.increment(1)
|
||||
self.increment(by: 1)
|
||||
}
|
||||
|
||||
/// Reset the counter back to zero.
|
||||
|
|
@ -394,8 +394,8 @@ public final class MultiplexMetricsHandler: MetricsFactory {
|
|||
self.counters = factories.map { $0.makeCounter(label: label, dimensions: dimensions) }
|
||||
}
|
||||
|
||||
func increment(_ value: Int64) {
|
||||
self.counters.forEach { $0.increment(value) }
|
||||
func increment(by amount: Int64) {
|
||||
self.counters.forEach { $0.increment(by: amount) }
|
||||
}
|
||||
|
||||
func reset() {
|
||||
|
|
@ -448,7 +448,7 @@ public final class NOOPMetricsHandler: MetricsFactory, CounterHandler, RecorderH
|
|||
return self
|
||||
}
|
||||
|
||||
public func increment(_: Int64) {}
|
||||
public func increment(by: Int64) {}
|
||||
public func reset() {}
|
||||
public func record(_: Int64) {}
|
||||
public func record(_: Double) {}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ class MetricsTests: XCTestCase {
|
|||
for _ in 0 ... total {
|
||||
group.enter()
|
||||
DispatchQueue(label: "\(name)-queue").async {
|
||||
counter.increment(Int.random(in: 0 ... 1000))
|
||||
counter.increment(by: Int.random(in: 0 ... 1000))
|
||||
group.leave()
|
||||
}
|
||||
}
|
||||
|
|
@ -45,7 +45,7 @@ class MetricsTests: XCTestCase {
|
|||
// run the test
|
||||
let name = "counter-\(NSUUID().uuidString)"
|
||||
let value = Int.random(in: Int.min ... Int.max)
|
||||
Counter(label: name).increment(value)
|
||||
Counter(label: name).increment(by: value)
|
||||
let counter = metrics.counters[name] as! TestCounter
|
||||
XCTAssertEqual(counter.values.count, 1, "expected number of entries to match")
|
||||
XCTAssertEqual(counter.values[0].1, Int64(value), "expected value to match")
|
||||
|
|
@ -215,7 +215,7 @@ class MetricsTests: XCTestCase {
|
|||
let name = NSUUID().uuidString
|
||||
let value = Int.random(in: Int.min ... Int.max)
|
||||
let mux = Counter(label: name)
|
||||
mux.increment(value)
|
||||
mux.increment(by: value)
|
||||
factories.forEach { factory in
|
||||
let counter = factory.counters.first?.1 as! TestCounter
|
||||
XCTAssertEqual(counter.label, name, "expected label to match")
|
||||
|
|
@ -231,7 +231,7 @@ class MetricsTests: XCTestCase {
|
|||
|
||||
func testCustomFactory() {
|
||||
class CustomHandler: CounterHandler {
|
||||
func increment<DataType>(_: DataType) where DataType: BinaryInteger {}
|
||||
func increment<DataType>(by: DataType) where DataType: BinaryInteger {}
|
||||
func reset() {}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -60,11 +60,11 @@ internal class TestCounter: CounterHandler, Equatable {
|
|||
self.dimensions = dimensions
|
||||
}
|
||||
|
||||
func increment(_ value: Int64) {
|
||||
func increment(by amount: Int64) {
|
||||
self.lock.withLock {
|
||||
self.values.append((Date(), value))
|
||||
self.values.append((Date(), amount))
|
||||
}
|
||||
print("adding \(value) to \(self.label)")
|
||||
print("adding \(amount) to \(self.label)")
|
||||
}
|
||||
|
||||
func reset() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue