Skip to content

Commit c654c5a

Browse files
committed
Add Info
1 parent c5b6fd8 commit c654c5a

File tree

3 files changed

+154
-65
lines changed

3 files changed

+154
-65
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class Info<Labels: MetricLabels>: Metric, PrometheusHandled {
2+
internal let prometheus: Prometheus
3+
4+
public let name: String
5+
public let help: String?
6+
7+
public let _type: MetricType = .info
8+
9+
internal var labels = Labels()
10+
11+
internal init(_ name: String, _ help: String? = nil, _ p: Prometheus) {
12+
self.name = name
13+
self.help = help
14+
self.prometheus = p
15+
}
16+
17+
public func info(_ labels: Labels) {
18+
self.labels = labels
19+
}
20+
21+
public func getMetric() -> String {
22+
var output = [String]()
23+
24+
output.append(headers)
25+
26+
let labelsString = encodeLabels(labels)
27+
output.append("\(name)\(labelsString) 1.0")
28+
29+
return output.joined(separator: "\n")
30+
}
31+
}

Sources/Prometheus/Prometheus.swift

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public class Prometheus {
101101
/// - name: Name of the histogram
102102
/// - helpText: Help text for the histogram. Usually a short description
103103
/// - buckets: Buckets to divide values over
104-
/// - labels: Labels to give this Histogram. Can be left out to default to no labels
104+
/// - labels: Labels to give this histogram. Can be left out to default to no labels
105105
///
106106
/// - Returns: Histogram instance
107107
public func createHistogram<T: Numeric, U: HistogramLabels>(
@@ -136,6 +136,16 @@ public class Prometheus {
136136

137137
// MARK: - Summary
138138

139+
/// Creates a summary with the given values
140+
///
141+
/// - Parameters:
142+
/// - type: The type the summary will observe
143+
/// - name: Name of the summary
144+
/// - helpText: Help text for the summary. Usually a short description
145+
/// - quantiles: Quantiles to caluculate
146+
/// - labels: Labels to give this summary. Can be left out to default to no labels
147+
///
148+
/// - Returns: Summary instance
139149
public func createSummary<T: Numeric, U: SummaryLabels>(
140150
forType type: T.Type,
141151
named name: String,
@@ -148,6 +158,15 @@ public class Prometheus {
148158
return summary
149159
}
150160

161+
/// Creates a summary with the given values
162+
///
163+
/// - Parameters:
164+
/// - type: The type the summary will observe
165+
/// - name: Name of the summary
166+
/// - helpText: Help text for the summary. Usually a short description
167+
/// - quantiles: Quantiles to caluculate
168+
///
169+
/// - Returns: Summary instance
151170
public func createSummary<T: Numeric>(
152171
forType type: T.Type,
153172
named name: String,
@@ -157,6 +176,26 @@ public class Prometheus {
157176
return self.createSummary(forType: type, named: name, helpText: helpText, quantiles: quantiles, labels: EmptySummaryCodable.self)
158177
}
159178

179+
// MARK: - Info
180+
181+
/// Creates an Info metric with the given values
182+
///
183+
/// - Parameters
184+
/// - name: Name of the info
185+
/// - helpText: Help text for the info. Usually a short description
186+
/// - labelType: Type of labels this Info can use
187+
///
188+
/// - Returns Info instance
189+
public func createInfo<U: MetricLabels>(
190+
named name: String,
191+
helpText: String? = nil,
192+
labelType: U.Type) -> Info<U>
193+
{
194+
let info = Info<U>(name, helpText, self)
195+
self.metrics.append(info)
196+
return info
197+
}
198+
160199
/// Creates prometheus formatted metrics
161200
///
162201
/// - Returns: Newline seperated string with metrics for all Metric Trackers of this Prometheus instance

Sources/PrometheusExample/main.swift

Lines changed: 83 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,87 @@
11
import Prometheus
22

3-
struct MyCodable: MetricLabels {
4-
var thing: String = "*"
5-
}
6-
7-
let codable1 = MyCodable(thing: "Thing1")
8-
let codable2 = MyCodable(thing: "Thing2")
9-
10-
let counter = Prometheus.shared.createCounter(forType: Int.self, named: "my_counter", helpText: "Just a counter", initialValue: 12, withLabelType: MyCodable.self)
11-
12-
counter.inc(5)
13-
counter.inc(Int.random(in: 0...100), codable2)
14-
counter.inc(Int.random(in: 0...100), codable1)
15-
16-
let gauge = Prometheus.shared.createGauge(forType: Int.self, named: "my_gauge", helpText: "Just a gauge", initialValue: 12, withLabelType: MyCodable.self)
17-
18-
gauge.inc(100)
19-
gauge.inc(Int.random(in: 0...100), codable2)
20-
gauge.inc(Int.random(in: 0...100), codable1)
21-
22-
struct HistogramThing: HistogramLabels {
23-
var le: String = ""
24-
let route: String
25-
26-
init() {
27-
self.route = "*"
28-
}
29-
30-
init(_ route: String) {
31-
self.route = route
32-
}
33-
}
34-
35-
let histogram = Prometheus.shared.createHistogram(forType: Double.self, named: "my_histogram", helpText: "Just a histogram", labels: HistogramThing.self)
36-
37-
for _ in 0...Int.random(in: 10...50) {
38-
histogram.observe(Double.random(in: 0...1))
39-
}
40-
41-
for _ in 0...Int.random(in: 10...50) {
42-
histogram.observe(Double.random(in: 0...1), HistogramThing("/test"))
43-
}
44-
45-
struct SummaryThing: SummaryLabels {
46-
var quantile: String = ""
47-
let route: String
48-
49-
init() {
50-
self.route = "*"
51-
}
52-
53-
init(_ route: String) {
54-
self.route = route
55-
}
56-
}
57-
58-
let summary = Prometheus.shared.createSummary(forType: Double.self, named: "my_summary", helpText: "Just a summary", labels: SummaryThing.self)
59-
60-
for _ in 0...Int.random(in: 100...1000) {
61-
summary.observe(Double.random(in: 0...10000))
62-
}
63-
64-
for _ in 0...Int.random(in: 100...1000) {
65-
summary.observe(Double.random(in: 0...10000), SummaryThing("/test"))
66-
}
3+
//struct MyCodable: MetricLabels {
4+
// var thing: String = "*"
5+
//}
6+
//
7+
//let codable1 = MyCodable(thing: "Thing1")
8+
//let codable2 = MyCodable(thing: "Thing2")
9+
//
10+
//let counter = Prometheus.shared.createCounter(forType: Int.self, named: "my_counter", helpText: "Just a counter", initialValue: 12, withLabelType: MyCodable.self)
11+
//
12+
//counter.inc(5)
13+
//counter.inc(Int.random(in: 0...100), codable2)
14+
//counter.inc(Int.random(in: 0...100), codable1)
15+
//
16+
//let gauge = Prometheus.shared.createGauge(forType: Int.self, named: "my_gauge", helpText: "Just a gauge", initialValue: 12, withLabelType: MyCodable.self)
17+
//
18+
//gauge.inc(100)
19+
//gauge.inc(Int.random(in: 0...100), codable2)
20+
//gauge.inc(Int.random(in: 0...100), codable1)
21+
//
22+
//struct HistogramThing: HistogramLabels {
23+
// var le: String = ""
24+
// let route: String
25+
//
26+
// init() {
27+
// self.route = "*"
28+
// }
29+
//
30+
// init(_ route: String) {
31+
// self.route = route
32+
// }
33+
//}
34+
//
35+
//let histogram = Prometheus.shared.createHistogram(forType: Double.self, named: "my_histogram", helpText: "Just a histogram", labels: HistogramThing.self)
36+
//
37+
//for _ in 0...Int.random(in: 10...50) {
38+
// histogram.observe(Double.random(in: 0...1))
39+
//}
40+
//
41+
//for _ in 0...Int.random(in: 10...50) {
42+
// histogram.observe(Double.random(in: 0...1), HistogramThing("/test"))
43+
//}
44+
//
45+
//struct SummaryThing: SummaryLabels {
46+
// var quantile: String = ""
47+
// let route: String
48+
//
49+
// init() {
50+
// self.route = "*"
51+
// }
52+
//
53+
// init(_ route: String) {
54+
// self.route = route
55+
// }
56+
//}
57+
//
58+
//let summary = Prometheus.shared.createSummary(forType: Double.self, named: "my_summary", helpText: "Just a summary", labels: SummaryThing.self)
59+
//
60+
//for _ in 0...Int.random(in: 100...1000) {
61+
// summary.observe(Double.random(in: 0...10000))
62+
//}
63+
//
64+
//for _ in 0...Int.random(in: 100...1000) {
65+
// summary.observe(Double.random(in: 0...10000), SummaryThing("/test"))
66+
//}
67+
68+
//struct MyInfoStruct: MetricLabels {
69+
// let version: String
70+
// let major: String
71+
//
72+
// init() {
73+
// self.version = "1.0.0"
74+
// self.major = "1"
75+
// }
76+
//
77+
// init(_ v: String, _ m: String) {
78+
// self.version = v
79+
// self.major = m
80+
// }
81+
//}
82+
//
83+
//let info = Prometheus.shared.createInfo(named: "my_info", helpText: "Just some info", labelType: MyInfoStruct.self)
84+
//
85+
//info.info(MyInfoStruct("2.0.0", "2"))
6786

6887
print(Prometheus.shared.getMetrics())

0 commit comments

Comments
 (0)