Skip to content

Commit 984e26d

Browse files
committed
Address comments
1 parent 700ff7a commit 984e26d

File tree

12 files changed

+102
-85
lines changed

12 files changed

+102
-85
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let package = Package(
1111
],
1212
dependencies: [
1313
.package(url: "https://github.com/apple/swift-metrics.git", from: "1.0.0"),
14-
.package(url: "https://github.com/apple/swift-nio.git", from: "1.0.0"),
14+
.package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"),
1515
],
1616
targets: [
1717
.target(

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,6 @@ Here, I used [Vapor](https://github.com/vapor/vapor) syntax, but this will work
126126

127127
All contributions are most welcome!
128128

129-
-If you think of some cool new feature that should be included, please [create an issue](https://github.com/MrLotU/SwiftPrometheus/issues/new/choose). Or, if you want to implement it yourself, [fork this repo](https://github.com/MrLotU/SwiftPrometheus/fork) and submit a PR!
129+
If you think of some cool new feature that should be included, please [create an issue](https://github.com/MrLotU/SwiftPrometheus/issues/new/choose). Or, if you want to implement it yourself, [fork this repo](https://github.com/MrLotU/SwiftPrometheus/fork) and submit a PR!
130130

131131
If you find a bug or have issues, please [create an issue](https://github.com/MrLotU/SwiftPrometheus/issues/new/choose) explaining your problems. Please include as much information as possible, so it's easier for me to reproduce (Framework, OS, Swift version, terminal output, etc.)

Sources/Prometheus/MetricTypes/Counter.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import NIOConcurrencyHelpers
22

33
/// Prometheus Counter metric
44
///
5-
/// See https://prometheus.io/docs/concepts/metric_types/#counter
6-
public class PromCounter<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHandled {
5+
/// See: https://prometheus.io/docs/concepts/metric_types/#counter
6+
public class PromCounter<NumType: Numeric, Labels: MetricLabels>: PromMetric, PrometheusHandled {
77
/// Prometheus instance that created this Counter
88
internal weak var prometheus: PrometheusClient?
99

@@ -13,7 +13,7 @@ public class PromCounter<NumType: Numeric, Labels: MetricLabels>: Metric, Promet
1313
public let help: String?
1414

1515
/// Type of the metric, used for formatting
16-
public let _type: MetricType = .counter
16+
public let _type: PromMetricType = .counter
1717

1818
/// Current value of the counter
1919
internal var value: NumType

Sources/Prometheus/MetricTypes/Gauge.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import NIOConcurrencyHelpers
33
/// Prometheus Gauge metric
44
///
55
/// See https://prometheus.io/docs/concepts/metric_types/#gauge
6-
public class PromGauge<NumType: Numeric, Labels: MetricLabels>: Metric, PrometheusHandled {
6+
public class PromGauge<NumType: Numeric, Labels: MetricLabels>: PromMetric, PrometheusHandled {
77
/// Prometheus instance that created this Gauge
88
internal weak var prometheus: PrometheusClient?
99

@@ -13,7 +13,7 @@ public class PromGauge<NumType: Numeric, Labels: MetricLabels>: Metric, Promethe
1313
public let help: String?
1414

1515
/// Type of the metric, used for formatting
16-
public let _type: MetricType = .gauge
16+
public let _type: PromMetricType = .gauge
1717

1818
/// Current value of the counter
1919
private var value: NumType

Sources/Prometheus/MetricTypes/Histogram.swift

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import NIOConcurrencyHelpers
22

3-
/// Default buckets used by Histograms
4-
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]
5-
63
/// Label type Histograms can use
74
public protocol HistogramLabels: MetricLabels {
85
/// Bucket
@@ -20,7 +17,7 @@ extension HistogramLabels {
2017
/// Prometheus Histogram metric
2118
///
2219
/// See https://prometheus.io/docs/concepts/metric_types/#Histogram
23-
public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: Metric, PrometheusHandled {
20+
public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: PromMetric, PrometheusHandled {
2421
/// Prometheus instance that created this Histogram
2522
internal weak var prometheus: PrometheusClient?
2623

@@ -30,7 +27,7 @@ public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels
3027
public let help: String?
3128

3229
/// Type of the metric, used for formatting
33-
public let _type: MetricType = .histogram
30+
public let _type: PromMetricType = .histogram
3431

3532
/// Bucketed values for this Histogram
3633
private var buckets: [PromCounter<NumType, EmptyLabels>] = []
@@ -45,7 +42,7 @@ public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels
4542
fileprivate var subHistograms: [PromHistogram<NumType, Labels>] = []
4643

4744
/// Total value of the Histogram
48-
private let total: PromCounter<NumType, EmptyLabels>
45+
private let sum: PromCounter<NumType, EmptyLabels>
4946

5047
/// Lock used for thread safety
5148
private let lock: Lock
@@ -58,13 +55,13 @@ public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels
5855
/// - labels: Labels for the Histogram
5956
/// - buckets: Buckets to use for the Histogram
6057
/// - p: Prometheus instance creating this Histogram
61-
internal init(_ name: String, _ help: String? = nil, _ labels: Labels = Labels(), _ buckets: [Double] = defaultBuckets, _ p: PrometheusClient) {
58+
internal init(_ name: String, _ help: String? = nil, _ labels: Labels = Labels(), _ buckets: [Double] = Prometheus.defaultBuckets, _ p: PrometheusClient) {
6259
self.name = name
6360
self.help = help
6461

6562
self.prometheus = p
6663

67-
self.total = .init("\(self.name)_sum", nil, 0, p)
64+
self.sum = .init("\(self.name)_sum", nil, 0, p)
6865

6966
self.labels = labels
7067

@@ -101,7 +98,7 @@ public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels
10198
let labelsString = encodeLabels(self.labels, ["le"])
10299
output.append("\(self.name)_count\(labelsString) \(acc)")
103100

104-
output.append("\(self.name)_sum\(labelsString) \(self.total.get())")
101+
output.append("\(self.name)_sum\(labelsString) \(self.sum.get())")
105102

106103
self.subHistograms.forEach { subHistogram in
107104
var acc: NumType = 0
@@ -115,7 +112,7 @@ public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels
115112
let labelsString = encodeLabels(subHistogram.labels, ["le"])
116113
output.append("\(subHistogram.name)_count\(labelsString) \(acc)")
117114

118-
output.append("\(subHistogram.name)_sum\(labelsString) \(subHistogram.total.get())")
115+
output.append("\(subHistogram.name)_sum\(labelsString) \(subHistogram.sum.get())")
119116

120117
subHistogram.labels.le = ""
121118
}
@@ -137,7 +134,7 @@ public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels
137134
guard let his = self.prometheus?.getOrCreateHistogram(with: labels, for: self) else { fatalError("Lingering Histogram") }
138135
his.observe(value)
139136
}
140-
self.total.inc(value)
137+
self.sum.inc(value)
141138

142139
for (i, bound) in self.upperBounds.enumerated() {
143140
if bound >= value.doubleValue {
@@ -152,17 +149,17 @@ public class PromHistogram<NumType: DoubleRepresentable, Labels: HistogramLabels
152149

153150
extension PrometheusClient {
154151
/// Helper for histograms & labels
155-
fileprivate func getOrCreateHistogram<T: Numeric, U: HistogramLabels>(with labels: U, for his: PromHistogram<T, U>) -> PromHistogram<T, U> {
156-
let histograms = his.subHistograms.filter { (metric) -> Bool in
157-
guard metric.name == his.name, metric.help == his.help, metric.labels == labels else { return false }
152+
fileprivate func getOrCreateHistogram<T: Numeric, U: HistogramLabels>(with labels: U, for histogram: PromHistogram<T, U>) -> PromHistogram<T, U> {
153+
let histograms = histogram.subHistograms.filter { (metric) -> Bool in
154+
guard metric.name == histogram.name, metric.help == histogram.help, metric.labels == labels else { return false }
158155
return true
159156
}
160157
if histograms.count > 2 { fatalError("Somehow got 2 histograms with the same data type") }
161158
if let histogram = histograms.first {
162159
return histogram
163160
} else {
164-
let histogram = PromHistogram<T, U>(his.name, his.help, labels, his.upperBounds, self)
165-
his.subHistograms.append(histogram)
161+
let histogram = PromHistogram<T, U>(histogram.name, histogram.help, labels, histogram.upperBounds, self)
162+
histogram.subHistograms.append(histogram)
166163
return histogram
167164
}
168165
}

Sources/Prometheus/MetricTypes/Metric.swift

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/// Different types of metrics supported by SwiftPrometheus
2+
public enum PromMetricType: String {
3+
/// See `PromCounter`
4+
case counter
5+
/// See `PromGauge`
6+
case gauge
7+
/// See `PromHistogram`
8+
case histogram
9+
/// See `PromSummary`
10+
case summary
11+
}
12+
13+
public enum Prometheus {
14+
/// Default buckets used by Histograms
15+
public static let 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]
16+
17+
/// Default quantiles used by Summaries
18+
public static let defaultQuantiles = [0.01, 0.05, 0.5, 0.9, 0.95, 0.99, 0.999]
19+
20+
}
21+
22+
/// Metric protocol
23+
///
24+
/// See https://prometheus.io/docs/concepts/metric_types/
25+
public protocol PromMetric {
26+
/// Name of the metric
27+
var name: String { get }
28+
/// Optional help of the metric
29+
var help: String? { get }
30+
/// Type of the metric
31+
var _type: PromMetricType { get }
32+
33+
/// Retrieves the Prometheus-formatted metric data
34+
func getMetric() -> String
35+
}
36+
37+
/// Adding a prometheus instance to all metrics
38+
internal protocol PrometheusHandled {
39+
/// Promtheus client handling this metric
40+
var prometheus: PrometheusClient? { get }
41+
}
42+
43+
/// Base MetricLabels protocol
44+
///
45+
/// MetricLabels are used to enrich & specify metrics.
46+
///
47+
/// struct Labels: MetricLabels {
48+
/// let status: String = "unknown"
49+
/// }
50+
/// let counter = myProm.createCounter(...)
51+
/// counter.inc(12, labels: Labels(status: "failure")
52+
/// counter.inc(1, labels: Labels(status: "success")
53+
/// Will result in the following Prometheus output:
54+
///
55+
/// # TYPE my_counter counter
56+
/// my_counter 0
57+
/// my_counter{status="unknown"} 0
58+
/// my_counter{status="failure"} 12
59+
/// my_counter{status="success"} 1
60+
public protocol MetricLabels: Encodable, Hashable {
61+
/// Create empty labels
62+
init()
63+
}

Sources/Prometheus/MetricTypes/Summary.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import NIOConcurrencyHelpers
22

3-
/// Default quantiles used by Summaries
4-
public var defaultQuantiles = [0.01, 0.05, 0.5, 0.9, 0.95, 0.99, 0.999]
5-
63
/// Label type Summaries can use
74
public protocol SummaryLabels: MetricLabels {
8-
/// Quantile
5+
/// Quantile used to label the summary.
96
var quantile: String { get set }
107
}
118

@@ -20,7 +17,7 @@ extension SummaryLabels {
2017
/// Prometheus Counter metric
2118
///
2219
/// See https://prometheus.io/docs/concepts/metric_types/#summary
23-
public class PromSummary<NumType: DoubleRepresentable, Labels: SummaryLabels>: Metric, PrometheusHandled {
20+
public class PromSummary<NumType: DoubleRepresentable, Labels: SummaryLabels>: PromMetric, PrometheusHandled {
2421
/// Prometheus instance that created this Summary
2522
internal weak var prometheus: PrometheusClient?
2623

@@ -30,7 +27,7 @@ public class PromSummary<NumType: DoubleRepresentable, Labels: SummaryLabels>: M
3027
public let help: String?
3128

3229
/// Type of the metric, used for formatting
33-
public let _type: MetricType = .summary
30+
public let _type: PromMetricType = .summary
3431

3532
/// Labels for this Summary
3633
internal private(set) var labels: Labels
@@ -61,7 +58,7 @@ public class PromSummary<NumType: DoubleRepresentable, Labels: SummaryLabels>: M
6158
/// - labels: Labels for the Summary
6259
/// - quantiles: Quantiles to use for the Summary
6360
/// - p: Prometheus instance creating this Summary
64-
internal init(_ name: String, _ help: String? = nil, _ labels: Labels = Labels(), _ quantiles: [Double] = defaultQuantiles, _ p: PrometheusClient) {
61+
internal init(_ name: String, _ help: String? = nil, _ labels: Labels = Labels(), _ quantiles: [Double] = Prometheus.defaultQuantiles, _ p: PrometheusClient) {
6562
self.name = name
6663
self.help = help
6764

Sources/Prometheus/Prometheus.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import NIOConcurrencyHelpers
66
public class PrometheusClient {
77

88
/// Metrics tracked by this Prometheus instance
9-
private var metrics: [Metric]
9+
private var metrics: [PromMetric]
1010

1111
/// To keep track of the type of a metric since it can not change
1212
/// througout the lifetime of the program
13-
private var metricTypeMap: [String: MetricType]
13+
private var metricTypeMap: [String: PromMetricType]
1414

1515
/// Lock used for thread safety
1616
private let lock: Lock
@@ -33,15 +33,15 @@ public class PrometheusClient {
3333

3434
// MARK: - Metric Access
3535

36-
public func removeMetric(_ metric: Metric) {
36+
public func removeMetric(_ metric: PromMetric) {
3737
// `metricTypeMap` is left untouched as those must be consistent
3838
// throughout the lifetime of a program.
3939
return lock.withLock {
4040
self.metrics.removeAll { $0._type == metric._type && $0.name == metric.name }
4141
}
4242
}
4343

44-
public func getMetricInstance<T>(with name: String, andType type: MetricType) -> T? where T: Metric {
44+
public func getMetricInstance<T>(with name: String, andType type: PromMetricType) -> T? where T: PromMetric {
4545
return lock.withLock {
4646
self.metrics.compactMap { $0 as? T }.filter { $0.name == name && $0._type == type }.first
4747
}
@@ -159,7 +159,7 @@ public class PrometheusClient {
159159
forType type: T.Type,
160160
named name: String,
161161
helpText: String? = nil,
162-
buckets: [Double] = defaultBuckets,
162+
buckets: [Double] = Prometheus.defaultBuckets,
163163
labels: U.Type) -> PromHistogram<T, U>
164164
{
165165
return self.lock.withLock {
@@ -186,7 +186,7 @@ public class PrometheusClient {
186186
forType type: T.Type,
187187
named name: String,
188188
helpText: String? = nil,
189-
buckets: [Double] = defaultBuckets) -> PromHistogram<T, EmptyHistogramLabels>
189+
buckets: [Double] = Prometheus.defaultBuckets) -> PromHistogram<T, EmptyHistogramLabels>
190190
{
191191
return self.createHistogram(forType: type, named: name, helpText: helpText, buckets: buckets, labels: EmptyHistogramLabels.self)
192192
}
@@ -207,7 +207,7 @@ public class PrometheusClient {
207207
forType type: T.Type,
208208
named name: String,
209209
helpText: String? = nil,
210-
quantiles: [Double] = defaultQuantiles,
210+
quantiles: [Double] = Prometheus.defaultQuantiles,
211211
labels: U.Type) -> PromSummary<T, U>
212212
{
213213
return self.lock.withLock {
@@ -234,7 +234,7 @@ public class PrometheusClient {
234234
forType type: T.Type,
235235
named name: String,
236236
helpText: String? = nil,
237-
quantiles: [Double] = defaultQuantiles) -> PromSummary<T, EmptySummaryLabels>
237+
quantiles: [Double] = Prometheus.defaultQuantiles) -> PromSummary<T, EmptySummaryLabels>
238238
{
239239
return self.createSummary(forType: type, named: name, helpText: helpText, quantiles: quantiles, labels: EmptySummaryLabels.self)
240240
}
@@ -245,6 +245,6 @@ public enum PrometheusError: Error {
245245
/// Thrown when a user tries to retrive
246246
/// a `PromtheusClient` from `MetricsSystem`
247247
/// but there was no `PrometheusClient` bootstrapped
248-
case PrometheusFactoryNotBootstrapped
248+
case prometheusFactoryNotBootstrapped(bootstrappedWith: String)
249249
}
250250

Sources/PrometheusExample/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Prometheus
2+
import Metrics
13
import PrometheusMetrics
24
import Foundation
35

0 commit comments

Comments
 (0)