Skip to content

Commit

Permalink
metrics should include their labels when printed to ease debugging (#82)
Browse files Browse the repository at this point in the history
motivation: ease debugging

changes: confirm metrics types to CustomStringConvertible
  • Loading branch information
ktoso authored Oct 8, 2020
1 parent cf757fe commit 5702ee1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Sources/CoreMetrics/Metrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public class Counter {
}
}

extension Counter: CustomStringConvertible {
public var description: String {
return "Counter(\(self.label), dimensions: \(self.dimensions))"
}
}

public extension Recorder {
/// Create a new `Recorder`.
///
Expand Down Expand Up @@ -158,6 +164,12 @@ public class Recorder {
}
}

extension Recorder: CustomStringConvertible {
public var description: String {
return "\(type(of: self))(\(self.label), dimensions: \(self.dimensions), aggregate: \(self.aggregate))"
}
}

/// A gauge is a metric that represents a single numerical value that can arbitrarily go up and down.
/// Gauges are typically used for measured values like temperatures or current memory usage, but also "counts" that can go up and down, like the number of active threads.
/// Gauges are modeled as `Recorder` with a sample size of 1 and that does not perform any aggregation.
Expand Down Expand Up @@ -356,6 +368,12 @@ public class Timer {
}
}

extension Timer: CustomStringConvertible {
public var description: String {
return "Timer(\(self.label), dimensions: \(self.dimensions))"
}
}

/// The `MetricsSystem` is a global facility where the default metrics backend implementation (`MetricsFactory`) can be
/// configured. `MetricsSystem` is set up just once in a given program to set up the desired metrics backend
/// implementation.
Expand Down
1 change: 1 addition & 0 deletions Tests/MetricsTests/CoreMetricsTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ extension MetricsTests {
("testDestroyingGauge", testDestroyingGauge),
("testDestroyingCounter", testDestroyingCounter),
("testDestroyingTimer", testDestroyingTimer),
("testDescriptions", testDescriptions),
]
}
}
17 changes: 17 additions & 0 deletions Tests/MetricsTests/CoreMetricsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,21 @@ class MetricsTests: XCTestCase {
let identityAgain = ObjectIdentifier(timerAgain)
XCTAssertNotEqual(identity, identityAgain, "since the cached metric was released, the created a new should have a different identity")
}

func testDescriptions() throws {
let metrics = TestMetrics()
MetricsSystem.bootstrapInternal(metrics)

let timer = Timer(label: "hello.timer")
XCTAssertEqual("\(timer)", "Timer(hello.timer, dimensions: [])")

let counter = Counter(label: "hello.counter")
XCTAssertEqual("\(counter)", "Counter(hello.counter, dimensions: [])")

let gauge = Gauge(label: "hello.gauge")
XCTAssertEqual("\(gauge)", "Gauge(hello.gauge, dimensions: [], aggregate: false)")

let recorder = Recorder(label: "hello.recorder")
XCTAssertEqual("\(recorder)", "Recorder(hello.recorder, dimensions: [], aggregate: true)")
}
}

0 comments on commit 5702ee1

Please sign in to comment.