Skip to content

Commit

Permalink
Fix label equality issue
Browse files Browse the repository at this point in the history
  • Loading branch information
MrLotU committed Aug 2, 2021
1 parent 156c16e commit daf102f
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 100 deletions.
2 changes: 1 addition & 1 deletion Sources/Prometheus/PrometheusMetrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public struct DimensionLabels: MetricLabels {
for index in 0..<lhs.dimensions.count {
guard lhs.dimensions[index] == rhs.dimensions[index] else { return false }
}
return false
return true

This comment has been minimized.

Copy link
@rauhul

rauhul Aug 3, 2021

Contributor

Thanks for catching this

}
}

Expand Down
198 changes: 99 additions & 99 deletions Sources/PrometheusExample/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,111 +6,111 @@ let myProm = PrometheusClient()

MetricsSystem.bootstrap(PrometheusMetricsFactory(client: myProm))

for _ in 0...Int.random(in: 10...100) {
let c = Counter(label: "test")
c.increment()
}
//for _ in 0...Int.random(in: 10...100) {
// let c = Counter(label: "test")
// c.increment()
//}

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

for _ in 0...Int.random(in: 100...500_000) {
let r = Recorder(label: "recorder")
r.record(Double.random(in: 0...20))
}

for _ in 0...Int.random(in: 100...500_000) {
let g = Gauge(label: "non_agg_recorder")
g.record(Double.random(in: 0...20))
}

for _ in 0...Int.random(in: 100...500_000) {
let t = Timer(label: "timer")
t.recordMicroseconds(Double.random(in: 20...150))
}

for _ in 0...Int.random(in: 100...500_000) {
let r = Recorder(label: "recorder", dimensions: [("abc", "123")])
r.record(Double.random(in: 0...20))
}

for _ in 0...Int.random(in: 100...500_000) {
let g = Gauge(label: "non_agg_recorder", dimensions: [("abc", "123")])
g.record(Double.random(in: 0...20))
}

for _ in 0...Int.random(in: 100...500_000) {
let t = Timer(label: "timer", dimensions: [("abc", "123")])
t.recordMicroseconds(Double.random(in: 20...150))
}


struct MyCodable: MetricLabels {
var thing: String = "*"
}

let codable1 = MyCodable(thing: "Thing1")
let codable2 = MyCodable(thing: "Thing2")

let counter = myProm.createCounter(forType: Int.self, named: "my_counter", helpText: "Just a counter", initialValue: 12, withLabelType: MyCodable.self)

counter.inc(5)
counter.inc(Int.random(in: 0...100), codable2)
counter.inc(Int.random(in: 0...100), codable1)

let gauge = myProm.createGauge(forType: Int.self, named: "my_gauge", helpText: "Just a gauge", initialValue: 12, withLabelType: MyCodable.self)

gauge.inc(100)
gauge.inc(Int.random(in: 0...100), codable2)
gauge.inc(Int.random(in: 0...100), codable1)

struct HistogramThing: HistogramLabels {
var le: String = ""
let route: String

init() {
self.route = "*"
}

init(_ route: String) {
self.route = route
}
}

let histogram = myProm.createHistogram(forType: Double.self, named: "my_histogram", helpText: "Just a histogram", labels: HistogramThing.self)

for _ in 0...Int.random(in: 10...50) {
histogram.observe(Double.random(in: 0...1))
}

for _ in 0...Int.random(in: 10...50) {
histogram.observe(Double.random(in: 0...1), HistogramThing("/test"))
}

struct SummaryThing: SummaryLabels {
var quantile: String = ""
let route: String

init() {
self.route = "*"
}

init(_ route: String) {
self.route = route
}
}

let summary = myProm.createSummary(forType: Double.self, named: "my_summary", helpText: "Just a summary", labels: SummaryThing.self)

for _ in 0...Int.random(in: 100...1000) {
summary.observe(Double.random(in: 0...10000))
}

