Skip to content

Commit f3acccc

Browse files
authored
Timer units (#17)
* package.resolved * WIP: Implement Time Units * Wrap tests & Up NIO version * Apply suggestions from code review Co-Authored-By: Joe Smith <[email protected]> * Update linux tests
1 parent b6e38ea commit f3acccc

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let package = Package(
1313
targets: ["PrometheusExample"]),
1414
],
1515
dependencies: [
16-
.package(url: "https://github.com/apple/swift-metrics.git", from: "1.0.0"),
16+
.package(url: "https://github.com/apple/swift-metrics.git", from: "1.2.0"),
1717
.package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"),
1818
],
1919
targets: [

Sources/Prometheus/MetricTypes/Summary.swift

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import NIOConcurrencyHelpers
2+
import enum CoreMetrics.TimeUnit
23

34
/// Label type Summaries can use
45
public protocol SummaryLabels: MetricLabels {
@@ -29,6 +30,8 @@ public class PromSummary<NumType: DoubleRepresentable, Labels: SummaryLabels>: P
2930
/// Type of the metric, used for formatting
3031
public let _type: PromMetricType = .summary
3132

33+
private var displayUnit: TimeUnit?
34+
3235
/// Labels for this Summary
3336
internal private(set) var labels: Labels
3437

@@ -64,6 +67,8 @@ public class PromSummary<NumType: DoubleRepresentable, Labels: SummaryLabels>: P
6467

6568
self.prometheus = p
6669

70+
self.displayUnit = nil
71+
6772
self.sum = .init("\(self.name)_sum", nil, 0, p)
6873

6974
self.count = .init("\(self.name)_count", nil, 0, p)
@@ -92,24 +97,24 @@ public class PromSummary<NumType: DoubleRepresentable, Labels: SummaryLabels>: P
9297
let (q, v) = arg
9398
self.labels.quantile = "\(q)"
9499
let labelsString = encodeLabels(self.labels)
95-
output.append("\(self.name)\(labelsString) \(v)")
100+
output.append("\(self.name)\(labelsString) \(format(v))")
96101
}
97102

98103
let labelsString = encodeLabels(self.labels, ["quantile"])
99104
output.append("\(self.name)_count\(labelsString) \(self.count.get())")
100-
output.append("\(self.name)_sum\(labelsString) \(self.sum.get())")
105+
output.append("\(self.name)_sum\(labelsString) \(format(self.sum.get().doubleValue))")
101106

102107
self.subSummaries.forEach { subSum in
103108
calculateQuantiles(quantiles: self.quantiles, values: subSum.values.map { $0.doubleValue }).sorted { $0.key < $1.key }.forEach { (arg) in
104109
let (q, v) = arg
105110
subSum.labels.quantile = "\(q)"
106111
let labelsString = encodeLabels(subSum.labels)
107-
output.append("\(subSum.name)\(labelsString) \(v)")
112+
output.append("\(subSum.name)\(labelsString) \(format(v))")
108113
}
109114

110115
let labelsString = encodeLabels(subSum.labels, ["quantile"])
111116
output.append("\(subSum.name)_count\(labelsString) \(subSum.count.get())")
112-
output.append("\(subSum.name)_sum\(labelsString) \(subSum.sum.get())")
117+
output.append("\(subSum.name)_sum\(labelsString) \(format(subSum.sum.get().doubleValue))")
113118
subSum.labels.quantile = ""
114119
}
115120

@@ -119,6 +124,24 @@ public class PromSummary<NumType: DoubleRepresentable, Labels: SummaryLabels>: P
119124
}
120125
}
121126

