@@ -16,8 +16,16 @@ import CoreMetrics
16
16
17
17
/// A wrapper around ``PrometheusCollectorRegistry`` to implement the `swift-metrics` `MetricsFactory` protocol
18
18
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
21
29
22
30
/// The default histogram buckets for a ``TimeHistogram``. If there is no explicit overwrite
23
31
/// via ``timeHistogramBuckets``, the buckets provided here will be used for any new
@@ -39,8 +47,8 @@ public struct PrometheusMetricsFactory: Sendable {
39
47
/// to overwrite the Metric names in third party packages.
40
48
public var labelAndDimensionSanitizer : @Sendable ( _ label: String , _ dimensions: [ ( String , String ) ] ) -> ( String , [ ( String , String ) ] )
41
49
42
- public init ( client: PrometheusCollectorRegistry ) {
43
- self . client = client
50
+ public init ( client: PrometheusCollectorRegistry = Self . defaultRegistry ) {
51
+ self . registry = client
44
52
45
53
self . timeHistogramBuckets = [ : ]
46
54
self . defaultTimeHistogramBuckets = [
@@ -79,54 +87,54 @@ public struct PrometheusMetricsFactory: Sendable {
79
87
extension PrometheusMetricsFactory : CoreMetrics . MetricsFactory {
80
88
public func makeCounter( label: String , dimensions: [ ( String , String ) ] ) -> CoreMetrics . CounterHandler {
81
89
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)
83
91
}
84
92
85
93
public func makeFloatingPointCounter( label: String , dimensions: [ ( String , String ) ] ) -> FloatingPointCounterHandler {
86
94
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)
88
96
}
89
97
90
98
public func makeRecorder( label: String , dimensions: [ ( String , String ) ] , aggregate: Bool ) -> CoreMetrics . RecorderHandler {
91
99
let ( label, dimensions) = self . labelAndDimensionSanitizer ( label, dimensions)
92
100
if aggregate {
93
101
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)
95
103
} else {
96
- return self . client . makeGauge ( name: label, labels: dimensions)
104
+ return self . registry . makeGauge ( name: label, labels: dimensions)
97
105
}
98
106
}
99
107
100
108
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)
102
110
}
103
111
104
112
public func makeTimer( label: String , dimensions: [ ( String , String ) ] ) -> CoreMetrics . TimerHandler {
105
113
let ( label, dimensions) = self . labelAndDimensionSanitizer ( label, dimensions)
106
114
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)
108
116
}
109
117
110
118
public func destroyCounter( _ handler: CoreMetrics . CounterHandler ) {
111
119
guard let counter = handler as? Counter else {
112
120
return
113
121
}
114
- self . client . destroyCounter ( counter)
122
+ self . registry . destroyCounter ( counter)
115
123
}
116
124
117
125
public func destroyFloatingPointCounter( _ handler: FloatingPointCounterHandler ) {
118
126
guard let counter = handler as? Counter else {
119
127
return
120
128
}
121
- self . client . destroyCounter ( counter)
129
+ self . registry . destroyCounter ( counter)
122
130
}
123
131
124
132
public func destroyRecorder( _ handler: CoreMetrics . RecorderHandler ) {
125
133
switch handler {
126
134
case let gauge as Gauge :
127
- self . client . destroyGauge ( gauge)
135
+ self . registry . destroyGauge ( gauge)
128
136
case let histogram as Histogram < Double > :
129
- self . client . destroyValueHistogram ( histogram)
137
+ self . registry . destroyValueHistogram ( histogram)
130
138
default :
131
139
break
132
140
}
@@ -136,13 +144,13 @@ extension PrometheusMetricsFactory: CoreMetrics.MetricsFactory {
136
144
guard let gauge = handler as? Gauge else {
137
145
return
138
146
}
139
- self . client . destroyGauge ( gauge)
147
+ self . registry . destroyGauge ( gauge)
140
148
}
141
149
142
150
public func destroyTimer( _ handler: CoreMetrics . TimerHandler ) {
143
151
guard let histogram = handler as? Histogram < Duration > else {
144
152
return
145
153
}
146
- self . client . destroyTimeHistogram ( histogram)
154
+ self . registry . destroyTimeHistogram ( histogram)
147
155
}
148
156
}
0 commit comments