for _ in 0...Int.random(in: 100...1000) {
summary.observe(Double.random(in: 0...10000), SummaryThing("/test"))
}
//for _ in 0...Int.random(in: 100...500_000) {
// let r = Recorder(label: "recorder")
// r.record(Double.random(in: 0...20))
//}
//
//for _ in 0...Int.random(in: 100...500_000) {
// let g = Gauge(label: "non_agg_recorder")
// g.record(Double.random(in: 0...20))
//}
//
//for _ in 0...Int.random(in: 100...500_000) {
// let t = Timer(label: "timer")
// t.recordMicroseconds(Double.random(in: 20...150))
//}
//
//for _ in 0...Int.random(in: 100...500_000) {
// let r = Recorder(label: "recorder", dimensions: [("abc", "123")])
// r.record(Double.random(in: 0...20))
//}
//
//for _ in 0...Int.random(in: 100...500_000) {
// let g = Gauge(label: "non_agg_recorder", dimensions: [("abc", "123")])
// g.record(Double.random(in: 0...20))
//}
//
//for _ in 0...Int.random(in: 100...500_000) {
// let t = Timer(label: "timer", dimensions: [("abc", "123")])
// t.recordMicroseconds(Double.random(in: 20...150))
//}


//struct MyCodable: MetricLabels {
// var thing: String = "*"
//}
//
//let codable1 = MyCodable(thing: "Thing1")
//let codable2 = MyCodable(thing: "Thing2")
//
//let counter = myProm.createCounter(forType: Int.self, named: "my_counter", helpText: "Just a counter", initialValue: 12, withLabelType: MyCodable.self)
//
//counter.inc(5)
//counter.inc(Int.random(in: 0...100), codable2)
//counter.inc(Int.random(in: 0...100), codable1)
//
//let gauge = myProm.createGauge(forType: Int.self, named: "my_gauge", helpText: "Just a gauge", initialValue: 12, withLabelType: MyCodable.self)
//
//gauge.inc(100)
//gauge.inc(Int.random(in: 0...100), codable2)
//gauge.inc(Int.random(in: 0...100), codable1)
//
//struct HistogramThing: HistogramLabels {
// var le: String = ""
// let route: String
//
// init() {
// self.route = "*"
// }
//
// init(_ route: String) {
// self.route = route
// }
//}
//
//let histogram = myProm.createHistogram(forType: Double.self, named: "my_histogram", helpText: "Just a histogram", labels: HistogramThing.self)
//
//for _ in 0...Int.random(in: 10...50) {
// histogram.observe(Double.random(in: 0...1))
//}
//
//for _ in 0...Int.random(in: 10...50) {
// histogram.observe(Double.random(in: 0...1), HistogramThing("/test"))
//}
//
//struct SummaryThing: SummaryLabels {
// var quantile: String = ""
// let route: String
//
// init() {
// self.route = "*"
// }
//
// init(_ route: String) {
// self.route = route
// }
//}
//
//let summary = myProm.createSummary(forType: Double.self, named: "my_summary", helpText: "Just a summary", labels: SummaryThing.self)
//
//for _ in 0...Int.random(in: 100...1000) {
// summary.observe(Double.random(in: 0...10000))
//}
//
//for _ in 0...Int.random(in: 100...1000) {
// summary.observe(Double.random(in: 0...10000), SummaryThing("/test"))
//}

let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let prom = elg.next().makePromise(of: String.self)
Expand Down
10 changes: 10 additions & 0 deletions Tests/SwiftPrometheusTests/PrometheusMetricsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,14 @@ final class PrometheusMetricsTests: XCTestCase {
let summary: PromSummary<Int64, DimensionSummaryLabels>? = prom.getMetricInstance(with: "duration_nanos", andType: .summary)
XCTAssertNil(summary)
}

func testDimensionLabelEquality() {
let labelsA = DimensionLabels([("a", "a")])
let labelsB = DimensionLabels([("b", "b")])
let labelsATwo = DimensionLabels([("a", "a")])

XCTAssertEqual(labelsA, labelsATwo)
XCTAssertNotEqual(labelsA, labelsB)
XCTAssertNotEqual(labelsATwo, labelsB)
}
}

0 comments on commit daf102f

Please sign in to comment.