Skip to content

Commit f1bc21d

Browse files
committed
Add a whole bunch of queue statements
1 parent de124db commit f1bc21d

File tree

9 files changed

+187
-145
lines changed

9 files changed

+187
-145
lines changed

Sources/Prometheus/MetricTypes/Counter.swift

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/// See https://prometheus.io/docs/concepts/metric_types/#counter
44
public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHandled {
55
/// Prometheus instance that created this Counter
6-
internal let prometheus: Prometheus
6+
internal let prometheus: PrometheusClient
77

88
/// Name of the Counter, required
99
public let name: String
@@ -15,10 +15,10 @@ public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, Prometheus
1515

1616
/// Current value of the counter
1717
internal var value: NumType
18-
18+
1919
/// Initial value of the counter
2020
private var initialValue: NumType
21-
21+
2222
/// Storage of values that have labels attached
2323
internal var metrics: [Labels: NumType] = [:]
2424

@@ -29,7 +29,7 @@ public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, Prometheus
2929
/// - help: Helpt text of the Counter
3030
/// - initialValue: Initial value to set the counter to
3131
/// - p: Prometheus instance that created this counter
32-
internal init(_ name: String, _ help: String? = nil, _ initialValue: NumType = 0, _ p: Prometheus) {
32+
internal init(_ name: String, _ help: String? = nil, _ initialValue: NumType = 0, _ p: PrometheusClient) {
3333
self.name = name
3434
self.help = help
3535
self.initialValue = initialValue
@@ -41,19 +41,21 @@ public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, Prometheus
4141
///
4242
/// - Returns:
4343
/// Newline seperated Prometheus formatted metric string
44-
public func getMetric() -> String {
45-
var output = [String]()
46-
47-
output.append(headers)
48-
49-
output.append("\(name) \(value)")
50-
51-
metrics.forEach { (labels, value) in
52-
let labelsString = encodeLabels(labels)
53-
output.append("\(name)\(labelsString) \(value)")
44+
public func getMetric(_ done: @escaping (String) -> Void) {
45+
prometheusQueue.async(flags: .barrier) {
46+
var output = [String]()
47+
48+
output.append(self.headers)
49+
50+
output.append("\(self.name) \(self.value)")
51+
52+
self.metrics.forEach { (labels, value) in
53+
let labelsString = encodeLabels(labels)
54+
output.append("\(self.name)\(labelsString) \(value)")
55+
}
56+
57+
done(output.joined(separator: "\n"))
5458
}
55-
56-
return output.joined(separator: "\n")
5759
}
5860

5961
/// Increments the Counter

Sources/Prometheus/MetricTypes/Gauge.swift

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/// See https://prometheus.io/docs/concepts/metric_types/#gauge
44
public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHandled {
55
/// Prometheus instance that created this Gauge
6-
internal let prometheus: Prometheus
6+
internal let prometheus: PrometheusClient
77

88
/// Name of the Gauge, required
99
public let name: String
@@ -15,7 +15,7 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
1515

1616
/// Current value of the counter
1717
private var value: NumType
18-
18+
1919
/// Initial value of the Gauge
2020
private var initialValue: NumType
2121

@@ -29,7 +29,7 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
2929
/// - help: Helpt text of the Gauge
3030
/// - initialValue: Initial value to set the Gauge to
3131
/// - p: Prometheus instance that created this Gauge
32-
internal init(_ name: String, _ help: String? = nil, _ initialValue: NumType = 0, _ p: Prometheus) {
32+
internal init(_ name: String, _ help: String? = nil, _ initialValue: NumType = 0, _ p: PrometheusClient) {
3333
self.name = name
3434
self.help = help
3535
self.initialValue = initialValue
@@ -41,19 +41,21 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
4141
///
4242
/// - Returns:
4343
/// Newline seperated Prometheus formatted metric string
44-
public func getMetric() -> String {
45-
var output = [String]()
46-
47-
output.append(headers)
48-
49-
output.append("\(name) \(value)")
50-
51-
metrics.forEach { (labels, value) in
52-
let labelsString = encodeLabels(labels)
53-
output.append("\(name)\(labelsString) \(value)")
44+
public func getMetric(_ done: @escaping (String) -> Void) {
45+
prometheusQueue.async(flags: .barrier) {
46+
var output = [String]()
47+
48+
output.append(self.headers)
49+
50+
output.append("\(self.name) \(self.value)")
51+
52+
self.metrics.forEach { (labels, value) in
53+
let labelsString = encodeLabels(labels)
54+
output.append("\(self.name)\(labelsString) \(value)")
55+
}
56+
57+
done(output.joined(separator: "\n"))
5458
}
55-
56-
return output.joined(separator: "\n")
5759
}
5860

5961
/// Sets the Gauge
@@ -104,7 +106,7 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
104106
public func inc(_ labels: Labels? = nil) -> NumType {
105107
return self.inc(1, labels)
106108
}
107-
109+
108110
/// Decrements the Gauge
109111
///
110112
/// - Parameters:
@@ -135,7 +137,7 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
135137
public func dec(_ labels: Labels? = nil) -> NumType {
136138
return self.dec(1, labels)
137139
}
138-
140+
139141
/// Gets the value of the Gauge
140142
///
141143
/// - Parameters:

Sources/Prometheus/MetricTypes/Histogram.swift

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension HistogramLabels {
1919
/// See https://prometheus.io/docs/concepts/metric_types/#Histogram
2020
public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: Metric, PrometheusHandled {
2121
/// Prometheus instance that created this Histogram
22-
internal let prometheus: Prometheus
22+
internal let prometheus: PrometheusClient
2323

2424
/// Name of this Histogram, required
2525
public let name: String
@@ -52,7 +52,7 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
5252
/// - labels: Labels for the Histogram
5353
/// - buckets: Buckets to use for the Histogram
5454
/// - p: Prometheus instance creating this Histogram
55-
internal init(_ name: String, _ help: String? = nil, _ labels: Labels = Labels(), _ buckets: [Double] = defaultBuckets, _ p: Prometheus) {
55+
internal init(_ name: String, _ help: String? = nil, _ labels: Labels = Labels(), _ buckets: [Double] = defaultBuckets, _ p: PrometheusClient) {
5656
self.name = name
5757
self.help = help
5858

@@ -73,44 +73,46 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
7373
///
7474
/// - Returns:
7575
/// Newline seperated Prometheus formatted metric string
76-
public func getMetric() -> String {
77-
var output = [String]()
78-
79-
output.append(headers)
80-
81-
var acc: NumType = 0
82-
for (i, bound) in self.upperBounds.enumerated() {
83-
acc += buckets[i].get()
84-
labels.le = bound.description
85-
let labelsString = encodeLabels(labels)
86-
output.append("\(name)_bucket\(labelsString) \(acc)")
87-
}
88-
89-
let labelsString = encodeLabels(labels, ["le"])
90-
output.append("\(name)_count\(labelsString) \(acc)")
91-
92-
output.append("\(name)_sum\(labelsString) \(total.get())")
93-
94-
subHistograms.forEach { subHistogram in
76+
public func getMetric(_ done: @escaping (String) -> Void) {
77+
prometheusQueue.async(flags: .barrier) {
78+
var output = [String]()
79+
80+
output.append(self.headers)
81+
9582
var acc: NumType = 0
96-
for (i, bound) in subHistogram.upperBounds.enumerated() {
97-
acc += subHistogram.buckets[i].get()
98-
subHistogram.labels.le = bound.description
99-
let labelsString = encodeLabels(subHistogram.labels)
100-
output.append("\(subHistogram.name)_bucket\(labelsString) \(acc)")
83+
for (i, bound) in self.upperBounds.enumerated() {
84+
acc += self.buckets[i].get()
85+
self.labels.le = bound.description
86+
let labelsString = encodeLabels(self.labels)
87+
output.append("\(self.name)_bucket\(labelsString) \(acc)")
10188
}
10289

103-
let labelsString = encodeLabels(subHistogram.labels, ["le"])
104-
output.append("\(subHistogram.name)_count\(labelsString) \(acc)")
90+
let labelsString = encodeLabels(self.labels, ["le"])
91+
output.append("\(self.name)_count\(labelsString) \(acc)")
10592

106-
output.append("\(subHistogram.name)_sum\(labelsString) \(subHistogram.total.get())")
93+
output.append("\(self.name)_sum\(labelsString) \(self.total.get())")
94+
95+
self.subHistograms.forEach { subHistogram in
96+
var acc: NumType = 0
97+
for (i, bound) in subHistogram.upperBounds.enumerated() {
98+
acc += subHistogram.buckets[i].get()
99+
subHistogram.labels.le = bound.description
100+
let labelsString = encodeLabels(subHistogram.labels)
101+
output.append("\(subHistogram.name)_bucket\(labelsString) \(acc)")
102+
}
103+
104+
let labelsString = encodeLabels(subHistogram.labels, ["le"])
105+
output.append("\(subHistogram.name)_count\(labelsString) \(acc)")
106+
107+
output.append("\(subHistogram.name)_sum\(labelsString) \(subHistogram.total.get())")
108+
109+
subHistogram.labels.le = ""
110+
}
107111

108-
subHistogram.labels.le = ""
112+
self.labels.le = ""
113+
114+
done(output.joined(separator: "\n"))
109115
}
110-
111-
self.labels.le = ""
112-
113-
return output.joined(separator: "\n")
114116
}
115117

116118
/// Observe a value
@@ -119,27 +121,29 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
119121
/// - value: Value to observe
120122
/// - labels: Labels to attach to the observed value
121123
public func observe(_ value: NumType, _ labels: Labels? = nil) {
122-
if let labels = labels, type(of: labels) != type(of: EmptySummaryLabels()) {
123-
let his = prometheus.getOrCreateHistogram(with: labels, for: self)
124-
his.observe(value)
125-
}
126-
self.total.inc(value)
127-
128-
for (i, bound) in self.upperBounds.enumerated() {
129-
if bound >= value.doubleValue {
130-
buckets[i].inc()
131-
return
124+
prometheusQueue.async(flags: .barrier) {
125+
if let labels = labels, type(of: labels) != type(of: EmptySummaryLabels()) {
126+
let his = self.prometheus.getOrCreateHistogram(with: labels, for: self)
127+
his.observe(value)
128+
}
129+
self.total.inc(value)
130+
131+
for (i, bound) in self.upperBounds.enumerated() {
132+
if bound >= value.doubleValue {
133+
self.buckets[i].inc()
134+
return
135+
}
132136
}
133137
}
134138
}
135139
}
136140

137-
extension Prometheus {
141+
extension PrometheusClient {
138142
fileprivate func getOrCreateHistogram<T: Numeric, U: HistogramLabels>(with labels: U, for his: Histogram<T, U>) -> Histogram<T, U> {
139143
let histograms = his.subHistograms.filter { (metric) -> Bool in
140144
guard metric.name == his.name, metric.help == his.help, metric.labels == labels else { return false }
141145
return true
142-
}
146+
}
143147
if histograms.count > 2 { fatalError("Somehow got 2 histograms with the same data type") }
144148
if let histogram = histograms.first {
145149
return histogram

Sources/Prometheus/MetricTypes/Info.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/// Info tracks key-value information, usually about a whole target
44
public class Info<Labels: MetricLabels>: Metric, PrometheusHandled {
55
/// Prometheus instance that created this Info
6-
internal let prometheus: Prometheus
6+
internal let prometheus: PrometheusClient
77

88
/// Name of the Info, required
99
public let name: String
@@ -23,7 +23,7 @@ public class Info<Labels: MetricLabels>: Metric, PrometheusHandled {
2323
/// - name: Name of the Info
2424
/// - help: Help text of the Info
2525
/// - p: Prometheus instance handling this Info
26-
internal init(_ name: String, _ help: String? = nil, _ p: Prometheus) {
26+
internal init(_ name: String, _ help: String? = nil, _ p: PrometheusClient) {
2727
self.name = name
2828
self.help = help
2929
self.prometheus = p
@@ -41,14 +41,16 @@ public class Info<Labels: MetricLabels>: Metric, PrometheusHandled {
4141
///
4242
/// - Returns:
4343
/// Newline seperated Prometheus formatted metric string
44-
public func getMetric() -> String {
45-
var output = [String]()
46-
47-
output.append(headers)
48-
49-
let labelsString = encodeLabels(labels)
50-
output.append("\(name)\(labelsString) 1.0")
51-
52-
return output.joined(separator: "\n")
44+
public func getMetric(_ done: @escaping (String) -> Void) {
45+
prometheusQueue.async(flags: .barrier) {
46+
var output = [String]()
47+
48+
output.append(self.headers)
49+
50+
let labelsString = encodeLabels(self.labels)
51+
output.append("\(self.name)\(labelsString) 1.0")
52+
53+
done(output.joined(separator: "\n"))
54+
}
5355
}
5456
}

Sources/Prometheus/MetricTypes/Metric.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public protocol Metric {
1111
var help: String? { get }
1212
var _type: MetricType { get }
1313

14-
func getMetric() -> String
14+
func getMetric(_ done: @escaping (String) -> Void)
1515
}
1616

1717
extension Metric {
@@ -29,7 +29,7 @@ extension Metric {
2929
/// Adding a prometheus instance to all
3030
/// metrics
3131
internal protocol PrometheusHandled {
32-
var prometheus: Prometheus { get }
32+
var prometheus: PrometheusClient { get }
3333
}
3434

3535
/// Base MetricLabels protocol

0 commit comments

Comments
 (0)