Skip to content

Commit 83b6679

Browse files
committed
Implement Summary?
1 parent 8c8e528 commit 83b6679

File tree

3 files changed

+146
-39
lines changed

3 files changed

+146
-39
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
public protocol SummaryLabels: MetricLabels {
2+
var quantile: String { get set }
3+
}
4+
5+
extension SummaryLabels {
6+
init() {
7+
self.init()
8+
self.quantile = ""
9+
}
10+
}
11+
12+
public class Summary<NumType: DoubleRepresentable, Labels: SummaryLabels>: Metric {
13+
public var name: String
14+
15+
public var help: String?
16+
17+
public var labels: Labels
18+
19+
private var sum: Counter<NumType, EmptyCodable>
20+
21+
private var count: Counter<NumType, EmptyCodable>
22+
23+
private var values: [NumType] = []
24+
25+
public init(_ name: String, _ help: String? = nil, _ labels: Labels = Labels()) {
26+
self.name = name
27+
self.help = help
28+
29+
self.sum = .init("\(self.name)_sum")
30+
31+
self.count = .init("\(self.name)_count")
32+
33+
self.labels = labels
34+
}
35+
36+
public func getMetric() -> String {
37+
var output = [String]()
38+
39+
if let help = help {
40+
output.append("# HELP \(name) \(help)")
41+
}
42+
output.append("# TYPE \(name) summary")
43+
44+
let quantiles = [0.5, 0.9, 0.99]
45+
46+
calculateQuantiles(quantiles: quantiles, values: values.map { $0.doubleValue }).forEach { (arg) in
47+
let (q, v) = arg
48+
self.labels.quantile = "\(q)"
49+
let labelsString = encodeLabels(self.labels)
50+
output.append("\(name)\(labelsString) \(v)")
51+
}
52+
53+
let labelsString = encodeLabels(self.labels, ["quantile"])
54+
output.append("\(name)_count\(labelsString) \(count.get())")
55+
output.append("\(name)_sum\(labelsString) \(sum.get())")
56+
57+
return output.joined(separator: "\n")
58+
}
59+
60+
public func observe(_ value: NumType) {
61+
self.count.inc(1)
62+
self.sum.inc(value)
63+
self.values.append(value)
64+
}
65+
}

Sources/Prometheus/Utils.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,34 @@ public func encodeLabels<Labels: MetricLabels>(_ labels: Labels, _ excludingKeys
2424
}
2525
}
2626

27+
func calculateQuantiles(quantiles: [Double], values: [Double]) -> [Double: Double] {
28+
let values = values.sorted()
29+
var quantilesMap: [Double: Double] = [:]
30+
quantiles.forEach { (q) in
31+
quantilesMap[q] = quantile(q, values)
32+
}
33+
return quantilesMap
34+
}
35+
36+
func quantile(_ q: Double, _ values: [Double]) -> Double {
37+
if values.count == 1 {
38+
return values[0]
39+
}
40+
41+
let n = Double(values.count)
42+
if let pos = Int(exactly: n*q) {
43+
if pos < 2 {
44+
return values[0]
45+
} else if pos == values.count {
46+
return values[pos - 1]
47+
}
48+
return (values[pos - 1] + values[pos]) / 2.0
49+
} else {
50+
let pos = Int((n*q).rounded(.up))
51+
return values[pos - 1]
52+
}
53+
}
54+
2755
extension Double {
2856
var description: String {
2957
if self == Double.greatestFiniteMagnitude {

Sources/PrometheusExample/main.swift

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,56 @@ struct MyCodable: MetricLabels {
1515
let codable1 = MyCodable("Thing1")
1616
let codable2 = MyCodable("Thing2")
1717

18-
let counter = Counter<Int, MyCodable>("some_test_value", "This value holds just a random imcrementer :)", 0)
19-
20-
counter.inc(5, codable1)
21-
22-
counter.inc(5, codable2)
23-
24-
counter.inc(5)
25-
26-
counter.inc(5, codable1)
27-
28-
print(counter.getMetric())
29-
30-
let gauge = Gauge<Int, MyCodable>("some_test_value", "This value holds a random Gauge :)")
31-
32-
gauge.inc(codable2)
33-
34-
gauge.dec()
35-
36-
gauge.inc(1, codable1)
37-
38-
let arr = [1, 2, 3, 4]
39-
40-
gauge.set(arr.count)
41-
42-
print(gauge.getMetric())
43-
44-
struct HistogramCodable: HistogramLabels {
45-
var le: String = ""
46-
}
47-
48-
let histogram = Histogram<Int, HistogramCodable>("my_histogram", "Just a histogram")
49-
50-
histogram.observe(6)
51-
histogram.observe(6)
52-
histogram.observe(1)
53-
histogram.observe(4)
54-
55-
print(histogram.getMetric())
56-
18+
//let counter = Counter<Int, MyCodable>("some_test_value", "This value holds just a random imcrementer :)", 0)
19+
//
20+
//counter.inc(5, codable1)
21+
//
22+
//counter.inc(5, codable2)
23+
//
24+
//counter.inc(5)
25+
//
26+
//counter.inc(5, codable1)
27+
//
28+
//print(counter.getMetric())
29+
//
30+
//let gauge = Gauge<Int, MyCodable>("some_test_value", "This value holds a random Gauge :)")
31+
//
32+
//gauge.inc(codable2)
33+
//
34+
//gauge.dec()
35+
//
36+
//gauge.inc(1, codable1)
37+
//
38+
//let arr = [1, 2, 3, 4]
39+
//
40+
//gauge.set(arr.count)
41+
//
42+
//print(gauge.getMetric())
43+
//
44+
//struct HistogramCodable: HistogramLabels {
45+
// var le: String = ""
46+
//}
47+
//
48+
//let histogram = Histogram<Int, HistogramCodable>("my_histogram", "Just a histogram")
49+
//
50+
//histogram.observe(6)
51+
//histogram.observe(6)
52+
//histogram.observe(1)
53+
//histogram.observe(4)
54+
//
55+
//print(histogram.getMetric())
56+
57+
//struct SummaryCodable: SummaryLabels {
58+
// var quantile: String = ""
59+
//}
60+
//
61+
//let summary = Summary<Int, SummaryCodable>("my_summary", "Just a summary")
62+
//
63+
//summary.observe(4)
64+
//summary.observe(3)
65+
//summary.observe(1)
66+
//summary.observe(3)
67+
//summary.observe(9)
68+
//summary.observe(5)
69+
//
70+
//print(summary.getMetric())

0 commit comments

Comments
 (0)