Skip to content

Commit 3b78712

Browse files
committed
Wrap summary
1 parent 83b6679 commit 3b78712

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

Sources/Prometheus/MetricTypes/Histogram.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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]
1+
private 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]
22

33
public protocol HistogramLabels: MetricLabels {
44
var le: String { get set }
@@ -24,7 +24,7 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
2424

2525
private var total: Counter<NumType, EmptyCodable>
2626

27-
public init(_ name: String, _ help: String? = nil, _ labels: Labels = Labels(), _ buckets: [Double] = defaultBuckets) {
27+
internal init(_ name: String, _ help: String? = nil, _ labels: Labels = Labels(), _ buckets: [Double] = defaultBuckets) {
2828
self.name = name
2929
self.help = help
3030

Sources/Prometheus/MetricTypes/Summary.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
public var defaultQuantiles = [0.01, 0.05, 0.5, 0.9, 0.95, 0.99, 0.999]
2+
13
public protocol SummaryLabels: MetricLabels {
24
var quantile: String { get set }
35
}
@@ -22,14 +24,18 @@ public class Summary<NumType: DoubleRepresentable, Labels: SummaryLabels>: Metri
2224

2325
private var values: [NumType] = []
2426

25-
public init(_ name: String, _ help: String? = nil, _ labels: Labels = Labels()) {
27+
private var quantiles: [Double]
28+
29+
public init(_ name: String, _ help: String? = nil, _ quantiles: [Double] = defaultQuantiles, _ labels: Labels = Labels()) {
2630
self.name = name
2731
self.help = help
2832

2933
self.sum = .init("\(self.name)_sum")
3034

3135
self.count = .init("\(self.name)_count")
3236

37+
self.quantiles = quantiles
38+
3339
self.labels = labels
3440
}
3541

@@ -41,9 +47,7 @@ public class Summary<NumType: DoubleRepresentable, Labels: SummaryLabels>: Metri
4147
}
4248
output.append("# TYPE \(name) summary")
4349

44-
let quantiles = [0.5, 0.9, 0.99]
45-
46-
calculateQuantiles(quantiles: quantiles, values: values.map { $0.doubleValue }).forEach { (arg) in
50+
calculateQuantiles(quantiles: quantiles, values: values.map { $0.doubleValue }).sorted { $0.key < $1.key }.forEach { (arg) in
4751
let (q, v) = arg
4852
self.labels.quantile = "\(q)"
4953
let labelsString = encodeLabels(self.labels)

Sources/PrometheusExample/main.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ let codable2 = MyCodable("Thing2")
6060
//
6161
//let summary = Summary<Int, SummaryCodable>("my_summary", "Just a summary")
6262
//
63-
//summary.observe(4)
64-
//summary.observe(3)
65-
//summary.observe(1)
66-
//summary.observe(3)
67-
//summary.observe(9)
68-
//summary.observe(5)
63+
//summary.observe(Int.random(in: 1...10000))
6964
//
7065
//print(summary.getMetric())

0 commit comments

Comments
 (0)