Skip to content

Commit 344293d

Browse files
incertumktoso
andcommitted
update: apply reviewers suggestions
Co-authored-by: Konrad `ktoso` Malawski <[email protected]> Signed-off-by: Melissa Kilby <[email protected]>
1 parent 3195d72 commit 344293d

File tree

1 file changed

+39
-49
lines changed

1 file changed

+39
-49
lines changed

Sources/Prometheus/PrometheusCollectorRegistry.swift

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,23 @@ public final class PrometheusCollectorRegistry: Sendable {
4747
}
4848
}
4949

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
5352
let help: String
5453
}
5554

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>]
7262
}
7363

7464
private enum Metric {
75-
case counter(CounterGroup)
76-
case gauge(GaugeGroup)
65+
case counter(MetricGroup<Counter>)
66+
case gauge(MetricGroup<Gauge>)
7767
case durationHistogram(DurationHistogram, help: String)
7868
case durationHistogramWithLabels([String], [LabelsKey: DurationHistogram], [Duration], help: String)
7969
case valueHistogram(ValueHistogram, help: String)
@@ -144,23 +134,23 @@ public final class PrometheusCollectorRegistry: Sendable {
144134
guard let entry = store[name] else {
145135
// First time a Counter is registered with this name.
146136
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]
150140
)
151141
store[name] = .counter(counterGroup)
152142
return counter
153143
}
154144
switch entry {
155145
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
158148
}
159149

160150
// Even if the metric name is identical, each label set defines a unique time series.
161151
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
164154

165155
// Write the modified entry back to the store.
166156
store[name] = .counter(existingCounterGroup)
@@ -265,23 +255,23 @@ public final class PrometheusCollectorRegistry: Sendable {
265255
guard let entry = store[name] else {
266256
// First time a Gauge is registered with this name.
267257
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]
271261
)
272262
store[name] = .gauge(gaugeGroup)
273263
return gauge
274264
}
275265
switch entry {
276266
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
279269
}
280270

281271
// Even if the metric name is identical, each label set defines a unique time series.
282272
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
285275

286276
// Write the modified entry back to the store.
287277
store[name] = .gauge(existingGaugeGroup)
@@ -687,14 +677,14 @@ public final class PrometheusCollectorRegistry: Sendable {
687677
switch store[counter.name] {
688678
case .counter(var counterGroup):
689679
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
692682
else {
693683
return
694684
}
695-
counterGroup.countersByLabelSets.removeValue(forKey: key)
685+
counterGroup.metricsByLabelSets.removeValue(forKey: key)
696686

697-
if counterGroup.countersByLabelSets.isEmpty {
687+
if counterGroup.metricsByLabelSets.isEmpty {
698688
store.removeValue(forKey: counter.name)
699689
} else {
700690
store[counter.name] = .counter(counterGroup)
@@ -715,14 +705,14 @@ public final class PrometheusCollectorRegistry: Sendable {
715705
switch store[gauge.name] {
716706
case .gauge(var gaugeGroup):
717707
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
720710
else {
721711
return
722712
}
723-
gaugeGroup.gaugesByLabelSets.removeValue(forKey: key)
713+
gaugeGroup.metricsByLabelSets.removeValue(forKey: key)
724714

725-
if gaugeGroup.gaugesByLabelSets.isEmpty {
715+
if gaugeGroup.metricsByLabelSets.isEmpty {
726716
store.removeValue(forKey: gauge.name)
727717
} else {
728718
store[gauge.name] = .gauge(gaugeGroup)
@@ -796,26 +786,26 @@ public final class PrometheusCollectorRegistry: Sendable {
796786
switch metric {
797787
case .counter(let counterGroup):
798788
// 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 {
800790
continue
801791
}
802-
for counterWithHelp in counterGroup.countersByLabelSets.values {
792+
for counterWithHelp in counterGroup.metricsByLabelSets.values {
803793
let help = counterWithHelp.help
804794
help.isEmpty ? () : buffer.addLine(prefix: prefixHelp, name: name, value: help)
805795
buffer.addLine(prefix: prefixType, name: name, value: "counter")
806-
counterWithHelp.counter.emit(into: &buffer)
796+
counterWithHelp.metric.emit(into: &buffer)
807797
}
808798

809799
case .gauge(let gaugeGroup):
810800
// 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 {
812802
continue
813803
}
814-
for gaugeWithHelp in gaugeGroup.gaugesByLabelSets.values {
804+
for gaugeWithHelp in gaugeGroup.metricsByLabelSets.values {
815805
let help = gaugeWithHelp.help
816806
help.isEmpty ? () : buffer.addLine(prefix: prefixHelp, name: name, value: help)
817807
buffer.addLine(prefix: prefixType, name: name, value: "gauge")
818-
gaugeWithHelp.gauge.emit(into: &buffer)
808+
gaugeWithHelp.metric.emit(into: &buffer)
819809
}
820810

821811
case .durationHistogram(let histogram, let help):

0 commit comments

Comments
 (0)