|
1 | | -open class Prometheus { |
| 1 | +public class Prometheus { |
| 2 | + /// Singleton instance |
| 3 | + /// |
| 4 | + /// Use this to create Metric trackers and retrieve your data, |
| 5 | + /// so you don't have to keep track of an instance. |
| 6 | + public static let shared = Prometheus() |
2 | 7 |
|
| 8 | + /// Metrics tracked by this Prometheus instance |
| 9 | + internal var metrics: [Metric] = [] |
| 10 | + |
| 11 | + // MARK: - Counter |
| 12 | + |
| 13 | + /// Creates a counter with the given values |
| 14 | + /// |
| 15 | + /// - Parameters: |
| 16 | + /// - type: Type the counter will count |
| 17 | + /// - name: Name of the counter |
| 18 | + /// - helpText: Help text for the counter. Usually a short description |
| 19 | + /// - initialValue: An initial value to set the counter to, defaults to 0 |
| 20 | + /// - labelType: Type of labels this counter can use. Can be left out to default to no labels |
| 21 | + /// |
| 22 | + /// - Returns: Counter instance |
| 23 | + public func createCounter<T: Numeric, U: MetricLabels>( |
| 24 | + forType type: T.Type, |
| 25 | + named name: String, |
| 26 | + helpText: String? = nil, |
| 27 | + initialValue: T = 0, |
| 28 | + withLabelType labelType: U.Type) -> Counter<T, U> |
| 29 | + { |
| 30 | + let counter = Counter<T, U>(name, helpText, initialValue) |
| 31 | + self.metrics.append(counter) |
| 32 | + return counter |
| 33 | + } |
| 34 | + |
| 35 | + /// Creates a counter with the given values |
| 36 | + /// |
| 37 | + /// - Parameters: |
| 38 | + /// - type: Type the counter will count |
| 39 | + /// - name: Name of the counter |
| 40 | + /// - helpText: Help text for the counter. Usually a short description |
| 41 | + /// - initialValue: An initial value to set the counter to, defaults to 0 |
| 42 | + /// |
| 43 | + /// - Returns: Counter instance |
| 44 | + public func createCounter<T: Numeric>( |
| 45 | + forType: T.Type, |
| 46 | + named name: String, |
| 47 | + helpText: String? = nil, |
| 48 | + initialValue: T = 0) -> Counter<T, EmptyCodable> |
| 49 | + { |
| 50 | + return self.createCounter(forType: T.self, named: name, helpText: helpText, initialValue: initialValue, withLabelType: EmptyCodable.self) |
| 51 | + } |
| 52 | + |
| 53 | + // MARK: - Gauge |
| 54 | + |
| 55 | + /// Creates a gauge with the given values |
| 56 | + /// |
| 57 | + /// - Parameters: |
| 58 | + /// - type: Type the gauge will hold |
| 59 | + /// - name: Name of the gauge |
| 60 | + /// - helpText: Help text for the gauge. Usually a short description |
| 61 | + /// - initialValue: An initial value to set the gauge to, defaults to 0 |
| 62 | + /// - labelType: Type of labels this gauge can use. Can be left out to default to no labels |
| 63 | + /// |
| 64 | + /// - Returns: Gauge instance |
| 65 | + public func createGauge<T: Numeric, U: MetricLabels>( |
| 66 | + forType type: T.Type, |
| 67 | + named name: String, |
| 68 | + helpText: String? = nil, |
| 69 | + initialValue: T = 0, |
| 70 | + withLabelType labelType: U.Type) -> Gauge<T, U> |
| 71 | + { |
| 72 | + let gauge = Gauge<T, U>(name, helpText, initialValue) |
| 73 | + self.metrics.append(gauge) |
| 74 | + return gauge |
| 75 | + } |
| 76 | + |
| 77 | + /// Creates a gauge with the given values |
| 78 | + /// |
| 79 | + /// - Parameters: |
| 80 | + /// - type: Type the gauge will count |
| 81 | + /// - name: Name of the gauge |
| 82 | + /// - helpText: Help text for the gauge. Usually a short description |
| 83 | + /// - initialValue: An initial value to set the gauge to, defaults to 0 |
| 84 | + /// |
| 85 | + /// - Returns: Gauge instance |
| 86 | + public func createGauge<T: Numeric>( |
| 87 | + forType: T.Type, |
| 88 | + named name: String, |
| 89 | + helpText: String? = nil, |
| 90 | + initialValue: T = 0) -> Gauge<T, EmptyCodable> |
| 91 | + { |
| 92 | + return self.createGauge(forType: T.self, named: name, helpText: helpText, initialValue: initialValue, withLabelType: EmptyCodable.self) |
| 93 | + } |
| 94 | + |
| 95 | + /// Creates prometheus formatted metrics |
| 96 | + /// |
| 97 | + /// - Returns: Newline seperated string with metrics for all Metric Trackers of this Prometheus instance |
| 98 | + public func getMetrics() -> String { |
| 99 | + return metrics.map { $0.getMetric() }.joined(separator: "\n\n") |
| 100 | + } |
3 | 101 | } |
0 commit comments