-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathPrometheusMetrics.swift
380 lines (317 loc) · 12.7 KB
/
PrometheusMetrics.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import CoreMetrics
private class MetricsCounter: CounterHandler {
let counter: PromCounter<Int64>
let labels: DimensionLabels?
internal init(counter: PromCounter<Int64>, dimensions: [(String, String)]) {
self.counter = counter
guard !dimensions.isEmpty else {
labels = nil
return
}
self.labels = DimensionLabels(dimensions)
}
func increment(by: Int64) {
self.counter.inc(by, labels)
}
func reset() { }
}
private class MetricsFloatingPointCounter: FloatingPointCounterHandler {
let counter: PromCounter<Double>
let labels: DimensionLabels?
internal init(counter: PromCounter<Double>, dimensions: [(String, String)]) {
self.counter = counter
guard !dimensions.isEmpty else {
labels = nil
return
}
self.labels = DimensionLabels(dimensions)
}
func increment(by: Double) {
self.counter.inc(by, labels)
}
func reset() { }
}
private class MetricsGauge: RecorderHandler {
let gauge: PromGauge<Double>
let labels: DimensionLabels?
internal init(gauge: PromGauge<Double>, dimensions: [(String, String)]) {
self.gauge = gauge
guard !dimensions.isEmpty else {
labels = nil
return
}
self.labels = DimensionLabels(dimensions)
}
func record(_ value: Int64) {
self.record(value.doubleValue)
}
func record(_ value: Double) {
gauge.set(value, labels)
}
}
private class MetricsHistogram: RecorderHandler {
let histogram: PromHistogram<Double>
let labels: DimensionLabels?
internal init(histogram: PromHistogram<Double>, dimensions: [(String, String)]) {
self.histogram = histogram
guard !dimensions.isEmpty else {
labels = nil
return
}
self.labels = DimensionLabels(dimensions)
}
func record(_ value: Int64) {
histogram.observe(value.doubleValue, labels)
}
func record(_ value: Double) {
histogram.observe(value, labels)
}
}
class MetricsHistogramTimer: TimerHandler {
let histogram: PromHistogram<Int64>
let labels: DimensionLabels?
init(histogram: PromHistogram<Int64>, dimensions: [(String, String)]) {
self.histogram = histogram
if !dimensions.isEmpty {
self.labels = DimensionLabels(dimensions)
} else {
self.labels = nil
}
}
func recordNanoseconds(_ duration: Int64) {
return histogram.observe(duration, labels)
}
}
private class MetricsSummary: TimerHandler {
let summary: PromSummary<Int64>
let labels: DimensionLabels?
func preferDisplayUnit(_ unit: TimeUnit) {
self.summary.preferDisplayUnit(unit)
}
internal init(summary: PromSummary<Int64>, dimensions: [(String, String)]) {
self.summary = summary
guard !dimensions.isEmpty else {
labels = nil
return
}
self.labels = DimensionLabels(dimensions)
}
func recordNanoseconds(_ duration: Int64) {
return summary.observe(duration, labels)
}
}
/// Defines the base for a bridge between PrometheusClient and swift-metrics.
/// Used by `SwiftMetrics.prometheus()` to get an instance of `PrometheusClient` from `MetricsSystem`
///
/// Any custom implementation of `MetricsFactory` using `PrometheusClient` should conform to this implementation.
public protocol PrometheusWrappedMetricsFactory: MetricsFactory {
var client: PrometheusClient { get }
}
/// A bridge between PrometheusClient and swift-metrics. Prometheus types don't map perfectly on swift-metrics API,
/// which makes bridge implementation non trivial. This class defines how exactly swift-metrics types should be backed
/// with Prometheus types, e.g. how to sanitize labels, what buckets/quantiles to use for recorder/timer, etc.
public struct PrometheusMetricsFactory: PrometheusWrappedMetricsFactory {
/// Prometheus client to bridge swift-metrics API to.
public let client: PrometheusClient
/// Bridge configuration.
private let configuration: Configuration
public init(client: PrometheusClient,
configuration: Configuration = Configuration()) {
self.client = client
self.configuration = configuration
}
public func destroyCounter(_ handler: CounterHandler) {
guard let handler = handler as? MetricsCounter else { return }
client.removeMetric(handler.counter)
}
public func destroyFloatingPointCounter(_ handler: FloatingPointCounterHandler) {
guard let handler = handler as? MetricsFloatingPointCounter else { return }
client.removeMetric(handler.counter)
}
public func destroyRecorder(_ handler: RecorderHandler) {
if let handler = handler as? MetricsGauge {
client.removeMetric(handler.gauge)
}
if let handler = handler as? MetricsHistogram {
client.removeMetric(handler.histogram)
}
}
public func destroyTimer(_ handler: TimerHandler) {
switch self.configuration.timerImplementation._wrapped {
case .summary:
guard let handler = handler as? MetricsSummary else { return }
client.removeMetric(handler.summary)
case .histogram:
guard let handler = handler as? MetricsHistogramTimer else { return }
client.removeMetric(handler.histogram)
}
}
public func makeCounter(label: String, dimensions: [(String, String)]) -> CounterHandler {
let label = configuration.labelSanitizer.sanitize(label)
let counter = client.createCounter(forType: Int64.self, named: label)
return MetricsCounter(counter: counter, dimensions: dimensions.sanitized())
}
public func makeFloatingPointCounter(label: String, dimensions: [(String, String)]) -> FloatingPointCounterHandler {
let label = configuration.labelSanitizer.sanitize(label)
let counter = client.createCounter(forType: Double.self, named: label)
return MetricsFloatingPointCounter(counter: counter, dimensions: dimensions.sanitized())
}
public func makeRecorder(label: String, dimensions: [(String, String)], aggregate: Bool) -> RecorderHandler {
let label = configuration.labelSanitizer.sanitize(label)
return aggregate ? makeHistogram(label: label, dimensions: dimensions) : makeGauge(label: label, dimensions: dimensions)
}
private func makeGauge(label: String, dimensions: [(String, String)]) -> RecorderHandler {
let label = configuration.labelSanitizer.sanitize(label)
let gauge = client.createGauge(forType: Double.self, named: label)
return MetricsGauge(gauge: gauge, dimensions: dimensions.sanitized())
}
private func makeHistogram(label: String, dimensions: [(String, String)]) -> RecorderHandler {
let label = configuration.labelSanitizer.sanitize(label)
let histogram = client.createHistogram(forType: Double.self, named: label, buckets: configuration.defaultRecorderBuckets)
return MetricsHistogram(histogram: histogram, dimensions: dimensions.sanitized())
}
public func makeTimer(label: String, dimensions: [(String, String)]) -> TimerHandler {
switch configuration.timerImplementation._wrapped {
case .summary(let quantiles):
return self.makeSummaryTimer(label: label, dimensions: dimensions, quantiles: quantiles)
case .histogram(let buckets):
return self.makeHistogramTimer(label: label, dimensions: dimensions, buckets: buckets)
}
}
/// There's two different ways to back swift-api `Timer` with Prometheus classes.
/// This method creates `Summary` backed timer implementation
private func makeSummaryTimer(label: String, dimensions: [(String, String)], quantiles: [Double]) -> TimerHandler {
let label = configuration.labelSanitizer.sanitize(label)
let summary = client.createSummary(forType: Int64.self, named: label, quantiles: quantiles)
return MetricsSummary(summary: summary, dimensions: dimensions.sanitized())
}
/// There's two different ways to back swift-api `Timer` with Prometheus classes.
/// This method creates `Histogram` backed timer implementation
private func makeHistogramTimer(label: String, dimensions: [(String, String)], buckets: Buckets) -> TimerHandler {
let label = configuration.labelSanitizer.sanitize(label)
let histogram = client.createHistogram(forType: Int64.self, named: label, buckets: buckets)
return MetricsHistogramTimer(histogram: histogram, dimensions: dimensions.sanitized())
}
}
extension Array where Element == (String, String) {
func sanitized() -> [(String, String)] {
let sanitizer = DimensionsSanitizer()
return self.map {
(sanitizer.sanitize($0.0), $0.1)
}
}
}
public extension MetricsSystem {
/// Get the bootstrapped `MetricsSystem` as `PrometheusClient`
///
/// - Returns: `PrometheusClient` used to bootstrap `MetricsSystem`
/// - Throws: `PrometheusError.PrometheusFactoryNotBootstrapped`
/// if no `PrometheusClient` was used to bootstrap `MetricsSystem`
static func prometheus() throws -> PrometheusClient {
guard let prom = self.factory as? PrometheusWrappedMetricsFactory else {
throw PrometheusError.prometheusFactoryNotBootstrapped(bootstrappedWith: "\(self.factory)")
}
return prom.client
}
}
// MARK: - Labels
/// A generic `String` based `CodingKey` implementation.
private struct StringCodingKey: CodingKey {
/// `CodingKey` conformance.
public var stringValue: String
/// `CodingKey` conformance.
public var intValue: Int? {
return Int(self.stringValue)
}
/// Creates a new `StringCodingKey`.
public init(_ string: String) {
self.stringValue = string
}
/// `CodingKey` conformance.
public init(stringValue: String) {
self.stringValue = stringValue
}
/// `CodingKey` conformance.
public init(intValue: Int) {
self.stringValue = intValue.description
}
}
/// Helper for dimensions
public struct DimensionLabels: Hashable, ExpressibleByArrayLiteral {
let dimensions: [(String, String)]
public init() {
self.dimensions = []
}
public init(_ dimensions: [(String, String)]) {
self.dimensions = dimensions
}
public init(arrayLiteral elements: (String, String)...) {
self.init(elements)
}
public func hash(into hasher: inout Hasher) {
for (key, value) in dimensions {
hasher.combine(key)
hasher.combine(value)
}
}
public static func == (lhs: DimensionLabels, rhs: DimensionLabels) -> Bool {
guard lhs.dimensions.count == rhs.dimensions.count else { return false }
for index in 0..<lhs.dimensions.count {
guard lhs.dimensions[index] == rhs.dimensions[index] else { return false }
}
return true
}
}
extension DimensionLabels: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: StringCodingKey.self)
for (key, value) in self.dimensions {
try container.encode(value, forKey: .init(key))
}
}
}
/// Helper for dimensions
/// swift-metrics api doesn't allow setting buckets explicitly.
/// If default buckets don't fit, this Labels implementation is a nice default to create Prometheus metric types with
struct EncodableHistogramLabels: Encodable {
/// Bucket
let le: String?
/// Dimensions
let labels: DimensionLabels?
public init(labels: DimensionLabels?, le: String? = nil) {
self.le = le
self.labels = labels
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: StringCodingKey.self)
if let labels = labels {
for (key, value) in labels.dimensions {
try container.encode(value, forKey: .init(key))
}
}
if let le = le {
try container.encode(le, forKey: .init("le"))
}
}
}
struct EncodableSummaryLabels: Encodable {
/// Quantile
var quantile: String?
/// Dimensions
let labels: DimensionLabels?
public init(labels: DimensionLabels?, quantile: String?) {
self.quantile = quantile
self.labels = labels
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: StringCodingKey.self)
if let labels = labels {
for (key, value) in labels.dimensions {
try container.encode(value, forKey: .init(key))
}
}
if let quantile = quantile {
try container.encode(quantile, forKey: .init("quantile"))
}
}
}