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
+20-38Lines changed: 20 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,45 +6,50 @@ 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
43
48
```
44
49
45
50
## Gauge
46
51
47
-
Gauges can go up and down
52
+
Gauges can go up and down, they represent a "point-in-time" snapshot of a value. This is similar to the speedometer of a car.
48
53
49
54
```swift
50
55
let gauge = myProm.createGauge(forType: Int.self, named: "my_gauge")
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
98
+
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. SwiftPrometheus produces a formatted string that Prometheus can parse, which can be integrated into your own application.
99
+
100
+
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
101
118
-
This could look something like this:
119
102
```swift
120
103
router.get("/metrics") { request ->Stringin
121
104
return myProm.collect()
122
105
}
123
106
```
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