Skip to content

Commit a8e35e9

Browse files
committed
Bunch of more thread-safety (I hope)
1 parent f1bc21d commit a8e35e9

File tree

7 files changed

+48
-27
lines changed

7 files changed

+48
-27
lines changed

Sources/Prometheus/MetricTypes/Counter.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, Prometheus
4545
prometheusQueue.async(flags: .barrier) {
4646
var output = [String]()
4747

48-
output.append(self.headers)
49-
48+
if let help = self.help {
49+
output.append("# HELP \(self.name) \(help)")
50+
}
51+
output.append("# TYPE \(self.name) \(self._type)")
52+
5053
output.append("\(self.name) \(self.value)")
5154

5255
self.metrics.forEach { (labels, value) in

Sources/Prometheus/MetricTypes/Gauge.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
4545
prometheusQueue.async(flags: .barrier) {
4646
var output = [String]()
4747

48-
output.append(self.headers)
49-
48+
if let help = self.help {
49+
output.append("# HELP \(self.name) \(help)")
50+
}
51+
output.append("# TYPE \(self.name) \(self._type)")
52+
5053
output.append("\(self.name) \(self.value)")
5154

5255
self.metrics.forEach { (labels, value) in

Sources/Prometheus/MetricTypes/Histogram.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
7777
prometheusQueue.async(flags: .barrier) {
7878
var output = [String]()
7979

80-
output.append(self.headers)
81-
80+
if let help = self.help {
81+
output.append("# HELP \(self.name) \(help)")
82+
}
83+
output.append("# TYPE \(self.name) \(self._type)")
84+
8285
var acc: NumType = 0
8386
for (i, bound) in self.upperBounds.enumerated() {
8487
acc += self.buckets[i].get()

Sources/Prometheus/MetricTypes/Info.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ public class Info<Labels: MetricLabels>: Metric, PrometheusHandled {
4545
prometheusQueue.async(flags: .barrier) {
4646
var output = [String]()
4747

48-
output.append(self.headers)
48+
if let help = self.help {
49+
output.append("# HELP \(self.name) \(help)")
50+
}
51+
output.append("# TYPE \(self.name) \(self._type)")
4952

5053
let labelsString = encodeLabels(self.labels)
5154
output.append("\(self.name)\(labelsString) 1.0")

Sources/Prometheus/MetricTypes/Metric.swift

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,13 @@ public protocol Metric {
1414
func getMetric(_ done: @escaping (String) -> Void)
1515
}
1616

17-
extension Metric {
18-
/// Default headers for a metric
19-
var headers: String {
20-
var output = [String]()
21-
if let help = help {
22-
output.append("# HELP \(name) \(help)")
23-
}
24-
output.append("# TYPE \(name) \(_type)")
25-
return output.joined(separator: "\n")
26-
}
27-
}
17+
//extension Metric {
18+
// /// Default headers for a metric
19+
// var headers: String {
20+
// var output = [String]()
21+
// return output.joined(separator: "\n")
22+
// }
23+
//}
2824

2925
/// Adding a prometheus instance to all
3026
/// metrics

Sources/Prometheus/MetricTypes/Summary.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ public class Summary<NumType: DoubleRepresentable, Labels: SummaryLabels>: Metri
7878
prometheusQueue.async(flags: .barrier) {
7979
var output = [String]()
8080

81-
output.append(self.headers)
82-
81+
if let help = self.help {
82+
output.append("# HELP \(self.name) \(help)")
83+
}
84+
output.append("# TYPE \(self.name) \(self._type)")
85+
8386
calculateQuantiles(quantiles: self.quantiles, values: self.values.map { $0.doubleValue }).sorted { $0.key < $1.key }.forEach { (arg) in
8487
let (q, v) = arg
8588
self.labels.quantile = "\(q)"

Sources/Prometheus/Prometheus.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class PrometheusClient {
1414
///
1515
/// - Returns: Newline seperated string with metrics for all Metric Trackers of this Prometheus instance
1616
public func getMetrics(_ done: @escaping (String) -> Void) {
17-
// prometheusQueue.async(flags: .barrier) {
17+
prometheusQueue.async(flags: .barrier) {
1818
var list = [String]()
1919
self.metrics.forEach { metric in
2020
metric.getMetric { str in
@@ -25,7 +25,7 @@ public class PrometheusClient {
2525
}
2626
}
2727
}
28-
// }
28+
}
2929
}
3030

3131
// MARK: - Counter
@@ -48,7 +48,9 @@ public class PrometheusClient {
4848
withLabelType labelType: U.Type) -> Counter<T, U>
4949
{
5050
let counter = Counter<T, U>(name, helpText, initialValue, self)
51-
self.metrics.append(counter)
51+
prometheusQueue.async(flags: .barrier) {
52+
self.metrics.append(counter)
53+
}
5254
return counter
5355
}
5456

@@ -90,7 +92,9 @@ public class PrometheusClient {
9092
withLabelType labelType: U.Type) -> Gauge<T, U>
9193
{
9294
let gauge = Gauge<T, U>(name, helpText, initialValue, self)
93-
self.metrics.append(gauge)
95+
prometheusQueue.async(flags: .barrier) {
96+
self.metrics.append(gauge)
97+
}
9498
return gauge
9599
}
96100

@@ -132,7 +136,9 @@ public class PrometheusClient {
132136
labels: U.Type) -> Histogram<T, U>
133137
{
134138
let histogram = Histogram<T, U>(name, helpText, U(), buckets, self)
135-
self.metrics.append(histogram)
139+
prometheusQueue.async(flags: .barrier) {
140+
self.metrics.append(histogram)
141+
}
136142
return histogram
137143
}
138144

@@ -174,7 +180,9 @@ public class PrometheusClient {
174180
labels: U.Type) -> Summary<T, U>
175181
{
176182
let summary = Summary<T, U>(name, helpText, U(), quantiles, self)
177-
metrics.append(summary)
183+
prometheusQueue.async(flags: .barrier) {
184+
self.metrics.append(summary)
185+
}
178186
return summary
179187
}
180188

@@ -212,7 +220,9 @@ public class PrometheusClient {
212220
labelType: U.Type) -> Info<U>
213221
{
214222
let info = Info<U>(name, helpText, self)
215-
self.metrics.append(info)
223+
prometheusQueue.async(flags: .barrier) {
224+
self.metrics.append(info)
225+
}
216226
return info
217227
}
218228
}

0 commit comments

Comments
 (0)