@@ -47,33 +47,23 @@ public final class PrometheusCollectorRegistry: Sendable {
47
47
}
48
48
}
49
49
50
- // Note: In order to support Sendable, need to explicitely define the types.
51
- private struct CounterWithHelp {
52
- var counter : Counter
50
+ private struct MetricWithHelp < Metric: AnyObject & Sendable > : Sendable {
51
+ var metric : Metric
53
52
let help : String
54
53
}
55
54
56
- private struct GaugeWithHelp {
57
- var gauge : Gauge
58
- let help : String
59
- }
60
-
61
- private struct CounterGroup {
62
- // A collection of Counter metrics, each with a unique label set, that share the same metric name.
63
- // Distinct help strings for the same metric name are permitted, but Prometheus retains only the
64
- // most recent one. For an unlabelled Counter, the empty label set is used as the key, and the
65
- // collection contains only one entry. Finally, for clarification, the same Counter metric name can
66
- // simultaneously be labeled and unlabeled.
67
- var countersByLabelSets : [ LabelsKey : CounterWithHelp ]
68
- }
69
-
70
- private struct GaugeGroup {
71
- var gaugesByLabelSets : [ LabelsKey : GaugeWithHelp ]
55
+ private struct MetricGroup < Metric: Sendable & AnyObject > : Sendable {
56
+ /// A collection of metrics, each with a unique label set, that share the same metric name.
57
+ /// Distinct help strings for the same metric name are permitted, but Prometheus retains only the
58
+ /// most recent one. For an unlabelled metric, the empty label set is used as the key, and the
59
+ /// collection contains only one entry. Finally, for clarification, the same metric name can
60
+ /// simultaneously be labeled and unlabeled.
61
+ var metricsByLabelSets : [ LabelsKey : MetricWithHelp < Metric > ]
72
62
}
73
63
74
64
private enum Metric {
75
- case counter( CounterGroup )
76
- case gauge( GaugeGroup )
65
+ case counter( MetricGroup < Counter > )
66
+ case gauge( MetricGroup < Gauge > )
77
67
case durationHistogram( DurationHistogram , help: String )
78
68
case durationHistogramWithLabels( [ String ] , [ LabelsKey : DurationHistogram ] , [ Duration ] , help: String )
79
69
case valueHistogram( ValueHistogram , help: String )
@@ -144,23 +134,23 @@ public final class PrometheusCollectorRegistry: Sendable {
144
134
guard let entry = store [ name] else {
145
135
// First time a Counter is registered with this name.
146
136
let counter = Counter ( name: name, labels: labels)
147
- let counterWithHelp = CounterWithHelp ( counter : counter, help: help)
148
- let counterGroup = CounterGroup (
149
- countersByLabelSets : [ key: counterWithHelp]
137
+ let counterWithHelp = MetricWithHelp ( metric : counter, help: help)
138
+ let counterGroup = MetricGroup (
139
+ metricsByLabelSets : [ key: counterWithHelp]
150
140
)
151
141
store [ name] = . counter( counterGroup)
152
142
return counter
153
143
}
154
144
switch entry {
155
145
case . counter( var existingCounterGroup) :
156
- if let existingCounterWithHelp = existingCounterGroup. countersByLabelSets [ key] {
157
- return existingCounterWithHelp. counter
146
+ if let existingCounterWithHelp = existingCounterGroup. metricsByLabelSets [ key] {
147
+ return existingCounterWithHelp. metric
158
148
}
159
149
160
150
// Even if the metric name is identical, each label set defines a unique time series.
161
151
let counter = Counter ( name: name, labels: labels)
162
- let counterWithHelp = CounterWithHelp ( counter : counter, help: help)
163
- existingCounterGroup. countersByLabelSets [ key] = counterWithHelp
152
+ let counterWithHelp = MetricWithHelp ( metric : counter, help: help)
153
+ existingCounterGroup. metricsByLabelSets [ key] = counterWithHelp
164
154
165
155
// Write the modified entry back to the store.
166
156
store [ name] = . counter( existingCounterGroup)
@@ -265,23 +255,23 @@ public final class PrometheusCollectorRegistry: Sendable {
265
255
guard let entry = store [ name] else {
266
256
// First time a Gauge is registered with this name.
267
257
let gauge = Gauge ( name: name, labels: labels)
268
- let gaugeWithHelp = GaugeWithHelp ( gauge : gauge, help: help)
269
- let gaugeGroup = GaugeGroup (
270
- gaugesByLabelSets : [ key: gaugeWithHelp]
258
+ let gaugeWithHelp = MetricWithHelp ( metric : gauge, help: help)
259
+ let gaugeGroup = MetricGroup (
260
+ metricsByLabelSets : [ key: gaugeWithHelp]
271
261
)
272
262
store [ name] = . gauge( gaugeGroup)
273
263
return gauge
274
264
}
275
265
switch entry {
276
266
case . gauge( var existingGaugeGroup) :
277
- if let existingGaugeWithHelp = existingGaugeGroup. gaugesByLabelSets [ key] {
278
- return existingGaugeWithHelp. gauge
267
+ if let existingGaugeWithHelp = existingGaugeGroup. metricsByLabelSets [ key] {
268
+ return existingGaugeWithHelp. metric
279
269
}
280
270
281
271
// Even if the metric name is identical, each label set defines a unique time series.
282
272
let gauge = Gauge ( name: name, labels: labels)
283
- let gaugeWithHelp = GaugeWithHelp ( gauge : gauge, help: help)
284
- existingGaugeGroup. gaugesByLabelSets [ key] = gaugeWithHelp
273
+ let gaugeWithHelp = MetricWithHelp ( metric : gauge, help: help)
274
+ existingGaugeGroup. metricsByLabelSets [ key] = gaugeWithHelp
285
275
286
276
// Write the modified entry back to the store.
287
277
store [ name] = . gauge( existingGaugeGroup)
@@ -687,14 +677,14 @@ public final class PrometheusCollectorRegistry: Sendable {
687
677
switch store [ counter. name] {
688
678
case . counter( var counterGroup) :
689
679
let key = LabelsKey ( counter. labels)
690
- guard let existingCounterGroup = counterGroup. countersByLabelSets [ key] ,
691
- existingCounterGroup. counter === counter
680
+ guard let existingCounterGroup = counterGroup. metricsByLabelSets [ key] ,
681
+ existingCounterGroup. metric === counter
692
682
else {
693
683
return
694
684
}
695
- counterGroup. countersByLabelSets . removeValue ( forKey: key)
685
+ counterGroup. metricsByLabelSets . removeValue ( forKey: key)
696
686
697
- if counterGroup. countersByLabelSets . isEmpty {
687
+ if counterGroup. metricsByLabelSets . isEmpty {
698
688
store. removeValue ( forKey: counter. name)
699
689
} else {
700
690
store [ counter. name] = . counter( counterGroup)
@@ -715,14 +705,14 @@ public final class PrometheusCollectorRegistry: Sendable {
715
705
switch store [ gauge. name] {
716
706
case . gauge( var gaugeGroup) :
717
707
let key = LabelsKey ( gauge. labels)
718
- guard let existingGaugeGroup = gaugeGroup. gaugesByLabelSets [ key] ,
719
- existingGaugeGroup. gauge === gauge
708
+ guard let existingGaugeGroup = gaugeGroup. metricsByLabelSets [ key] ,
709
+ existingGaugeGroup. metric === gauge
720
710
else {
721
711
return
722
712
}
723
- gaugeGroup. gaugesByLabelSets . removeValue ( forKey: key)
713
+ gaugeGroup. metricsByLabelSets . removeValue ( forKey: key)
724
714
725
- if gaugeGroup. gaugesByLabelSets . isEmpty {
715
+ if gaugeGroup. metricsByLabelSets . isEmpty {
726
716
store. removeValue ( forKey: gauge. name)
727
717
} else {
728
718
store [ gauge. name] = . gauge( gaugeGroup)
@@ -796,26 +786,26 @@ public final class PrometheusCollectorRegistry: Sendable {
796
786
switch metric {
797
787
case . counter( let counterGroup) :
798
788
// Should not be empty, as a safeguard skip if it is.
799
- guard let _ = counterGroup. countersByLabelSets . first? . value else {
789
+ guard let _ = counterGroup. metricsByLabelSets . first? . value else {
800
790
continue
801
791
}
802
- for counterWithHelp in counterGroup. countersByLabelSets . values {
792
+ for counterWithHelp in counterGroup. metricsByLabelSets . values {
803
793
let help = counterWithHelp. help
804
794
help. isEmpty ? ( ) : buffer. addLine ( prefix: prefixHelp, name: name, value: help)
805
795
buffer. addLine ( prefix: prefixType, name: name, value: " counter " )
806
- counterWithHelp. counter . emit ( into: & buffer)
796
+ counterWithHelp. metric . emit ( into: & buffer)
807
797
}
808
798
809
799
case . gauge( let gaugeGroup) :
810
800
// Should not be empty, as a safeguard skip if it is.
811
- guard let _ = gaugeGroup. gaugesByLabelSets . first? . value else {
801
+ guard let _ = gaugeGroup. metricsByLabelSets . first? . value else {
812
802
continue
813
803
}
814
- for gaugeWithHelp in gaugeGroup. gaugesByLabelSets . values {
804
+ for gaugeWithHelp in gaugeGroup. metricsByLabelSets . values {
815
805
let help = gaugeWithHelp. help
816
806
help. isEmpty ? ( ) : buffer. addLine ( prefix: prefixHelp, name: name, value: help)
817
807
buffer. addLine ( prefix: prefixType, name: name, value: " gauge " )
818
- gaugeWithHelp. gauge . emit ( into: & buffer)
808
+ gaugeWithHelp. metric . emit ( into: & buffer)
819
809
}
820
810
821
811
case . durationHistogram( let histogram, let help) :
0 commit comments