diff --git a/Sources/CoreMetrics/Metrics.swift b/Sources/CoreMetrics/Metrics.swift index 0225044..de48f14 100644 --- a/Sources/CoreMetrics/Metrics.swift +++ b/Sources/CoreMetrics/Metrics.swift @@ -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`. /// @@ -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. @@ -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. diff --git a/Tests/MetricsTests/CoreMetricsTests+XCTest.swift b/Tests/MetricsTests/CoreMetricsTests+XCTest.swift index 2b01d07..2f0274b 100644 --- a/Tests/MetricsTests/CoreMetricsTests+XCTest.swift +++ b/Tests/MetricsTests/CoreMetricsTests+XCTest.swift @@ -43,6 +43,7 @@ extension MetricsTests { ("testDestroyingGauge", testDestroyingGauge), ("testDestroyingCounter", testDestroyingCounter), ("testDestroyingTimer", testDestroyingTimer), + ("testDescriptions", testDescriptions), ] } } diff --git a/Tests/MetricsTests/CoreMetricsTests.swift b/Tests/MetricsTests/CoreMetricsTests.swift index ddb38f5..eeaaca0 100644 --- a/Tests/MetricsTests/CoreMetricsTests.swift +++ b/Tests/MetricsTests/CoreMetricsTests.swift @@ -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)") + } }