Skip to content

Commit daf102f

Browse files
committed
Fix label equality issue
1 parent 156c16e commit daf102f

File tree

3 files changed

+110
-100
lines changed

3 files changed

+110
-100
lines changed

Sources/Prometheus/PrometheusMetrics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public struct DimensionLabels: MetricLabels {
354354
for index in 0..<lhs.dimensions.count {
355355
guard lhs.dimensions[index] == rhs.dimensions[index] else { return false }
356356
}
357-
return false
357+
return true
358358
}
359359
}
360360

Sources/PrometheusExample/main.swift

Lines changed: 99 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -6,111 +6,111 @@ let myProm = PrometheusClient()
66

77
MetricsSystem.bootstrap(PrometheusMetricsFactory(client: myProm))
88

9-
for _ in 0...Int.random(in: 10...100) {
10-
let c = Counter(label: "test")
11-
c.increment()
12-
}
9+
//for _ in 0...Int.random(in: 10...100) {
10+
// let c = Counter(label: "test")
11+
// c.increment()
12+
//}
1313

1414
for _ in 0...Int.random(in: 10...100) {
1515
let c = Counter(label: "test", dimensions: [("abc", "123")])
1616
c.increment()
1717
}
1818

19-
for _ in 0...Int.random(in: 100...500_000) {
20-
let r = Recorder(label: "recorder")
21-
r.record(Double.random(in: 0...20))
22-
}
23-
24-
for _ in 0...Int.random(in: 100...500_000) {
25-
let g = Gauge(label: "non_agg_recorder")
26-
g.record(Double.random(in: 0...20))
27-
}
28-
29-
for _ in 0...Int.random(in: 100...500_000) {
30-
let t = Timer(label: "timer")
31-
t.recordMicroseconds(Double.random(in: 20...150))
32-
}
33-
34-
for _ in 0...Int.random(in: 100...500_000) {
35-
let r = Recorder(label: "recorder", dimensions: [("abc", "123")])
36-
r.record(Double.random(in: 0...20))
37-
}
38-
39-
for _ in 0...Int.random(in: 100...500_000) {
40-
let g = Gauge(label: "non_agg_recorder", dimensions: [("abc", "123")])
41-
g.record(Double.random(in: 0...20))
42-
}
43-
44-
for _ in 0...Int.random(in: 100...500_000) {
45-
let t = Timer(label: "timer", dimensions: [("abc", "123")])
46-
t.recordMicroseconds(Double.random(in: 20...150))
47-
}
48-
49-
50-
struct MyCodable: MetricLabels {
51-
var thing: String = "*"
52-
}
53-
54-
let codable1 = MyCodable(thing: "Thing1")
55-
let codable2 = MyCodable(thing: "Thing2")
56-
57-
let counter = myProm.createCounter(forType: Int.self, named: "my_counter", helpText: "Just a counter", initialValue: 12, withLabelType: MyCodable.self)
58-
59-
counter.inc(5)
60-
counter.inc(Int.random(in: 0...100), codable2)
61-
counter.inc(Int.random(in: 0...100), codable1)
62-
63-
let gauge = myProm.createGauge(forType: Int.self, named: "my_gauge", helpText: "Just a gauge", initialValue: 12, withLabelType: MyCodable.self)
64-
65-
gauge.inc(100)
66-
gauge.inc(Int.random(in: 0...100), codable2)
67-
gauge.inc(Int.random(in: 0...100), codable1)
68-
69-
struct HistogramThing: HistogramLabels {
70-
var le: String = ""
71-
let route: String
72-
73-
init() {
74-
self.route = "*"
75-
}
76-
77-
init(_ route: String) {
78-
self.route = route
79-
}
80-
}
81-
82-
let histogram = myProm.createHistogram(forType: Double.self, named: "my_histogram", helpText: "Just a histogram", labels: HistogramThing.self)
83-
84-
for _ in 0...Int.random(in: 10...50) {
85-
histogram.observe(Double.random(in: 0...1))
86-
}
87-
88-
for _ in 0...Int.random(in: 10...50) {
89-
histogram.observe(Double.random(in: 0...1), HistogramThing("/test"))
90-
}
91-
92-
struct SummaryThing: SummaryLabels {
93-
var quantile: String = ""
94-
let route: String
95-
96-
init() {
97-
self.route = "*"
98-
}
99-
100-
init(_ route: String) {
101-
self.route = route
102-
}
103-
}
104-
105-
let summary = myProm.createSummary(forType: Double.self, named: "my_summary", helpText: "Just a summary", labels: SummaryThing.self)
106-
107-
for _ in 0...Int.random(in: 100...1000) {
108-
summary.observe(Double.random(in: 0...10000))
109-
}
110-
111-
for _ in 0...Int.random(in: 100...1000) {
112-
summary.observe(Double.random(in: 0...10000), SummaryThing("/test"))
113-
}
19+
//for _ in 0...Int.random(in: 100...500_000) {
20+
// let r = Recorder(label: "recorder")
21+
// r.record(Double.random(in: 0...20))
22+
//}
23+
//
24+
//for _ in 0...Int.random(in: 100...500_000) {
25+
// let g = Gauge(label: "non_agg_recorder")
26+
// g.record(Double.random(in: 0...20))
27+
//}
28+
//
29+
//for _ in 0...Int.random(in: 100...500_000) {
30+
// let t = Timer(label: "timer")
31+
// t.recordMicroseconds(Double.random(in: 20...150))
32+
//}
33+
//
34+
//for _ in 0...Int.random(in: 100...500_000) {
35+
// let r = Recorder(label: "recorder", dimensions: [("abc", "123")])
36+
// r.record(Double.random(in: 0...20))
37+
//}
38+
//
39+
//for _ in 0...Int.random(in: 100...500_000) {
40+
// let g = Gauge(label: "non_agg_recorder", dimensions: [("abc", "123")])
41+
// g.record(Double.random(in: 0...20))
42+
//}
43+
//
44+
//for _ in 0...Int.random(in: 100...500_000) {
45+
// let t = Timer(label: "timer", dimensions: [("abc", "123")])
46+
// t.recordMicroseconds(Double.random(in: 20...150))
47+
//}
48+
49+
50+
//struct MyCodable: MetricLabels {
51+
// var thing: String = "*"
52+
//}
53+
//
54+
//let codable1 = MyCodable(thing: "Thing1")
55+
//let codable2 = MyCodable(thing: "Thing2")
56+
//
57+
//let counter = myProm.createCounter(forType: Int.self, named: "my_counter", helpText: "Just a counter", initialValue: 12, withLabelType: MyCodable.self)
58+
//
59+
//counter.inc(5)
60+
//counter.inc(Int.random(in: 0...100), codable2)
61+
//counter.inc(Int.random(in: 0...100), codable1)
62+
//
63+
//let gauge = myProm.createGauge(forType: Int.self, named: "my_gauge", helpText: "Just a gauge", initialValue: 12, withLabelType: MyCodable.self)
64+
//
65+
//gauge.inc(100)
66+
//gauge.inc(Int.random(in: 0...100), codable2)
67+
//gauge.inc(Int.random(in: 0...100), codable1)
68+
//
69+
//struct HistogramThing: HistogramLabels {
70+
// var le: String = ""
71+
// let route: String
72+
//
73+
// init() {
74+
// self.route = "*"
75+
// }
76+
//
77+
// init(_ route: String) {
78+
// self.route = route
79+
// }
80+
//}
81+
//
82+
//let histogram = myProm.createHistogram(forType: Double.self, named: "my_histogram", helpText: "Just a histogram", labels: HistogramThing.self)
83+
//
84+
//for _ in 0...Int.random(in: 10...50) {
85+
// histogram.observe(Double.random(in: 0...1))
86+
//}
87+
//
88+
//for _ in 0...Int.random(in: 10...50) {
89+
// histogram.observe(Double.random(in: 0...1), HistogramThing("/test"))
90+
//}
91+
//
92+
//struct SummaryThing: SummaryLabels {
93+
// var quantile: String = ""
94+
// let route: String
95+
//
96+
// init() {
97+
// self.route = "*"
98+
// }
99+
//
100+
// init(_ route: String) {
101+
// self.route = route
102+
// }
103+
//}
104+
//
105+
//let summary = myProm.createSummary(forType: Double.self, named: "my_summary", helpText: "Just a summary", labels: SummaryThing.self)
106+
//
107+
//for _ in 0...Int.random(in: 100...1000) {
108+
// summary.observe(Double.random(in: 0...10000))
109+
//}
110+
//
111+
//for _ in 0...Int.random(in: 100...1000) {
112+
// summary.observe(Double.random(in: 0...10000), SummaryThing("/test"))
113+
//}
114114

115115
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
116116
let prom = elg.next().makePromise(of: String.self)

Tests/SwiftPrometheusTests/PrometheusMetricsTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,14 @@ final class PrometheusMetricsTests: XCTestCase {
198198
let summary: PromSummary<Int64, DimensionSummaryLabels>? = prom.getMetricInstance(with: "duration_nanos", andType: .summary)
199199
XCTAssertNil(summary)
200200
}
201+
202+
func testDimensionLabelEquality() {
203+
let labelsA = DimensionLabels([("a", "a")])
204+
let labelsB = DimensionLabels([("b", "b")])
205+
let labelsATwo = DimensionLabels([("a", "a")])
206+
207+
XCTAssertEqual(labelsA, labelsATwo)
208+
XCTAssertNotEqual(labelsA, labelsB)
209+
XCTAssertNotEqual(labelsATwo, labelsB)
210+
}
201211
}

0 commit comments

Comments
 (0)