127+
private func format(_ v: Double) -> Double {
128+
guard let displayUnit = self.displayUnit else { return v }
129+
switch displayUnit {
130+
case .days: return (v / 1_000_000_000) * 60 * 60 * 24
131+
case .hours: return (v / 1_000_000_000) * 60 * 60
132+
case .minutes: return (v / 1_000_000_000) * 60
133+
case .seconds: return v / 1_000_000_000
134+
case .milliseconds: return v / 1_000_000
135+
case .nanoseconds: return v
136+
}
137+
}
138+
139+
internal func preferDisplayUnit(_ unit: TimeUnit) {
140+
self.lock.withLock {
141+
self.displayUnit = unit
142+
}
143+
}
144+
122145
/// Record a value
123146
///
124147
/// - Parameters:

Sources/Prometheus/PrometheusMetrics.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ private class MetricsSummary: TimerHandler {
6868
let summary: PromSummary<Int64, DimensionSummaryLabels>
6969
let labels: DimensionSummaryLabels?
7070

71+
func preferDisplayUnit(_ unit: TimeUnit) {
72+
self.summary.preferDisplayUnit(unit)
73+
}
74+
7175
internal init(summary: PromSummary<Int64, DimensionSummaryLabels>, dimensions: [(String, String)]) {
7276
self.summary = summary
7377
guard !dimensions.isEmpty else {

Tests/SwiftPrometheusTests/PrometheusMetricsTests.swift

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ final class PrometheusMetricsTests: XCTestCase {
133133
my_summary{quantile="0.99"} 10000.0
134134
my_summary{quantile="0.999"} 10000.0
135135
my_summary_count 5
136-
my_summary_sum 10130
136+
my_summary_sum 10130.0
137137
my_summary{quantile="0.01", myValue="labels"} 123.0
138138
my_summary{quantile="0.05", myValue="labels"} 123.0
139139
my_summary{quantile="0.5", myValue="labels"} 123.0
@@ -142,7 +142,32 @@ final class PrometheusMetricsTests: XCTestCase {
142142
my_summary{quantile="0.99", myValue="labels"} 123.0
143143
my_summary{quantile="0.999", myValue="labels"} 123.0
144144
my_summary_count{myValue="labels"} 1
145-
my_summary_sum{myValue="labels"} 123
145+
my_summary_sum{myValue="labels"} 123.0
146+
""")
147+
}
148+
149+
func testSummaryWithPreferredDisplayUnit() {
150+
let summary = Timer(label: "my_summary", preferredDisplayUnit: .seconds)
151+
152+
summary.recordSeconds(1)
153+
summary.recordMilliseconds(2 * 1_000)
154+
summary.recordNanoseconds(4 * 1_000_000_000)
155+
summary.recordSeconds(10000)
156+
157+
let promise = self.eventLoop.makePromise(of: String.self)
158+
prom.collect(promise.succeed)
159+
160+
XCTAssertEqual(try! promise.futureResult.wait(), """
161+
# TYPE my_summary summary
162+
my_summary{quantile="0.01"} 1.0
163+
my_summary{quantile="0.05"} 1.0
164+
my_summary{quantile="0.5"} 3.0
165+
my_summary{quantile="0.9"} 10000.0
166+
my_summary{quantile="0.95"} 10000.0
167+
my_summary{quantile="0.99"} 10000.0
168+
my_summary{quantile="0.999"} 10000.0
169+
my_summary_count 4
170+
my_summary_sum 10007.0
146171
""")
147172
}
148173

Tests/SwiftPrometheusTests/XCTestManifests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extension PrometheusMetricsTests {
1414
("testHistogram", testHistogram),
1515
("testMetricDestroying", testMetricDestroying),
1616
("testSummary", testSummary),
17+
("testSummaryWithPreferredDisplayUnit", testSummaryWithPreferredDisplayUnit),
1718
]
1819
}
1920

@@ -25,6 +26,7 @@ extension SwiftPrometheusTests {
2526
("testCounter", testCounter),
2627
("testGauge", testGauge),
2728
("testHistogram", testHistogram),
29+
("testMultipleCounter", testMultipleCounter),
2830
("testSummary", testSummary),
2931
]
3032
}

0 commit comments

Comments
 (0)