Skip to content

Commit 317b719

Browse files
committed
Fixup error in Counter/Gauge
1 parent 3fdf15d commit 317b719

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

Sources/Prometheus/MetricTypes/Counter.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, Prometheus
66

77
internal var value: NumType
88

9+
private var initialValue: NumType
10+
911
internal var metrics: [Labels: NumType] = [:]
1012

1113
internal init(_ name: String, _ help: String? = nil, _ initialValue: NumType = 0, _ p: Prometheus) {
1214
self.name = name
1315
self.help = help
16+
self.initialValue = initialValue
1417
self.value = initialValue
1518
self.prometheus = p
1619
}
@@ -36,7 +39,7 @@ public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, Prometheus
3639
@discardableResult
3740
public func inc(_ amount: NumType = 1, _ labels: Labels? = nil) -> NumType {
3841
if let labels = labels {
39-
var val = self.metrics[labels] ?? 0
42+
var val = self.metrics[labels] ?? initialValue
4043
val += amount
4144
self.metrics[labels] = val
4245
return val
@@ -48,7 +51,7 @@ public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, Prometheus
4851

4952
public func get(_ labels: Labels? = nil) -> NumType {
5053
if let labels = labels {
51-
return self.metrics[labels] ?? 0
54+
return self.metrics[labels] ?? initialValue
5255
} else {
5356
return self.value
5457
}

Sources/Prometheus/MetricTypes/Gauge.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
55
public let help: String?
66

77
private var value: NumType
8+
9+
private var initialValue: NumType
810

911
private var metrics: [Labels: NumType] = [:]
1012

1113
internal init(_ name: String, _ help: String? = nil, _ initialValue: NumType = 0, _ p: Prometheus) {
1214
self.name = name
1315
self.help = help
16+
self.initialValue = initialValue
1417
self.value = initialValue
1518
self.prometheus = p
1619
}
@@ -47,7 +50,7 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
4750
@discardableResult
4851
public func inc(_ amount: NumType, _ labels: Labels? = nil) -> NumType {
4952
if let labels = labels {
50-
var val = self.metrics[labels] ?? 0
53+
var val = self.metrics[labels] ?? initialValue
5154
val += amount
5255
self.metrics[labels] = val
5356
return val
@@ -65,7 +68,7 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
6568
@discardableResult
6669
public func dec(_ amount: NumType, _ labels: Labels? = nil) -> NumType {
6770
if let labels = labels {
68-
var val = self.metrics[labels] ?? 0
71+
var val = self.metrics[labels] ?? initialValue
6972
val -= amount
7073
self.metrics[labels] = val
7174
return val
@@ -82,7 +85,7 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
8285

8386
public func get(_ labels: Labels? = nil) -> NumType {
8487
if let labels = labels {
85-
return self.metrics[labels] ?? 0
88+
return self.metrics[labels] ?? initialValue
8689
} else {
8790
return self.value
8891
}

Sources/PrometheusExample/main.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ struct MyCodable: MetricLabels {
77
let codable1 = MyCodable(thing: "Thing1")
88
let codable2 = MyCodable(thing: "Thing2")
99

10-
//let counter = Prometheus.shared.createCounter(forType: Int.self, named: "my_counter", helpText: "Just a counter", initialValue: 12)
11-
//
12-
//counter.inc(5)
13-
//
14-
//let gauge = Prometheus.shared.createGauge(forType: Int.self, named: "my_gauge", helpText: "Just a gauge", initialValue: 0)
15-
//
16-
//gauge.set(123)
10+
let counter = Prometheus.shared.createCounter(forType: Int.self, named: "my_counter", helpText: "Just a counter", initialValue: 12, withLabelType: MyCodable.self)
11+
12+
counter.inc(5)
13+
counter.inc(Int.random(in: 0...100), codable2)
14+
counter.inc(Int.random(in: 0...100), codable1)
15+
16+
let gauge = Prometheus.shared.createGauge(forType: Int.self, named: "my_gauge", helpText: "Just a gauge", initialValue: 12, withLabelType: MyCodable.self)
17+
18+
gauge.inc(100)
19+
gauge.inc(Int.random(in: 0...100), codable2)
20+
gauge.inc(Int.random(in: 0...100), codable1)
1721

1822
struct HistogramThing: HistogramLabels {
1923
var le: String = ""

0 commit comments

Comments
 (0)