Skip to content

Commit fb378fd

Browse files
authored
New line in reporter (#43)
* fix collection to strign * make collection into string consistent with collection into buffer
1 parent db5bb3d commit fb378fd

File tree

7 files changed

+48
-12
lines changed

7 files changed

+48
-12
lines changed

Sources/Prometheus/Prometheus.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class PrometheusClient {
3636
/// - succeed: Closure that will be called with a newline separated string with metrics for all Metrics this PrometheusClient handles
3737
public func collect(_ succeed: (String) -> ()) {
3838
self.lock.withLock {
39-
succeed(self.metrics.map { $0.collect() }.joined(separator: "\n"))
39+
succeed(self.metrics.isEmpty ? "": "\(self.metrics.map { $0.collect() }.joined(separator: "\n"))\n")
4040
}
4141
}
4242

Tests/SwiftPrometheusTests/GaugeTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ final class GaugeTests: XCTestCase {
4949
XCTAssertEqual(try! promise.futureResult.wait(), """
5050
# TYPE my_gauge gauge
5151
my_gauge 20.0
52-
my_gauge{myValue=\"labels\"} 10.0
52+
my_gauge{myValue=\"labels\"} 10.0\n
5353
""")
5454
}
5555

Tests/SwiftPrometheusTests/HistogramTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ final class HistogramTests: XCTestCase {
7575
my_histogram_bucket{myValue="labels", le="10.0"} 1.0
7676
my_histogram_bucket{myValue="labels", le="+Inf"} 1.0
7777
my_histogram_count{myValue="labels"} 1.0
78-
my_histogram_sum{myValue="labels"} 3.0
78+
my_histogram_sum{myValue="labels"} 3.0\n
7979
""")
8080
}
8181

Tests/SwiftPrometheusTests/PrometheusMetricsTests.swift

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class PrometheusMetricsTests: XCTestCase {
3434
XCTAssertEqual(try! promise.futureResult.wait(), """
3535
# TYPE my_counter counter
3636
my_counter 10
37-
my_counter{myValue=\"labels\"} 10
37+
my_counter{myValue=\"labels\"} 10\n
3838
""")
3939
}
4040

@@ -65,7 +65,44 @@ final class PrometheusMetricsTests: XCTestCase {
6565
""")
6666
}
6767

68-
func testCollectAFewMetricsIntoBuffer() {
68+
func testEmptyCollectIsConsistent() throws {
69+
let promise = self.eventLoop.makePromise(of: ByteBuffer.self)
70+
prom.collect(promise.succeed)
71+
var buffer = try promise.futureResult.wait()
72+
73+
let stringPromise = self.eventLoop.makePromise(of: String.self)
74+
prom.collect(stringPromise.succeed)
75+
let collectedToString = try stringPromise.futureResult.wait()
76+
77+
let collectedToBuffer = buffer.readString(length: buffer.readableBytes)
78+
XCTAssertEqual(collectedToBuffer, "")
79+
XCTAssertEqual(collectedToBuffer, collectedToString)
80+
}
81+
82+
func testCollectIsConsistent() throws {
83+
let counter = Counter(label: "my_counter")
84+
counter.increment(by: 10)
85+
let counterTwo = Counter(label: "my_counter", dimensions: [("myValue", "labels")])
86+
counterTwo.increment(by: 10)
87+
88+
let promise = self.eventLoop.makePromise(of: ByteBuffer.self)
89+
prom.collect(promise.succeed)
90+
var buffer = try promise.futureResult.wait()
91+
92+
let stringPromise = self.eventLoop.makePromise(of: String.self)
93+
prom.collect(stringPromise.succeed)
94+
let collectedToString = try stringPromise.futureResult.wait()
95+
96+
let collectedToBuffer = buffer.readString(length: buffer.readableBytes)
97+
XCTAssertEqual(collectedToBuffer, """
98+
# TYPE my_counter counter
99+
my_counter 10
100+
my_counter{myValue=\"labels\"} 10\n
101+
""")
102+
XCTAssertEqual(collectedToBuffer, collectedToString)
103+
}
104+
105+
func testCollectAFewMetricsIntoBuffer() throws {
69106
let counter = Counter(label: "my_counter")
70107
counter.increment(by: 10)
71108
let counterA = Counter(label: "my_counter", dimensions: [("a", "aaa"), ("x", "x")])
@@ -75,7 +112,7 @@ final class PrometheusMetricsTests: XCTestCase {
75112

76113
let promise = self.eventLoop.makePromise(of: ByteBuffer.self)
77114
prom.collect(promise.succeed)
78-
var buffer = try! promise.futureResult.wait()
115+
var buffer = try promise.futureResult.wait()
79116

80117
XCTAssertEqual(buffer.readString(length: buffer.readableBytes),
81118
"""
@@ -105,8 +142,7 @@ final class PrometheusMetricsTests: XCTestCase {
105142
my_counter 10
106143
my_counter{x="x", a="aaa"} 4
107144
# TYPE my_gauge gauge
108-
my_gauge 100.0
145+
my_gauge 100.0\n
109146
""")
110147
}
111148
}
112-

Tests/SwiftPrometheusTests/SanitizerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class SanitizerTests: XCTestCase {
4646
prom.collect(into: promise)
4747
XCTAssertEqual(try! promise.futureResult.wait(), """
4848
# TYPE test_counter counter
49-
test_counter 10
49+
test_counter 10\n
5050
""")
5151
}
5252
}

Tests/SwiftPrometheusTests/SummaryTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ final class SummaryTests: XCTestCase {
6767
my_summary{quantile="0.99", myValue="labels"} 123.0
6868
my_summary{quantile="0.999", myValue="labels"} 123.0
6969
my_summary_count{myValue="labels"} 1
70-
my_summary_sum{myValue="labels"} 123.0
70+
my_summary_sum{myValue="labels"} 123.0\n
7171
""")
7272
}
7373

@@ -93,7 +93,7 @@ final class SummaryTests: XCTestCase {
9393
my_summary{quantile="0.99"} 10000.0
9494
my_summary{quantile="0.999"} 10000.0
9595
my_summary_count 5
96-
my_summary_sum 10010.0
96+
my_summary_sum 10010.0\n
9797
""")
9898
}
9999

Tests/SwiftPrometheusTests/SwiftPrometheusTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ final class SwiftPrometheusTests: XCTestCase {
4949

5050
XCTAssertEqual(counter.collect(), "# HELP my_counter Counter for testing\n# TYPE my_counter counter\nmy_counter 30\nmy_counter{myValue=\"labels\"} 30")
5151
self.prom.collect { metricsString in
52-
XCTAssertEqual(metricsString, "# HELP my_counter Counter for testing\n# TYPE my_counter counter\nmy_counter 30\nmy_counter{myValue=\"labels\"} 30")
52+
XCTAssertEqual(metricsString, "# HELP my_counter Counter for testing\n# TYPE my_counter counter\nmy_counter 30\nmy_counter{myValue=\"labels\"} 30\n")
5353
}
5454
}
5555
}

0 commit comments

Comments
 (0)