Skip to content

Commit b6e38ea

Browse files
YasumotoMrLotU
authored andcommitted
Do not recreate a new counter if we already have one. (#13)
* Do not recreate a new counter if we already have one. * Apply across the board * Newline for CI * Newline for CI
1 parent ece301f commit b6e38ea

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

Sources/Prometheus/MetricTypes/Summary.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extension SummaryLabels {
1414
}
1515
}
1616

17-
/// Prometheus Counter metric
17+
/// Prometheus Summary metric
1818
///
1919
/// See https://prometheus.io/docs/concepts/metric_types/#summary
2020
public class PromSummary<NumType: DoubleRepresentable, Labels: SummaryLabels>: PromMetric, PrometheusHandled {

Sources/Prometheus/Prometheus.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import NIO
55
///
66
/// See https://prometheus.io/docs/introduction/overview/
77
public class PrometheusClient {
8-
8+
99
/// Metrics tracked by this Prometheus instance
1010
private var metrics: [PromMetric]
1111

@@ -101,6 +101,10 @@ public class PrometheusClient {
101101
initialValue: T = 0,
102102
withLabelType labelType: U.Type) -> PromCounter<T, U>
103103
{
104+
if let counter: PromCounter<T, U> = getMetricInstance(with: name, andType: .counter) {
105+
return counter
106+
}
107+
104108
return self.lock.withLock {
105109
if let type = metricTypeMap[name] {
106110
precondition(type == .counter, "Label \(name) was associated with \(type) before. Can not be used for a counter now.")
@@ -149,6 +153,10 @@ public class PrometheusClient {
149153
initialValue: T = 0,
150154
withLabelType labelType: U.Type) -> PromGauge<T, U>
151155
{
156+
if let gauge: PromGauge<T, U> = getMetricInstance(with: name, andType: .gauge) {
157+
return gauge
158+
}
159+
152160
return self.lock.withLock {
153161
if let type = metricTypeMap[name] {
154162
precondition(type == .gauge, "Label \(name) was associated with \(type) before. Can not be used for a gauge now.")
@@ -197,6 +205,10 @@ public class PrometheusClient {
197205
buckets: [Double] = Prometheus.defaultBuckets,
198206
labels: U.Type) -> PromHistogram<T, U>
199207
{
208+
if let histogram: PromHistogram<T, U> = getMetricInstance(with: name, andType: .histogram) {
209+
return histogram
210+
}
211+
200212
return self.lock.withLock {
201213
if let type = metricTypeMap[name] {
202214
precondition(type == .histogram, "Label \(name) was associated with \(type) before. Can not be used for a histogram now.")
@@ -245,6 +257,10 @@ public class PrometheusClient {
245257
quantiles: [Double] = Prometheus.defaultQuantiles,
246258
labels: U.Type) -> PromSummary<T, U>
247259
{
260+
if let summary: PromSummary<T, U> = getMetricInstance(with: name, andType: .summary) {
261+
return summary
262+
}
263+
248264
return self.lock.withLock {
249265
if let type = metricTypeMap[name] {
250266
precondition(type == .summary, "Label \(name) was associated with \(type) before. Can not be used for a summary now.")

Tests/SwiftPrometheusTests/SwiftPrometheusTests.swift

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ final class SwiftPrometheusTests: XCTestCase {
6262

6363
XCTAssertEqual(counter.collect(), "# HELP my_counter Counter for testing\n# TYPE my_counter counter\nmy_counter 20\nmy_counter{myValue=\"labels\"} 20")
6464
}
65+
66+
func testMultipleCounter() {
67+
let counter = prom.createCounter(forType: Int.self, named: "my_counter", helpText: "Counter for testing", initialValue: 10, withLabelType: BaseLabels.self)
68+
counter.inc(10)
69+
XCTAssertEqual(counter.get(), 20)
70+
71+
let counterTwo = prom.createCounter(forType: Int.self, named: "my_counter", helpText: "Counter for testing", initialValue: 10, withLabelType: BaseLabels.self)
72+
counter.inc(10)
73+
XCTAssertEqual(counterTwo.get(), 30)
74+
counterTwo.inc(20, BaseLabels(myValue: "labels"))
75+
76+
XCTAssertEqual(counter.collect(), "# HELP my_counter Counter for testing\n# TYPE my_counter counter\nmy_counter 30\nmy_counter{myValue=\"labels\"} 30")
77+
self.prom.collect { metricsString in
78+
XCTAssertEqual(metricsString, "# HELP my_counter Counter for testing\n# TYPE my_counter counter\nmy_counter 30\nmy_counter{myValue=\"labels\"} 30")
79+
}
80+
}
6581

6682
func testGauge() {
6783
let gauge = prom.createGauge(forType: Int.self, named: "my_gauge", helpText: "Gauge for testing", initialValue: 10, withLabelType: BaseLabels.self)
@@ -74,28 +90,37 @@ final class SwiftPrometheusTests: XCTestCase {
7490
gauge.inc(10, BaseLabels(myValue: "labels"))
7591
XCTAssertEqual(gauge.get(), 20)
7692
XCTAssertEqual(gauge.get(BaseLabels(myValue: "labels")), 20)
93+
94+
let gaugeTwo = prom.createGauge(forType: Int.self, named: "my_gauge", helpText: "Gauge for testing", initialValue: 10, withLabelType: BaseLabels.self)
95+
XCTAssertEqual(gaugeTwo.get(), 20)
96+
gaugeTwo.inc()
97+
XCTAssertEqual(gauge.get(), 21)
98+
XCTAssertEqual(gaugeTwo.get(), 21)
7799

78-
XCTAssertEqual(gauge.collect(), "# HELP my_gauge Gauge for testing\n# TYPE my_gauge gauge\nmy_gauge 20\nmy_gauge{myValue=\"labels\"} 20")
100+
XCTAssertEqual(gauge.collect(), "# HELP my_gauge Gauge for testing\n# TYPE my_gauge gauge\nmy_gauge 21\nmy_gauge{myValue=\"labels\"} 20")
79101
}
80102

81103
func testHistogram() {
82104
let histogram = prom.createHistogram(forType: Double.self, named: "my_histogram", helpText: "Histogram for testing", buckets: [0.5, 1, 2, 3, 5, Double.greatestFiniteMagnitude], labels: BaseHistogramLabels.self)
105+
let histogramTwo = prom.createHistogram(forType: Double.self, named: "my_histogram", helpText: "Histogram for testing", buckets: [0.5, 1, 2, 3, 5, Double.greatestFiniteMagnitude], labels: BaseHistogramLabels.self)
106+
83107
histogram.observe(1)
84108
histogram.observe(2)
85-
histogram.observe(3)
109+
histogramTwo.observe(3)
86110

87111
histogram.observe(3, .init(myValue: "labels"))
88-
112+
89113
XCTAssertEqual(histogram.collect(), "# HELP my_histogram Histogram for testing\n# TYPE my_histogram histogram\nmy_histogram_bucket{myValue=\"*\", le=\"0.5\"} 0.0\nmy_histogram_bucket{myValue=\"*\", le=\"1.0\"} 1.0\nmy_histogram_bucket{myValue=\"*\", le=\"2.0\"} 2.0\nmy_histogram_bucket{myValue=\"*\", le=\"3.0\"} 4.0\nmy_histogram_bucket{myValue=\"*\", le=\"5.0\"} 4.0\nmy_histogram_bucket{myValue=\"*\", le=\"+Inf\"} 4.0\nmy_histogram_count{myValue=\"*\"} 4.0\nmy_histogram_sum{myValue=\"*\"} 9.0\nmy_histogram_bucket{myValue=\"labels\", le=\"0.5\"} 0.0\nmy_histogram_bucket{myValue=\"labels\", le=\"1.0\"} 0.0\nmy_histogram_bucket{myValue=\"labels\", le=\"2.0\"} 0.0\nmy_histogram_bucket{myValue=\"labels\", le=\"3.0\"} 1.0\nmy_histogram_bucket{myValue=\"labels\", le=\"5.0\"} 1.0\nmy_histogram_bucket{myValue=\"labels\", le=\"+Inf\"} 1.0\nmy_histogram_count{myValue=\"labels\"} 1.0\nmy_histogram_sum{myValue=\"labels\"} 3.0")
90114
}
91115

92116
func testSummary() {
93117
let summary = prom.createSummary(forType: Double.self, named: "my_summary", helpText: "Summary for testing", quantiles: [0.5, 0.9, 0.99], labels: BaseSummaryLabels.self)
118+
let summaryTwo = prom.createSummary(forType: Double.self, named: "my_summary", helpText: "Summary for testing", quantiles: [0.5, 0.9, 0.99], labels: BaseSummaryLabels.self)
94119

95120
summary.observe(1)
96121
summary.observe(2)
97122
summary.observe(4)
98-
summary.observe(10000)
123+
summaryTwo.observe(10000)
99124

100125
summary.observe(123, .init(myValue: "labels"))
101126

0 commit comments

Comments
 (0)