Skip to content

Commit dbbf950

Browse files
committed
Show off the problem with dual-writing metrics from one function call.
We might want to instead break this out into two separate functions— one that takes a label, the other that doesn't. That might allow us to then properly pass the callback and only call it once when everything is done.
1 parent 04e4d48 commit dbbf950

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

Sources/Prometheus/MetricTypes/Histogram.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,18 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
128128
prometheusQueue.async(flags: .barrier) {
129129
if let labels = labels, type(of: labels) != type(of: EmptySummaryLabels()) {
130130
let his = self.prometheus.getOrCreateHistogram(with: labels, for: self)
131-
his.observe(value)
131+
his.observe(value) {
132+
self.total.inc(value)
133+
134+
for (i, bound) in self.upperBounds.enumerated() {
135+
if bound >= value.doubleValue {
136+
self.buckets[i].inc()
137+
break
138+
}
139+
}
140+
done()
141+
}
142+
return
132143
}
133144
self.total.inc(value)
134145

Sources/Prometheus/MetricTypes/Summary.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Dispatch
2+
13
/// Default quantiles used by Summaries
24
public var defaultQuantiles = [0.01, 0.05, 0.5, 0.9, 0.95, 0.99, 0.999]
35

@@ -127,12 +129,19 @@ public class Summary<NumType: DoubleRepresentable, Labels: SummaryLabels>: Metri
127129
prometheusQueue.async(flags: .barrier) {
128130
if let labels = labels, type(of: labels) != type(of: EmptySummaryLabels()) {
129131
let sum = self.prometheus.getOrCreateSummary(withLabels: labels, forSummary: self)
130-
sum.observe(value)
132+
sum.observe(value) {
133+
self.count.inc(1)
134+
self.sum.inc(value)
135+
self.values.append(value)
136+
done()
137+
}
138+
return
139+
} else {
140+
self.count.inc(1)
141+
self.sum.inc(value)
142+
self.values.append(value)
143+
done()
131144
}
132-
self.count.inc(1)
133-
self.sum.inc(value)
134-
self.values.append(value)
135-
done()
136145
}
137146
}
138147
}

0 commit comments

Comments
 (0)