Skip to content

Commit ec3d756

Browse files
committed
Cleanup and start on final integration
1 parent 9820bbd commit ec3d756

File tree

2 files changed

+110
-59
lines changed

2 files changed

+110
-59
lines changed
Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,101 @@
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()
27

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+
}
3101
}
Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,18 @@
11
import Prometheus
22

33
struct MyCodable: MetricLabels {
4-
var thing: String
5-
6-
init() {
7-
self.thing = "*"
8-
}
9-
10-
init(_ thing: String) {
11-
self.thing = thing
12-
}
4+
var thing: String = "*"
135
}
146

15-
let codable1 = MyCodable("Thing1")
16-
let codable2 = MyCodable("Thing2")
7+
let codable1 = MyCodable(thing: "Thing1")
8+
let codable2 = MyCodable(thing: "Thing2")
179

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())
10+
let counter = Prometheus.shared.createCounter(forType: Int.self, named: "my_counter", helpText: "Just a counter", initialValue: 12)
5611

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(Int.random(in: 1...10000))
64-
//
65-
//print(summary.getMetric())
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)
17+
18+
print(Prometheus.shared.getMetrics())

0 commit comments

Comments
 (0)