Skip to content

Commit 855358a

Browse files
committed
Docs & tests
Signed-off-by: MrLotU <[email protected]>
1 parent f71033e commit 855358a

File tree

97 files changed

+23255
-48
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+23255
-48
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/.build
33
/Packages
44
/*.xcodeproj
5+
build/

.jazzy.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
author: 'MrLotU'
2+
author_url: 'https://github.com/MrLotU'
3+
github_url: 'https://github.com/MrLotU/SwiftPrometheus'
4+
github_file_prefix: 'https://github.com/MrLotU/SwiftPrometheus/tree/master/'
5+
theme: 'fullwidth'
6+
undocumented_text: ''

Sources/Prometheus/MetricTypes/Counter.swift

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
1+
/// Prometheus Counter metric
2+
///
3+
/// See https://prometheus.io/docs/concepts/metric_types/#counter
14
public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHandled {
5+
/// Prometheus instance that created this Counter
26
internal let prometheus: Prometheus
37

8+
/// Name of the Counter, required
49
public let name: String
10+
/// Help text of the Counter, optional
511
public let help: String?
612

13+
/// Type of the metric, used for formatting
714
public let _type: MetricType = .counter
815

16+
/// Current value of the counter
917
internal var value: NumType
1018

19+
/// Initial value of the counter
1120
private var initialValue: NumType
1221

22+
/// Storage of values that have labels attached
1323
internal var metrics: [Labels: NumType] = [:]
1424

25+
/// Creates a new instance of a Counter
26+
///
27+
/// - Parameters:
28+
/// - name: Name of the Counter
29+
/// - help: Helpt text of the Counter
30+
/// - initialValue: Initial value to set the counter to
31+
/// - p: Prometheus instance that created this counter
1532
internal init(_ name: String, _ help: String? = nil, _ initialValue: NumType = 0, _ p: Prometheus) {
1633
self.name = name
1734
self.help = help
@@ -20,14 +37,16 @@ public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, Prometheus
2037
self.prometheus = p
2138
}
2239

40+
/// Gets the metric string for this counter
41+
///
42+
/// - Returns:
43+
/// Newline seperated Prometheus formatted metric string
2344
public func getMetric() -> String {
2445
var output = [String]()
2546

2647
output.append(headers)
2748

28-
if value != initialValue && initialValue == 0 {
29-
output.append("\(name) \(value)")
30-
}
49+
output.append("\(name) \(value)")
3150

3251
metrics.forEach { (labels, value) in
3352
let labelsString = encodeLabels(labels)
@@ -37,6 +56,13 @@ public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, Prometheus
3756
return output.joined(separator: "\n")
3857
}
3958

59+
/// Increments the Counter
60+
///
61+
/// - Parameters:
62+
/// - amount: Amount to increment the counter with
63+
/// - labels: Labels to attach to the value
64+
///
65+
/// - Returns: The new value
4066
@discardableResult
4167
public func inc(_ amount: NumType = 1, _ labels: Labels? = nil) -> NumType {
4268
if let labels = labels {
@@ -50,6 +76,12 @@ public class Counter<NumType: Numeric, Labels: MetricLabels>: Metric, Prometheus
5076
}
5177
}
5278

79+
/// Gets the value of the Counter
80+
///
81+
/// - Parameters:
82+
/// - labels: Labels to get the value for
83+
///
84+
/// - Returns: The value of the Counter attached to the provided labels
5385
public func get(_ labels: Labels? = nil) -> NumType {
5486
if let labels = labels {
5587
return self.metrics[labels] ?? initialValue

Sources/Prometheus/MetricTypes/Gauge.swift

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
1+
/// Prometheus Counter metric
2+
///
3+
/// See https://prometheus.io/docs/concepts/metric_types/#gauge
14
public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHandled {
5+
/// Prometheus instance that created this Gauge
26
internal let prometheus: Prometheus
37

8+
/// Name of the Gauge, required
49
public let name: String
10+
/// Help text of the Gauge, optional
511
public let help: String?
612

13+
/// Type of the metric, used for formatting
714
public let _type: MetricType = .gauge
815

16+
/// Current value of the counter
917
private var value: NumType
1018

19+
/// Initial value of the Gauge
1120
private var initialValue: NumType
1221

22+
/// Storage of values that have labels attached
1323
private var metrics: [Labels: NumType] = [:]
1424

25+
/// Creates a new instance of a Gauge
26+
///
27+
/// - Parameters:
28+
/// - name: Name of the Gauge
29+
/// - help: Helpt text of the Gauge
30+
/// - initialValue: Initial value to set the Gauge to
31+
/// - p: Prometheus instance that created this Gauge
1532
internal init(_ name: String, _ help: String? = nil, _ initialValue: NumType = 0, _ p: Prometheus) {
1633
self.name = name
1734
self.help = help
@@ -20,14 +37,16 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
2037
self.prometheus = p
2138
}
2239

40+
/// Gets the metric string for this Gauge
41+
///
42+
/// - Returns:
43+
/// Newline seperated Prometheus formatted metric string
2344
public func getMetric() -> String {
2445
var output = [String]()
2546

2647
output.append(headers)
2748

28-
if value != initialValue && initialValue == 0 {
29-
output.append("\(name) \(value)")
30-
}
49+
output.append("\(name) \(value)")
3150

3251
metrics.forEach { (labels, value) in
3352
let labelsString = encodeLabels(labels)
@@ -37,6 +56,13 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
3756
return output.joined(separator: "\n")
3857
}
3958

59+
/// Sets the Gauge
60+
///
61+
/// - Parameters:
62+
/// - amount: Amount to set the gauge to
63+
/// - labels: Labels to attach to the value
64+
///
65+
/// - Returns: The new value
4066
@discardableResult
4167
public func set(_ amount: NumType, _ labels: Labels? = nil) -> NumType {
4268
if let labels = labels {
@@ -48,6 +74,13 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
4874
}
4975
}
5076

77+
/// Increments the Gauge
78+
///
79+
/// - Parameters:
80+
/// - amount: Amount to increment the Gauge with
81+
/// - labels: Labels to attach to the value
82+
///
83+
/// - Returns: The new value
5184
@discardableResult
5285
public func inc(_ amount: NumType, _ labels: Labels? = nil) -> NumType {
5386
if let labels = labels {
@@ -61,11 +94,24 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
6194
}
6295
}
6396

97+
/// Increments the Gauge
98+
///
99+
/// - Parameters:
100+
/// - labels: Labels to attach to the value
101+
///
102+
/// - Returns: The new value
64103
@discardableResult
65104
public func inc(_ labels: Labels? = nil) -> NumType {
66105
return self.inc(1, labels)
67106
}
68107

108+
/// Decrements the Gauge
109+
///
110+
/// - Parameters:
111+
/// - amount: Amount to decrement the Gauge with
112+
/// - labels: Labels to attach to the value
113+
///
114+
/// - Returns: The new value
69115
@discardableResult
70116
public func dec(_ amount: NumType, _ labels: Labels? = nil) -> NumType {
71117
if let labels = labels {
@@ -79,11 +125,23 @@ public class Gauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHa
79125
}
80126
}
81127

128+
/// Decrements the Gauge
129+
///
130+
/// - Parameters:
131+
/// - labels: Labels to attach to the value
132+
///
133+
/// - Returns: The new value
82134
@discardableResult
83135
public func dec(_ labels: Labels? = nil) -> NumType {
84136
return self.dec(1, labels)
85137
}
86138

139+
/// Gets the value of the Gauge
140+
///
141+
/// - Parameters:
142+
/// - labels: Labels to get the value for
143+
///
144+
/// - Returns: The value of the Gauge attached to the provided labels
87145
public func get(_ labels: Labels? = nil) -> NumType {
88146
if let labels = labels {
89147
return self.metrics[labels] ?? initialValue

Sources/Prometheus/MetricTypes/Histogram.swift

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,57 @@
1+
/// Default buckets used by Histograms
12
public var defaultBuckets = [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10.0, Double.greatestFiniteMagnitude]
23

4+
/// Label type Histograms can use
35
public protocol HistogramLabels: MetricLabels {
46
var le: String { get set }
57
}
68

79
extension HistogramLabels {
10+
/// Creates empty HistogramLabels
811
init() {
912
self.init()
1013
self.le = ""
1114
}
1215
}
1316

17+
/// Prometheus Counter metric
18+
///
19+
/// See https://prometheus.io/docs/concepts/metric_types/#Histogram
1420
public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: Metric, PrometheusHandled {
21+
/// Prometheus instance that created this Histogram
1522
internal let prometheus: Prometheus
1623

24+
/// Name of this Histogram, required
1725
public let name: String
26+
/// Help text of this Histogram, optional
1827
public let help: String?
1928

29+
/// Type of the metric, used for formatting
2030
public let _type: MetricType = .histogram
2131

22-
private var buckets: [Counter<NumType, EmptyCodable>] = []
32+
/// Bucketed values for this Histogram
33+
private var buckets: [Counter<NumType, EmptyLabels>] = []
2334

35+
/// Buckets used by this Histogram
2436
internal let upperBounds: [Double]
2537

38+
/// Labels for this Histogram
2639
internal private(set) var labels: Labels
2740

41+
/// Sub Histograms for this Histogram
2842
fileprivate var subHistograms: [Histogram<NumType, Labels>] = []
2943

30-
private let total: Counter<NumType, EmptyCodable>
44+
/// Total value of the Histogram
45+
private let total: Counter<NumType, EmptyLabels>
3146

47+
/// Creates a new Histogram
48+
///
49+
/// - Parameters:
50+
/// - name: Name of the Histogram
51+
/// - help: Help text of the Histogram
52+
/// - labels: Labels for the Histogram
53+
/// - buckets: Buckets to use for the Histogram
54+
/// - p: Prometheus instance creating this Histogram
3255
internal init(_ name: String, _ help: String? = nil, _ labels: Labels = Labels(), _ buckets: [Double] = defaultBuckets, _ p: Prometheus) {
3356
self.name = name
3457
self.help = help
@@ -46,10 +69,13 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
4669
}
4770
}
4871

72+
/// Gets the metric string for this Histogram
73+
///
74+
/// - Returns:
75+
/// Newline seperated Prometheus formatted metric string
4976
public func getMetric() -> String {
5077
var output = [String]()
5178

52-
5379
output.append(headers)
5480

5581
var acc: NumType = 0
@@ -87,8 +113,13 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
87113
return output.joined(separator: "\n")
88114
}
89115

116+
/// Observe a value
117+
///
118+
/// - Parameters:
119+
/// - value: Value to observe
120+
/// - labels: Labels to attach to the observed value
90121
public func observe(_ value: NumType, _ labels: Labels? = nil) {
91-
if let labels = labels, type(of: labels) != type(of: EmptySummaryCodable()) {
122+
if let labels = labels, type(of: labels) != type(of: EmptySummaryLabels()) {
92123
let his = prometheus.getOrCreateHistogram(with: labels, for: self)
93124
his.observe(value)
94125
}
@@ -109,7 +140,7 @@ extension Prometheus {
109140
guard metric.name == his.name, metric.help == his.help, metric.labels == labels else { return false }
110141
return true
111142
}
112-
if histograms.count > 2 { fatalError("Somehow got 2 summaries with the same data type") }
143+
if histograms.count > 2 { fatalError("Somehow got 2 histograms with the same data type") }
113144
if let histogram = histograms.first {
114145
return histogram
115146
} else {

Sources/Prometheus/MetricTypes/Info.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
1+
/// Info metric
2+
///
3+
/// Info tracks key-value information, usually about a whole target
14
public class Info<Labels: MetricLabels>: Metric, PrometheusHandled {
5+
/// Prometheus instance that created this Info
26
internal let prometheus: Prometheus
37

8+
/// Name of the Info, required
49
public let name: String
10+
/// Help text of the Info, optional
511
public let help: String?
612

13+
/// Type of the metric, used for formatting
714
public let _type: MetricType = .info
815

16+
/// Labels of the info
17+
/// For Info metrics, these are the actual values the metric is exposing
918
internal var labels = Labels()
1019

20+
/// Creates a new Info
21+
///
22+
/// - Parameters:
23+
/// - name: Name of the Info
24+
/// - help: Help text of the Info
25+
/// - p: Prometheus instance handling this Info
1126
internal init(_ name: String, _ help: String? = nil, _ p: Prometheus) {
1227
self.name = name
1328
self.help = help
1429
self.prometheus = p
1530
}
1631

32+
/// Set the info
33+
///
34+
/// - Parameters:
35+
/// - labels: Labels to set the Info to
1736
public func info(_ labels: Labels) {
1837
self.labels = labels
1938
}
2039

40+
/// Gets the metric string for this Info
41+
///
42+
/// - Returns:
43+
/// Newline seperated Prometheus formatted metric string
2144
public func getMetric() -> String {
2245
var output = [String]()
2346

0 commit comments

Comments
 (0)