Skip to content

Commit 913a256

Browse files
committed
Add Prom instance to all metrics, internalise some things
1 parent 18d0d7a commit 913a256

File tree

6 files changed

+34
-18
lines changed

6 files changed

+34
-18
lines changed

Sources/Prometheus/MetricTypes/Counter.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric {
1+
public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHandled {
2+
internal let prometheus: Prometheus
3+
24
public let name: String
35
public let help: String?
46

57
internal var value: NumType
68

79
internal var metrics: [Labels: NumType] = [:]
810

9-
public init(_ name: String, _ help: String? = nil, _ initialValue: NumType = 0) {
11+
internal init(_ name: String, _ help: String? = nil, _ initialValue: NumType = 0, _ p: Prometheus) {
1012
self.name = name
1113
self.help = help
1214
self.value = initialValue
15+
self.prometheus = p
1316
}
1417

1518
public func getMetric() -> String {

Sources/Prometheus/MetricTypes/Gauge.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric {
1+
public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHandled {
2+
internal let prometheus: Prometheus
3+
24
public let name: String
35
public let help: String?
46

57
private var value: NumType
68

79
private var metrics: [Labels: NumType] = [:]
810

9-
public init(_ name: String, _ help: String? = nil, _ initialValue: NumType = 0) {
11+
internal init(_ name: String, _ help: String? = nil, _ initialValue: NumType = 0, _ p: Prometheus) {
1012
self.name = name
1113
self.help = help
1214
self.value = initialValue
15+
self.prometheus = p
1316
}
1417

1518
public func getMetric() -> String {

Sources/Prometheus/MetricTypes/Histogram.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ extension HistogramLabels {
1111
}
1212
}
1313

14-
public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: Metric {
15-
public let name: String
14+
public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: Metric, PrometheusHandled {
15+
internal let prometheus: Prometheus
1616

17+
public let name: String
1718
public let help: String?
1819

1920
private var buckets: [Counter<NumType, EmptyCodable>] = []
@@ -24,18 +25,20 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
2425

2526
private let total: Counter<NumType, EmptyCodable>
2627

27-
internal init(_ name: String, _ help: String? = nil, _ labels: Labels = Labels(), _ buckets: [Double] = defaultBuckets) {
28+
internal init(_ name: String, _ help: String? = nil, _ labels: Labels = Labels(), _ buckets: [Double] = defaultBuckets, _ p: Prometheus) {
2829
self.name = name
2930
self.help = help
3031

31-
self.total = .init("\(self.name)_sum")
32+
self.prometheus = p
33+
34+
self.total = .init("\(self.name)_sum", nil, 0, p)
3235

3336
self.labels = labels
3437

3538
self.upperBounds = buckets
3639

3740
buckets.forEach { _ in
38-
self.buckets.append(.init("\(name)_bucket", nil, 0))
41+
self.buckets.append(.init("\(name)_bucket", nil, 0, p))
3942
}
4043
}
4144

Sources/Prometheus/MetricTypes/Metric.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ public protocol Metric {
55
func getMetric() -> String
66
}
77

8+
internal protocol PrometheusHandled {
9+
var prometheus: Prometheus { get }
10+
}
11+
812
public protocol MetricLabels: Codable, Hashable {
913
init()
1014
}

Sources/Prometheus/MetricTypes/Summary.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ extension SummaryLabels {
1111
}
1212
}
1313

14-
public class Summary<NumType: DoubleRepresentable, Labels: SummaryLabels>: Metric {
15-
public let name: String
14+
public class Summary<NumType: DoubleRepresentable, Labels: SummaryLabels>: Metric, PrometheusHandled {
15+
internal let prometheus: Prometheus
1616

17+
public let name: String
1718
public let help: String?
1819

1920
private var labels: Labels
@@ -26,13 +27,15 @@ public class Summary<NumType: DoubleRepresentable, Labels: SummaryLabels>: Metri
2627

2728
private var quantiles: [Double]
2829

29-
internal init(_ name: String, _ help: String? = nil, _ quantiles: [Double] = defaultQuantiles, _ labels: Labels = Labels()) {
30+
internal init(_ name: String, _ help: String? = nil, _ quantiles: [Double] = defaultQuantiles, _ labels: Labels = Labels(), _ p: Prometheus) {
3031
self.name = name
3132
self.help = help
3233

33-
self.sum = .init("\(self.name)_sum")
34+
self.prometheus = p
35+
36+
self.sum = .init("\(self.name)_sum", nil, 0, p)
3437

35-
self.count = .init("\(self.name)_count")
38+
self.count = .init("\(self.name)_count", nil, 0, p)
3639

3740
self.quantiles = quantiles
3841

Sources/Prometheus/Prometheus.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Prometheus {
2727
initialValue: T = 0,
2828
withLabelType labelType: U.Type) -> Counter<T, U>
2929
{
30-
let counter = Counter<T, U>(name, helpText, initialValue)
30+
let counter = Counter<T, U>(name, helpText, initialValue, self)
3131
self.metrics.append(counter)
3232
return counter
3333
}
@@ -69,7 +69,7 @@ public class Prometheus {
6969
initialValue: T = 0,
7070
withLabelType labelType: U.Type) -> Gauge<T, U>
7171
{
72-
let gauge = Gauge<T, U>(name, helpText, initialValue)
72+
let gauge = Gauge<T, U>(name, helpText, initialValue, self)
7373
self.metrics.append(gauge)
7474
return gauge
7575
}
@@ -111,7 +111,7 @@ public class Prometheus {
111111
buckets: [Double] = defaultBuckets,
112112
labels: U) -> Histogram<T, U>
113113
{
114-
let histogram = Histogram<T, U>(name, helpText, labels, buckets)
114+
let histogram = Histogram<T, U>(name, helpText, labels, buckets, self)
115115
self.metrics.append(histogram)
116116
return histogram
117117
}
@@ -143,7 +143,7 @@ public class Prometheus {
143143
quantiles: [Double] = defaultQuantiles,
144144
labels: U) -> Summary<T, U>
145145
{
146-
let summary = Summary<T, U>(name, helpText, quantiles, labels)
146+
let summary = Summary<T, U>(name, helpText, quantiles, labels, self)
147147
metrics.append(summary)
148148
return summary
149149
}

0 commit comments

Comments
 (0)