Skip to content

Commit 488b935

Browse files
authored
Merge pull request #76 from fabianfett/ff-remove-weak-references
2 parents 290b9e8 + 34b9687 commit 488b935

File tree

6 files changed

+18
-46
lines changed

6 files changed

+18
-46
lines changed

Sources/Prometheus/MetricTypes/Counter.swift

+2-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import NIOConcurrencyHelpers
33
/// Prometheus Counter metric
44
///
55
/// See: https://prometheus.io/docs/concepts/metric_types/#counter
6-
public class PromCounter<NumType: Numeric>: PromMetric, PrometheusHandled {
7-
/// Prometheus instance that created this Counter
8-
internal weak var prometheus: PrometheusClient?
9-
6+
public class PromCounter<NumType: Numeric>: PromMetric {
107
/// Name of the Counter, required
118
public let name: String
129
/// Help text of the Counter, optional
@@ -34,12 +31,11 @@ public class PromCounter<NumType: Numeric>: PromMetric, PrometheusHandled {
3431
/// - help: Help text of the Counter
3532
/// - initialValue: Initial value to set the counter to
3633
/// - p: Prometheus instance that created this counter
37-
internal init(_ name: String, _ help: String? = nil, _ initialValue: NumType = 0, _ p: PrometheusClient) {
34+
internal init(_ name: String, _ help: String? = nil, _ initialValue: NumType = 0) {
3835
self.name = name
3936
self.help = help
4037
self.initialValue = initialValue
4138
self.value = initialValue
42-
self.prometheus = p
4339
self.lock = Lock()
4440
}
4541

Sources/Prometheus/MetricTypes/Gauge.swift

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import NIOConcurrencyHelpers
55
/// Prometheus Gauge metric
66
///
77
/// See https://prometheus.io/docs/concepts/metric_types/#gauge
8-
public class PromGauge<NumType: DoubleRepresentable>: PromMetric, PrometheusHandled {
9-
/// Prometheus instance that created this Gauge
10-
internal weak var prometheus: PrometheusClient?
11-
8+
public class PromGauge<NumType: DoubleRepresentable>: PromMetric {
129
/// Name of the Gauge, required
1310
public let name: String
1411
/// Help text of the Gauge, optional
@@ -37,12 +34,11 @@ public class PromGauge<NumType: DoubleRepresentable>: PromMetric, PrometheusHand
3734
/// - initialValue: Initial value to set the Gauge to
3835
/// - p: Prometheus instance that created this Gauge
3936
///
40-
internal init(_ name: String, _ help: String? = nil, _ initialValue: NumType = 0, _ p: PrometheusClient) {
37+
internal init(_ name: String, _ help: String? = nil, _ initialValue: NumType = 0) {
4138
self.name = name
4239
self.help = help
4340
self.initialValue = initialValue
4441
self.value = initialValue
45-
self.prometheus = p
4642
self.lock = Lock()
4743
}
4844

Sources/Prometheus/MetricTypes/Histogram.swift

+5-11
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ public struct Buckets: ExpressibleByArrayLiteral {
6666
/// Prometheus Histogram metric
6767
///
6868
/// See https://prometheus.io/docs/concepts/metric_types/#Histogram
69-
public class PromHistogram<NumType: DoubleRepresentable>: PromMetric, PrometheusHandled {
70-
/// Prometheus instance that created this Histogram
71-
internal weak var prometheus: PrometheusClient?
72-
69+
public class PromHistogram<NumType: DoubleRepresentable>: PromMetric {
7370
/// Name of this Histogram, required
7471
public let name: String
7572
/// Help text of this Histogram, optional
@@ -100,20 +97,18 @@ public class PromHistogram<NumType: DoubleRepresentable>: PromMetric, Prometheus
10097
/// - help: Help text of the Histogram
10198
/// - buckets: Buckets to use for the Histogram
10299
/// - p: Prometheus instance creating this Histogram
103-
internal init(_ name: String, _ help: String? = nil, _ buckets: Buckets = .defaultBuckets, _ p: PrometheusClient) {
100+
internal init(_ name: String, _ help: String? = nil, _ buckets: Buckets = .defaultBuckets) {
104101
self.name = name
105102
self.help = help
106103

107-
self.prometheus = p
108-
109-
self.sum = .init("\(self.name)_sum", nil, 0, p)
104+
self.sum = .init("\(self.name)_sum", nil, 0)
110105

111106
self.upperBounds = buckets.buckets
112107

113108
self.lock = Lock()
114109

115110
buckets.buckets.forEach { _ in
116-
self.buckets.append(.init("\(name)_bucket", nil, 0, p))
111+
self.buckets.append(.init("\(name)_bucket", nil, 0))
117112
}
118113
}
119114

@@ -239,8 +234,7 @@ public class PromHistogram<NumType: DoubleRepresentable>: PromMetric, Prometheus
239234
""")
240235
return histogram
241236
}
242-
guard let prometheus = prometheus else { fatalError("Lingering Histogram") }
243-
let newHistogram = PromHistogram(self.name, self.help, Buckets(self.upperBounds), prometheus)
237+
let newHistogram = PromHistogram(self.name, self.help, Buckets(self.upperBounds))
244238
self.subHistograms[labels] = newHistogram
245239
return newHistogram
246240
}

Sources/Prometheus/MetricTypes/PromMetric.swift

-6
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,3 @@ extension PromMetric {
4444
buffer.writeString(collect())
4545
}
4646
}
47-
48-
/// Adding a prometheus instance to all metrics
49-
internal protocol PrometheusHandled {
50-
/// Prometheus client handling this metric
51-
var prometheus: PrometheusClient? { get }
52-
}

Sources/Prometheus/MetricTypes/Summary.swift

+5-13
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import Dispatch
66
/// Prometheus Summary metric
77
///
88
/// See https://prometheus.io/docs/concepts/metric_types/#summary
9-
public class PromSummary<NumType: DoubleRepresentable>: PromMetric, PrometheusHandled {
10-
/// Prometheus instance that created this Summary
11-
internal weak var prometheus: PrometheusClient?
12-
9+
public class PromSummary<NumType: DoubleRepresentable>: PromMetric {
1310
/// Name of this Summary, required
1411
public let name: String
1512
/// Help text of this Summary, optional
@@ -49,17 +46,15 @@ public class PromSummary<NumType: DoubleRepresentable>: PromMetric, PrometheusHa
4946
/// - capacity: Number of values to keep for calculating quantiles
5047
/// - quantiles: Quantiles to use for the Summary
5148
/// - p: Prometheus instance creating this Summary
52-
internal init(_ name: String, _ help: String? = nil, _ capacity: Int = Prometheus.defaultSummaryCapacity, _ quantiles: [Double] = Prometheus.defaultQuantiles, _ p: PrometheusClient) {
49+
internal init(_ name: String, _ help: String? = nil, _ capacity: Int = Prometheus.defaultSummaryCapacity, _ quantiles: [Double] = Prometheus.defaultQuantiles) {
5350
self.name = name
5451
self.help = help
5552

56-
self.prometheus = p
57-
5853
self.displayUnit = nil
5954

60-
self.sum = .init("\(self.name)_sum", nil, 0, p)
55+
self.sum = .init("\(self.name)_sum", nil, 0)
6156

62-
self.count = .init("\(self.name)_count", nil, 0, p)
57+
self.count = .init("\(self.name)_count", nil, 0)
6358

6459
self.values = CircularBuffer(initialCapacity: capacity)
6560

@@ -197,10 +192,7 @@ public class PromSummary<NumType: DoubleRepresentable>: PromMetric, PrometheusHa
197192
""")
198193
return summary
199194
}
200-
guard let prometheus = prometheus else {
201-
fatalError("Lingering Summary")
202-
}
203-
let newSummary = PromSummary(self.name, self.help, self.capacity, self.quantiles, prometheus)
195+
let newSummary = PromSummary(self.name, self.help, self.capacity, self.quantiles)
204196
self.subSummaries[labels] = newSummary
205197
return newSummary
206198
}

Sources/Prometheus/Prometheus.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public class PrometheusClient {
138138
return cachedCounter
139139
}
140140

141-
let counter = PromCounter<T>(name, helpText, initialValue, self)
141+
let counter = PromCounter<T>(name, helpText, initialValue)
142142
let oldInstrument = self.metrics.updateValue(counter, forKey: name)
143143
precondition(oldInstrument == nil, "Label \(oldInstrument!.name) is already associated with a \(oldInstrument!._type).")
144144
return counter
@@ -167,7 +167,7 @@ public class PrometheusClient {
167167
return cachedGauge
168168
}
169169

170-
let gauge = PromGauge<T>(name, helpText, initialValue, self)
170+
let gauge = PromGauge<T>(name, helpText, initialValue)
171171
let oldInstrument = self.metrics.updateValue(gauge, forKey: name)
172172
precondition(oldInstrument == nil, "Label \(oldInstrument!.name) is already associated with a \(oldInstrument!._type).")
173173
return gauge
@@ -196,7 +196,7 @@ public class PrometheusClient {
196196
return cachedHistogram
197197
}
198198

199-
let histogram = PromHistogram<T>(name, helpText, buckets, self)
199+
let histogram = PromHistogram<T>(name, helpText, buckets)
200200
let oldInstrument = self.metrics.updateValue(histogram, forKey: name)
201201
precondition(oldInstrument == nil, "Label \(oldInstrument!.name) is already associated with a \(oldInstrument!._type).")
202202
return histogram
@@ -226,7 +226,7 @@ public class PrometheusClient {
226226
if let cachedSummary: PromSummary<T> = self._getMetricInstance(with: name, andType: .summary) {
227227
return cachedSummary
228228
}
229-
let summary = PromSummary<T>(name, helpText, capacity, quantiles, self)
229+
let summary = PromSummary<T>(name, helpText, capacity, quantiles)
230230
let oldInstrument = self.metrics.updateValue(summary, forKey: name)
231231
precondition(oldInstrument == nil, "Label \(oldInstrument!.name) is already associated with a \(oldInstrument!._type).")
232232
return summary

0 commit comments

Comments
 (0)