Skip to content

Commit a1455d8

Browse files
committed
Workable code, that needs cleaning up
1 parent 3d5d06d commit a1455d8

File tree

4 files changed

+72
-27
lines changed

4 files changed

+72
-27
lines changed

Sources/Prometheus/MetricTypes/Histogram.swift

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
1919

2020
private var buckets: [Counter<NumType, EmptyCodable>] = []
2121

22-
private var upperBounds: [Double]
22+
internal let upperBounds: [Double]
2323

24-
private var labels: Labels
24+
internal private(set) var labels: Labels
2525

2626
private let total: Counter<NumType, EmptyCodable>
2727

@@ -45,10 +45,13 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
4545
public func getMetric() -> String {
4646
var output = [String]()
4747

48-
if let help = help {
49-
output.append("# HELP \(name) \(help)")
48+
49+
if self.labels == Labels() {
50+
if let help = help {
51+
output.append("# HELP \(name) \(help)")
52+
}
53+
output.append("# TYPE \(name) histogram")
5054
}
51-
output.append("# TYPE \(name) histogram")
5255

5356
var acc: NumType = 0
5457
for (i, bound) in self.upperBounds.enumerated() {
@@ -66,7 +69,12 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
6669
return output.joined(separator: "\n")
6770
}
6871

69-
public func observe(_ value: NumType) {
72+
public func observe(_ value: NumType, _ labels: Labels? = nil) {
73+
if let labels = labels {
74+
let his = prometheus.getOrCreateHistogram(with: labels, for: self)
75+
his.observe(value)
76+
return
77+
}
7078
self.total.inc(value)
7179

7280
for (i, bound) in self.upperBounds.enumerated() {
@@ -77,3 +85,21 @@ public class Histogram<NumType: DoubleRepresentable, Labels: HistogramLabels>: M
7785
}
7886
}
7987
}
88+
89+
extension Prometheus {
90+
fileprivate func getOrCreateHistogram<T: Numeric, U: HistogramLabels>(with labels: U, for his: Histogram<T, U>) -> Histogram<T, U> {
91+
let summaries = metrics.filter { (metric) -> Bool in
92+
guard let metric = metric as? Histogram<T, U> else { return false }
93+
guard metric.name == his.name, metric.help == his.help, metric.labels == labels else { return false }
94+
return true
95+
} as? [Histogram<T, U>] ?? []
96+
if summaries.count > 2 { fatalError("Somehow got 2 summaries with the same data type") }
97+
if let summary = summaries.first {
98+
return summary
99+
} else {
100+
let histogram = Histogram<T, U>(his.name, his.help, labels, his.upperBounds, self)
101+
self.metrics.append(histogram)
102+
return histogram
103+
}
104+
}
105+
}

Sources/Prometheus/MetricTypes/Summary.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Summary<NumType: DoubleRepresentable, Labels: SummaryLabels>: Metri
2727

2828
internal let quantiles: [Double]
2929

30-
internal init(_ name: String, _ help: String? = nil, _ quantiles: [Double] = defaultQuantiles, _ labels: Labels = Labels(), _ p: Prometheus) {
30+
internal init(_ name: String, _ help: String? = nil, _ labels: Labels = Labels(), _ quantiles: [Double] = defaultQuantiles, _ p: Prometheus) {
3131
self.name = name
3232
self.help = help
3333

@@ -82,16 +82,18 @@ public class Summary<NumType: DoubleRepresentable, Labels: SummaryLabels>: Metri
8282

8383
extension Prometheus {
8484
fileprivate func getOrCreateSummary<T: Numeric, U: SummaryLabels>(withLabels labels: U, forSummary sum: Summary<T, U>) -> Summary<T, U> {
85-
let summary = metrics.filter { (metric) -> Bool in
85+
let summaries = metrics.filter { (metric) -> Bool in
8686
guard let metric = metric as? Summary<T, U> else { return false }
8787
guard metric.name == sum.name, metric.help == sum.help, metric.labels == labels else { return false }
8888
return true
8989
} as? [Summary<T, U>] ?? []
90-
if summary.count > 2 { fatalError("Somehow got 2 summaries with the same data type") }
91-
if let summary = summary.first {
90+
if summaries.count > 2 { fatalError("Somehow got 2 summaries with the same data type") }
91+
if let summary = summaries.first {
9292
return summary
9393
} else {
94-
return createSummary(forType: T.self, named: sum.name, helpText: sum.help, quantiles: sum.quantiles, labels: labels)
94+
let summary = Summary<T, U>(sum.name, sum.help, labels, sum.quantiles, self)
95+
self.metrics.append(summary)
96+
return summary
9597
}
9698
}
9799
}

Sources/Prometheus/Prometheus.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ public class Prometheus {
109109
named name: String,
110110
helpText: String? = nil,
111111
buckets: [Double] = defaultBuckets,
112-
labels: U) -> Histogram<T, U>
112+
labels: U.Type) -> Histogram<T, U>
113113
{
114-
let histogram = Histogram<T, U>(name, helpText, labels, buckets, self)
114+
let histogram = Histogram<T, U>(name, helpText, U(), buckets, self)
115115
self.metrics.append(histogram)
116116
return histogram
117117
}
@@ -131,7 +131,7 @@ public class Prometheus {
131131
helpText: String? = nil,
132132
buckets: [Double] = defaultBuckets) -> Histogram<T, EmptyHistogramCodable>
133133
{
134-
return self.createHistogram(forType: type, named: name, helpText: helpText, buckets: buckets, labels: EmptyHistogramCodable())
134+
return self.createHistogram(forType: type, named: name, helpText: helpText, buckets: buckets, labels: EmptyHistogramCodable.self)
135135
}
136136

137137
// MARK: - Summary
@@ -141,9 +141,9 @@ public class Prometheus {
141141
named name: String,
142142
helpText: String? = nil,
143143
quantiles: [Double] = defaultQuantiles,
144-
labels: U) -> Summary<T, U>
144+
labels: U.Type) -> Summary<T, U>
145145
{
146-
let summary = Summary<T, U>(name, helpText, quantiles, labels, self)
146+
let summary = Summary<T, U>(name, helpText, U(), quantiles, self)
147147
metrics.append(summary)
148148
return summary
149149
}
@@ -154,13 +154,13 @@ public class Prometheus {
154154
helpText: String? = nil,
155155
quantiles: [Double] = defaultQuantiles) -> Summary<T, EmptySummaryCodable>
156156
{
157-
return self.createSummary(forType: type, named: name, helpText: helpText, quantiles: quantiles, labels: EmptySummaryCodable())
157+
return self.createSummary(forType: type, named: name, helpText: helpText, quantiles: quantiles, labels: EmptySummaryCodable.self)
158158
}
159159

160160
/// Creates prometheus formatted metrics
161161
///
162162
/// - Returns: Newline seperated string with metrics for all Metric Trackers of this Prometheus instance
163163
public func getMetrics() -> String {
164-
return metrics.map { $0.getMetric() }.joined(separator: "\n\n")
164+
return metrics.map { $0.getMetric() }.joined(separator: "\n")
165165
}
166166
}

Sources/PrometheusExample/main.swift

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,51 @@ struct MyCodable: MetricLabels {
77
let codable1 = MyCodable(thing: "Thing1")
88
let codable2 = MyCodable(thing: "Thing2")
99

10-
let counter = Prometheus.shared.createCounter(forType: Int.self, named: "my_counter", helpText: "Just a counter", initialValue: 12)
10+
//let counter = Prometheus.shared.createCounter(forType: Int.self, named: "my_counter", helpText: "Just a counter", initialValue: 12)
11+
//
12+
//counter.inc(5)
13+
//
14+
//let gauge = Prometheus.shared.createGauge(forType: Int.self, named: "my_gauge", helpText: "Just a gauge", initialValue: 0)
15+
//
16+
//gauge.set(123)
1117

12-
counter.inc(5)
13-
14-
let gauge = Prometheus.shared.createGauge(forType: Int.self, named: "my_gauge", helpText: "Just a gauge", initialValue: 0)
15-
16-
gauge.set(123)
18+
struct HistogramThing: HistogramLabels {
19+
var le: String = ""
20+
let route: String
21+
22+
init() {
23+
self.route = "*"
24+
}
25+
26+
init(_ route: String) {
27+
self.route = route
28+
}
29+
}
1730

18-
let histogram = Prometheus.shared.createHistogram(forType: Double.self, named: "my_histogram", helpText: "Just a histogram")
31+
let histogram = Prometheus.shared.createHistogram(forType: Double.self, named: "my_histogram", helpText: "Just a histogram", labels: HistogramThing.self)
1932

2033
for _ in 0...Int.random(in: 10...50) {
2134
histogram.observe(Double.random(in: 0...1))
2235
}
2336

37+
for _ in 0...Int.random(in: 10...50) {
38+
histogram.observe(Double.random(in: 0...1), HistogramThing("/test"))
39+
}
40+
2441
struct SummaryThing: SummaryLabels {
2542
var quantile: String = ""
2643
let route: String
2744

2845
init() {
2946
self.route = "*"
3047
}
31-
48+
3249
init(_ route: String) {
3350
self.route = route
3451
}
3552
}
3653

37-
let summary = Prometheus.shared.createSummary(forType: Double.self, named: "my_summary", helpText: "Just a summary", labels: SummaryThing())
54+
let summary = Prometheus.shared.createSummary(forType: Double.self, named: "my_summary", helpText: "Just a summary", labels: SummaryThing.self)
3855

3956
for _ in 0...Int.random(in: 100...1000) {
4057
summary.observe(Double.random(in: 0...10000))

0 commit comments

Comments
 (0)