Skip to content

Commit d7e25d9

Browse files
authored
Update SwiftPrometheus to use SwiftMetric 2.* (TimeUnit change to struct) (#31)
* Update SwiftPrometheus to use SwiftMetric 2.* SemVer (TimeUnit def change) * Update Package.swift * Update SummaryTests.swift * Update Summary.swift
1 parent 1d685b4 commit d7e25d9

File tree

4 files changed

+10
-15
lines changed

4 files changed

+10
-15
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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.2.0"),
16+
.package(url: "https://github.com/apple/swift-metrics.git", from: "2.0.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: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import NIOConcurrencyHelpers
2-
import enum CoreMetrics.TimeUnit
2+
import struct CoreMetrics.TimeUnit
33
import Dispatch
44

55
/// Label type Summaries can use
@@ -125,16 +125,10 @@ public class PromSummary<NumType: DoubleRepresentable, Labels: SummaryLabels>: P
125125
}
126126
}
127127

128+
// Updated for SwiftMetrics 2.0 to be unit agnostic if displayUnit is set or default to nanoseconds.
128129
private func format(_ v: Double) -> Double {
129-
guard let displayUnit = self.displayUnit else { return v }
130-
switch displayUnit {
131-
case .days: return (v / 1_000_000_000) * 60 * 60 * 24
132-
case .hours: return (v / 1_000_000_000) * 60 * 60
133-
case .minutes: return (v / 1_000_000_000) * 60
134-
case .seconds: return v / 1_000_000_000
135-
case .milliseconds: return v / 1_000_000
136-
case .nanoseconds: return v
137-
}
130+
let displayUnitScale = self.displayUnit?.scaleFromNanoseconds ?? 1
131+
return v / Double(displayUnitScale)
138132
}
139133

140134
internal func preferDisplayUnit(_ unit: TimeUnit) {

Tests/SwiftPrometheusTests/SummaryTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ final class SummaryTests: XCTestCase {
7676

7777
summary.recordSeconds(1)
7878
summary.recordMilliseconds(2 * 1_000)
79+
summary.recordMicroseconds(3 * 1_000_000)
7980
summary.recordNanoseconds(4 * 1_000_000_000)
8081
summary.recordSeconds(10000)
8182

@@ -91,8 +92,8 @@ final class SummaryTests: XCTestCase {
9192
my_summary{quantile="0.95"} 10000.0
9293
my_summary{quantile="0.99"} 10000.0
9394
my_summary{quantile="0.999"} 10000.0
94-
my_summary_count 4
95-
my_summary_sum 10007.0
95+
my_summary_count 5
96+
my_summary_sum 10010.0
9697
""")
9798
}
9899

0 commit comments

Comments
 (0)