Skip to content

Commit dd7db34

Browse files
committed
Cleanup, fix some small mistakes
1 parent 317b719 commit dd7db34

File tree

5 files changed

+32
-20
lines changed

5 files changed

+32
-20
lines changed

Sources/Prometheus/MetricTypes/Counter.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, Prometheus
44
public let name: String
55
public let help: String?
66

7+
public var _type: MetricType = .counter
8+
79
internal var value: NumType
810

911
private var initialValue: NumType
@@ -21,11 +23,8 @@ public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, Prometheus
2123
public func getMetric() -> String {
2224
var output = [String]()
2325

24-
if let help = help {
25-
output.append("# HELP \(name) \(help)")
26-
}
26+
output.append(headers)
2727

28-
output.append("# TYPE \(name) counter")
2928
output.append("\(name) \(value)")
3029

3130
metrics.forEach { (labels, value) in

Sources/Prometheus/MetricTypes/Gauge.swift

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

7+
public var _type: MetricType = .gauge
8+
79
private var value: NumType
810

911
private var initialValue: NumType
@@ -21,11 +23,8 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
2123
public func getMetric() -> String {
2224
var output = [String]()
2325

24-
if let help = help {
25-
output.append("# HELP \(name) \(help)")
26-
}
27-
28-
output.append("# TYPE \(name) gauge")
26+
output.append(headers)
27+
2928
output.append("\(name) \(value)")
3029

3130
metrics.forEach { (labels, value) in

Sources/Prometheus/MetricTypes/Histogram.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
1717
public let name: String
1818
public let help: String?
1919

20+
public var _type: MetricType = .histogram
21+
2022
private var buckets: [Counter<NumType, EmptyCodable>] = []
2123

2224
internal let upperBounds: [Double]
@@ -48,10 +50,7 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
4850
var output = [String]()
4951

5052

51-
if let help = help {
52-
output.append("# HELP \(name) \(help)")
53-
}
54-
output.append("# TYPE \(name) histogram")
53+
output.append(headers)
5554

5655
var acc: NumType = 0
5756
for (i, bound) in self.upperBounds.enumerated() {
@@ -85,7 +84,7 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
8584
}
8685

8786
public func observe(_ value: NumType, _ labels: Labels? = nil) {
88-
if let labels = labels {
87+
if let labels = labels, type(of: labels) != type(of: EmptySummaryCodable()) {
8988
let his = prometheus.getOrCreateHistogram(with: labels, for: self)
9089
his.observe(value)
9190
return

Sources/Prometheus/MetricTypes/Metric.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1+
public enum MetricType: String {
2+
case counter, gauge, histogram, summary, info, `enum`
3+
}
4+
15
public protocol Metric {
26
var name: String { get }
37
var help: String? { get }
8+
var _type: MetricType { get }
49

510
func getMetric() -> String
611
}
712

13+
extension Metric {
14+
var headers: String {
15+
var output = [String]()
16+
if let help = help {
17+
output.append("# HELP \(name) \(help)")
18+
}
19+
output.append("# TYPE \(name) \(_type)")
20+
return output.joined(separator: "\n")
21+
}
22+
}
23+
824
internal protocol PrometheusHandled {
925
var prometheus: Prometheus { get }
1026
}

Sources/Prometheus/MetricTypes/Summary.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class Summary<NumType: DoubleRepresentable, Labels: SummaryLabels>: Metri
1616

1717
public let name: String
1818
public let help: String?
19+
20+
public var _type: MetricType = .summary
1921

2022
internal private(set) var labels: Labels
2123

@@ -46,12 +48,9 @@ public class Summary<NumType: DoubleRepresentable, Labels: SummaryLabels>: Metri
4648

4749
public func getMetric() -> String {
4850
var output = [String]()
49-
50-
if let help = help {
51-
output.append("# HELP \(name) \(help)")
52-
}
53-
output.append("# TYPE \(name) summary")
54-
51+
52+
output.append(headers)
53+
5554
calculateQuantiles(quantiles: quantiles, values: values.map { $0.doubleValue }).sorted { $0.key < $1.key }.forEach { (arg) in
5655
let (q, v) = arg
5756
self.labels.quantile = "\(q)"

0 commit comments

Comments
 (0)