Skip to content

Commit 26e2b29

Browse files
authored
Don't require to pass a registry on MetricsFactory initialization (#98)
1 parent 77465d7 commit 26e2b29

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

Sources/Prometheus/PrometheusMetricsFactory.swift

+24-16
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ import CoreMetrics
1616

1717
/// A wrapper around ``PrometheusCollectorRegistry`` to implement the `swift-metrics` `MetricsFactory` protocol
1818
public struct PrometheusMetricsFactory: Sendable {
19-
/// The underlying ``PrometheusCollectorRegistry`` that is used to generate
20-
public var client: PrometheusCollectorRegistry
19+
private static let _defaultRegistry = PrometheusCollectorRegistry()
20+
21+
/// The default ``PrometheusCollectorRegistry``, which is used inside the ``PrometheusMetricsFactory``
22+
/// if no other is provided in ``init(client:)`` or set via ``PrometheusMetricsFactory/client``
23+
public static var defaultRegistry: PrometheusCollectorRegistry {
24+
self._defaultRegistry
25+
}
26+
27+
/// The underlying ``PrometheusCollectorRegistry`` that is used to generate the swift-metrics handlers
28+
public var registry: PrometheusCollectorRegistry
2129

2230
/// The default histogram buckets for a ``TimeHistogram``. If there is no explicit overwrite
2331
/// via ``timeHistogramBuckets``, the buckets provided here will be used for any new
@@ -39,8 +47,8 @@ public struct PrometheusMetricsFactory: Sendable {
3947
/// to overwrite the Metric names in third party packages.
4048
public var labelAndDimensionSanitizer: @Sendable (_ label: String, _ dimensions: [(String, String)]) -> (String, [(String, String)])
4149

42-
public init(client: PrometheusCollectorRegistry) {
43-
self.client = client
50+
public init(client: PrometheusCollectorRegistry = Self.defaultRegistry) {
51+
self.registry = client
4452

4553
self.timeHistogramBuckets = [:]
4654
self.defaultTimeHistogramBuckets = [
@@ -79,54 +87,54 @@ public struct PrometheusMetricsFactory: Sendable {
7987
extension PrometheusMetricsFactory: CoreMetrics.MetricsFactory {
8088
public func makeCounter(label: String, dimensions: [(String, String)]) -> CoreMetrics.CounterHandler {
8189
let (label, dimensions) = self.labelAndDimensionSanitizer(label, dimensions)
82-
return self.client.makeCounter(name: label, labels: dimensions)
90+
return self.registry.makeCounter(name: label, labels: dimensions)
8391
}
8492

8593
public func makeFloatingPointCounter(label: String, dimensions: [(String, String)]) -> FloatingPointCounterHandler {
8694
let (label, dimensions) = self.labelAndDimensionSanitizer(label, dimensions)
87-
return self.client.makeCounter(name: label, labels: dimensions)
95+
return self.registry.makeCounter(name: label, labels: dimensions)
8896
}
8997

9098
public func makeRecorder(label: String, dimensions: [(String, String)], aggregate: Bool) -> CoreMetrics.RecorderHandler {
9199
let (label, dimensions) = self.labelAndDimensionSanitizer(label, dimensions)
92100
if aggregate {
93101
let buckets = self.valueHistogramBuckets[label] ?? self.defaultValueHistogramBuckets
94-
return self.client.makeValueHistogram(name: label, labels: dimensions, buckets: buckets)
102+
return self.registry.makeValueHistogram(name: label, labels: dimensions, buckets: buckets)
95103
} else {
96-
return self.client.makeGauge(name: label, labels: dimensions)
104+
return self.registry.makeGauge(name: label, labels: dimensions)
97105
}
98106
}
99107

100108
public func makeMeter(label: String, dimensions: [(String, String)]) -> CoreMetrics.MeterHandler {
101-
return self.client.makeGauge(name: label, labels: dimensions)
109+
return self.registry.makeGauge(name: label, labels: dimensions)
102110
}
103111

104112
public func makeTimer(label: String, dimensions: [(String, String)]) -> CoreMetrics.TimerHandler {
105113
let (label, dimensions) = self.labelAndDimensionSanitizer(label, dimensions)
106114
let buckets = self.timeHistogramBuckets[label] ?? self.defaultTimeHistogramBuckets
107-
return self.client.makeDurationHistogram(name: label, labels: dimensions, buckets: buckets)
115+
return self.registry.makeDurationHistogram(name: label, labels: dimensions, buckets: buckets)
108116
}
109117

110118
public func destroyCounter(_ handler: CoreMetrics.CounterHandler) {
111119
guard let counter = handler as? Counter else {
112120
return
113121
}
114-
self.client.destroyCounter(counter)
122+
self.registry.destroyCounter(counter)
115123
}
116124

117125
public func destroyFloatingPointCounter(_ handler: FloatingPointCounterHandler) {
118126
guard let counter = handler as? Counter else {
119127
return
120128
}
121-
self.client.destroyCounter(counter)
129+
self.registry.destroyCounter(counter)
122130
}
123131

124132
public func destroyRecorder(_ handler: CoreMetrics.RecorderHandler) {
125133
switch handler {
126134
case let gauge as Gauge:
127-
self.client.destroyGauge(gauge)
135+
self.registry.destroyGauge(gauge)
128136
case let histogram as Histogram<Double>:
129-
self.client.destroyValueHistogram(histogram)
137+
self.registry.destroyValueHistogram(histogram)
130138
default:
131139
break
132140
}
@@ -136,13 +144,13 @@ extension PrometheusMetricsFactory: CoreMetrics.MetricsFactory {
136144
guard let gauge = handler as? Gauge else {
137145
return
138146
}
139-
self.client.destroyGauge(gauge)
147+
self.registry.destroyGauge(gauge)
140148
}
141149

142150
public func destroyTimer(_ handler: CoreMetrics.TimerHandler) {
143151
guard let histogram = handler as? Histogram<Duration> else {
144152
return
145153
}
146-
self.client.destroyTimeHistogram(histogram)
154+
self.registry.destroyTimeHistogram(histogram)
147155
}
148156
}

Tests/PrometheusTests/PrometheusMetricsFactoryTests.swift

+6
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,10 @@ final class PrometheusMetricsFactoryTests: XCTestCase {
120120
)
121121
}
122122

123+
func testTwoMetricFactoriesUseTheSameUnderlyingCollectorRegsitry() {
124+
let first = PrometheusMetricsFactory()
125+
let second = PrometheusMetricsFactory()
126+
127+
XCTAssert(first.registry === second.registry)
128+
}
123129
}

0 commit comments

Comments
 (0)