Skip to content

Commit b6fd495

Browse files
committed
Review feedback
1 parent dbbf950 commit b6fd495

File tree

5 files changed

+45
-47
lines changed

5 files changed

+45
-47
lines changed

Sources/Prometheus/MetricTypes/Counter.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, Prometheus
6767
/// - amount: Amount to increment the counter with
6868
/// - labels: Labels to attach to the value
6969
///
70-
public func inc(_ amount: NumType = 1, _ labels: Labels? = nil, _ done: @escaping () -> Void = { }) {
70+
public func inc(_ amount: NumType = 1, _ labels: Labels? = nil, _ done: @escaping (NumType) -> Void = { _ in }) {
7171
prometheusQueue.async(flags: .barrier) {
7272
if let labels = labels {
7373
var val = self.metrics[labels] ?? self.initialValue
@@ -76,7 +76,7 @@ public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, Prometheus
7676
} else {
7777
self.value += amount
7878
}
79-
done()
79+
done(self.value)
8080
}
8181
}
8282

Sources/Prometheus/MetricTypes/Gauge.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,16 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
8888
/// - labels: Labels to attach to the value
8989
/// - done: Completion handler
9090
///
91-
public func inc(_ amount: NumType, _ labels: Labels? = nil, _ done: @escaping () -> Void = { }) {
91+
public func inc(_ amount: NumType, _ labels: Labels? = nil, _ done: @escaping (NumType) -> Void = { _ in }) {
9292
prometheusQueue.async(flags: .barrier) {
9393
if let labels = labels {
9494
var val = self.metrics[labels] ?? self.initialValue
9595
val += amount
9696
self.metrics[labels] = val
97-
done()
97+
done(val)
9898
} else {
9999
self.value += amount
100-
done()
100+
done(self.value)
101101
}
102102
}
103103
}
@@ -108,9 +108,9 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
108108
/// - labels: Labels to attach to the value
109109
/// - done: Completion handler
110110
///
111-
public func inc(_ labels: Labels? = nil, _ done: @escaping () -> Void = { }) {
112-
self.inc(1, labels) {
113-
done()
111+
public func inc(_ labels: Labels? = nil, _ done: @escaping (NumType) -> Void = { _ in }) {
112+
self.inc(1, labels) { value in
113+
done(value)
114114
}
115115
}
116116

@@ -121,16 +121,16 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
121121
/// - labels: Labels to attach to the value
122122
/// - done: Completion handler
123123
///
124-
public func dec(_ amount: NumType, _ labels: Labels? = nil, _ done: @escaping () -> Void = { }) {
124+
public func dec(_ amount: NumType, _ labels: Labels? = nil, _ done: @escaping (NumType) -> Void = { _ in }) {
125125
prometheusQueue.async(flags: .barrier) {
126126
if let labels = labels {
127127
var val = self.metrics[labels] ?? self.initialValue
128128
val -= amount
129129
self.metrics[labels] = val
130-
done()
130+
done(val)
131131
} else {
132132
self.value -= amount
133-
done()
133+
done(self.value)
134134
}
135135
}
136136
}
@@ -140,9 +140,9 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
140140
/// - Parameters:
141141
/// - labels: Labels to attach to the value
142142
///
143-
public func dec(_ labels: Labels? = nil, _ done: @escaping () -> Void = { }) {
144-
self.dec(1, labels) {
145-
done()
143+
public func dec(_ labels: Labels? = nil, _ done: @escaping (NumType) -> Void = { _ in }) {
144+
self.dec(1, labels) { value in
145+
done(value)
146146
}
147147
}
148148

Sources/Prometheus/MetricTypes/Histogram.swift

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,30 +126,23 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
126126
/// - done: Completion handler
127127
public func observe(_ value: NumType, _ labels: Labels? = nil, _ done: @escaping () -> Void = { }) {
128128
prometheusQueue.async(flags: .barrier) {
129-
if let labels = labels, type(of: labels) != type(of: EmptySummaryLabels()) {
130-
let his = self.prometheus.getOrCreateHistogram(with: labels, for: self)
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-
}
129+
let completion: () -> Void = {
130+
self.total.inc(value)
131+
132+
for (i, bound) in self.upperBounds.enumerated() {
133+
if bound >= value.doubleValue {
134+
self.buckets[i].inc()
135+
break
139136
}
140-
done()
141137
}
142-
return
138+
done()
143139
}
144-
self.total.inc(value)
145140

146-
for (i, bound) in self.upperBounds.enumerated() {
147-
if bound >= value.doubleValue {
148-
self.buckets[i].inc()
149-
break
150-
}
141+
if let labels = labels, type(of: labels) != type(of: EmptySummaryLabels()) {
142+
let his = self.prometheus.getOrCreateHistogram(with: labels, for: self)
143+
return his.observe(value, nil, completion)
151144
}
152-
done()
145+
return completion()
153146
}
154147
}
155148
}

Sources/Prometheus/MetricTypes/Summary.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,20 @@ public class Summary<NumType: DoubleRepresentable, Labels: SummaryLabels>: Metri
127127
///
128128
public func observe(_ value: NumType, _ labels: Labels? = nil, _ done: @escaping () -> Void = { }) {
129129
prometheusQueue.async(flags: .barrier) {
130+
func completion() {
131+
self.count.inc(1)
132+
self.sum.inc(value)
133+
self.values.append(value)
134+
done()
135+
}
136+
130137
if let labels = labels, type(of: labels) != type(of: EmptySummaryLabels()) {
131138
let sum = self.prometheus.getOrCreateSummary(withLabels: labels, forSummary: self)
132139
sum.observe(value) {
133-
self.count.inc(1)
134-
self.sum.inc(value)
135-
self.values.append(value)
136-
done()
140+
completion()
137141
}
138-
return
139142
} else {
140-
self.count.inc(1)
141-
self.sum.inc(value)
142-
self.values.append(value)
143-
done()
143+
completion()
144144
}
145145
}
146146
}

Tests/SwiftPrometheusTests/SwiftPrometheusTests.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ final class SwiftPrometheusTests: XCTestCase {
5757

5858
let counter = prom.createCounter(forType: Int.self, named: "my_counter", helpText: "Counter for testing", initialValue: 10, withLabelType: BaseLabels.self)
5959
XCTAssertEqual(counter.get(), 10)
60-
counter.inc(10) {
60+
counter.inc(10) { value in
61+
XCTAssertEqual(value, 20)
6162
semaphore.signal()
6263
}
6364
semaphore.wait()
6465
XCTAssertEqual(counter.get(), 20)
6566

66-
counter.inc(10, BaseLabels(myValue: "labels")) {
67+
counter.inc(10, BaseLabels(myValue: "labels")) { value in
68+
XCTAssertEqual(value, 20)
6769
semaphore.signal()
6870
}
6971
semaphore.wait()
@@ -82,13 +84,15 @@ final class SwiftPrometheusTests: XCTestCase {
8284

8385
let gauge = prom.createGauge(forType: Int.self, named: "my_gauge", helpText: "Gauge for testing", initialValue: 10, withLabelType: BaseLabels.self)
8486
XCTAssertEqual(gauge.get(), 10)
85-
gauge.inc(10) {
87+
gauge.inc(10) { value in
88+
XCTAssertEqual(value, 20)
8689
semaphore.signal()
8790
}
8891
semaphore.wait()
8992

9093
XCTAssertEqual(gauge.get(), 20)
91-
gauge.dec(12) {
94+
gauge.dec(12) { value in
95+
XCTAssertEqual(value, 8)
9296
semaphore.signal()
9397
}
9498
semaphore.wait()
@@ -99,7 +103,8 @@ final class SwiftPrometheusTests: XCTestCase {
99103
}
100104
semaphore.wait()
101105

102-
gauge.inc(10, BaseLabels(myValue: "labels")) {
106+
gauge.inc(10, BaseLabels(myValue: "labels")) { value in
107+
XCTAssertEqual(value, 20)
103108
semaphore.signal()
104109
}
105110
semaphore.wait()

0 commit comments

Comments
 (0)