You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+24-17Lines changed: 24 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,45 +6,51 @@ A prometheus client for Swift supporting counters, gauges, histograms, summaries
6
6
7
7
# Usage
8
8
9
-
For examples, see [main.swift](./Sources/PrometheusExample/main.swift)
9
+
To see a working demo, see [PrometheusExample](./Sources/PrometheusExample/main.swift).
10
10
11
11
First, we have to create an instance of our `PrometheusClient`:
12
+
12
13
```swift
13
14
importPrometheus
14
15
let myProm =PrometheusClient()
15
16
```
16
17
17
-
## Usage with Swift-Metrics
18
-
_For more details about swift-metrics, check the GitHub repo [here](https://github.com/apple/swift-metrics)_
18
+
## Usage with SwiftMetrics
19
+
_For more details about swift-metrics, please view [the GitHub repo](https://github.com/apple/swift-metrics)._
20
+
21
+
To use SwiftPrometheus with swift-metrics, you need to configure the backend inside the `MetricsSystem`:
19
22
20
-
To use SwiftPrometheus with swift-metrics, all the setup required is this:
21
23
```swift
24
+
importMetrics
22
25
importPrometheus
23
-
importPrometheusMetrics
24
26
let myProm =PrometheusClient()
25
27
MetricsSystem.bootstrap(myProm)
26
28
```
27
29
28
-
To use prometheus specific features in a later stage of your program, or to get your metrics out of the system, there is a convenience method added to `MetricsSystem`:
30
+
To use prometheus-specific features in a later stage of your program, or to get your metrics out of the system, there is a convenience method added to `MetricsSystem`:
31
+
29
32
```swift
30
-
// This is the same instance was used in `.bootstrap()` earlier.
33
+
// This returns the same instance passed in to `.bootstrap()` earlier.
31
34
let promInstance =try MetricsSystem.prometheus()
35
+
print(promInstance.collect())
32
36
```
33
-
You can than use the same APIs that are layed out in the rest of this README
37
+
38
+
You can then use the same APIs described in the rest of this README.
34
39
35
40
## Counter
36
41
37
-
Counters go up, and reset when the process restarts.
42
+
Counters go up (they can only increase in value), and reset when the process restarts.
38
43
39
44
```swift
40
45
let counter = myProm.createCounter(forType: Int.self, named: "my_counter")
41
46
counter.inc() // Increment by 1
42
-
counter.inc(12) // Increment by given value
47
+
counter.inc(12) // Increment by given value
48
+
counter.reset() // Set the value back to 0
43
49
```
44
50
45
51
## Gauge
46
52
47
-
Gauges can go up and down
53
+
Gauges can go up and down, they represent a "point-in-time" snapshot of a value. This is similar to the sppedometer of a car.
48
54
49
55
```swift
50
56
let gauge = myProm.createGauge(forType: Int.self, named: "my_gauge")
@@ -73,16 +79,16 @@ summary.observe(4.7) // Observe the given value
73
79
74
80
## Info
75
81
76
-
Info tracks key-value information, usually about a whole target.
82
+
Info tracks key-value information, usually about a whole target. These are typically helpful details like git sha or other metadata.
77
83
78
84
```swift
79
85
structMyInfoStruct: MetricLabels {
80
86
let value: String
81
-
87
+
82
88
init() {
83
89
self.value="abc"
84
90
}
85
-
91
+
86
92
init(_v: String) {
87
93
self.value= v
88
94
}
@@ -99,6 +105,7 @@ info.info(MyInfoStruct("def"))
99
105
All metric types support adding labels, allowing for grouping of related metrics.
To keep SwiftPrometheus as clean and lightweight as possible, there is no way of exporting metrics directly to Prometheus. Instead, retrieve a formatted string that Prometheus can use, so you can integrate it in your own Serverside Swift application
123
+
Prometheus itself is designed to "pull" metrics from a destination. Following this pattern, SwiftPrometheus is designed to expose metrics, as opposed to submitted/exporting them directly to Prometheus itself. Instead, SwiftPrometheus produces a formatted string that Prometheus can parse, which can be integrated into your own application.
124
+
125
+
By default, this should be accessible on your main serving port, at the `/metrics` endpoint. An example in [Vapor](https://vapor.codes) syntax looks like:
117
126
118
-
This could look something like this:
119
127
```swift
120
128
router.get("/metrics") { request ->Stringin
121
129
return myProm.collect()
122
130
}
123
131
```
124
-
Here, I used [Vapor](https://github.com/vapor/vapor) syntax, but this will work with any web framework, since it's just returning a plain String.
0 commit comments