|
1 |
| -[](http://swift.org) |
| 1 | +# Swift Prometheus |
2 | 2 |
|
3 |
| -# SwiftPrometheus, Prometheus client for Swift |
| 3 | +[][SSWG-Incubation] |
| 4 | +[][Documentation] |
4 | 5 |
|
5 |
| -A prometheus client for Swift supporting counters, gauges, histograms, summaries and info. |
6 |
| - |
7 |
| -# Installation |
8 |
| - |
9 |
| -SwiftPrometheus is available through SPM. To include it in your project add the following dependency to your `Package.swift`: |
10 |
| -```swift |
11 |
| -.package(url: "https://github.com/swift-server-community/SwiftPrometheus.git", from: "1.0.2") |
12 |
| -``` |
13 |
| - |
14 |
| -# Usage |
15 |
| - |
16 |
| -To see a working demo, see [PrometheusExample](./Sources/PrometheusExample/main.swift). |
17 |
| - |
18 |
| -First, we have to create an instance of our `PrometheusClient`: |
19 |
| - |
20 |
| -```swift |
21 |
| -import Prometheus |
22 |
| -let myProm = PrometheusClient() |
23 |
| -``` |
24 |
| - |
25 |
| -## Usage with SwiftMetrics |
26 |
| -_For more details about swift-metrics, please view [the GitHub repo](https://github.com/apple/swift-metrics)._ |
27 |
| - |
28 |
| -Starting with SwiftPrometheus [1.0.0-alpha.10](https://github.com/swift-server-community/SwiftPrometheus/releases/tag/1.0.0-alpha.10) `MetricsSystem` is no longer directly configured with a `PrometheusClient`. |
29 |
| - |
30 |
| -Instead, create a `PrometheusMetricsFactory` instance wrapping a `PrometheusClient`. |
31 |
| - |
32 |
| -```swift |
33 |
| -let myProm = PrometheusClient() |
34 |
| -MetricsSystem.bootstrap(PrometheusMetricsFactory(client: myProm)) |
35 |
| -``` |
36 |
| - |
37 |
| -Along with a `PrometheusClient`, `PrometheusMetricsFactory` can take a `Configuration` object setting the following properties: |
38 |
| -- A `LabelSanitizer` used to sanitize metric names to valid Prometheus values. A default implementation is provided. |
39 |
| -- The Prometheus metric type to use for swift-metrics' `Timer`. Can be a `Histogram` or a `Summary`. Note that when using `Histogram`, `preferredDisplayUnit` will not be observed. |
40 |
| -- Default buckets for use by aggregating swift-metrics `Recorder` instances. |
41 |
| - |
42 |
| -### Before Alpha 10 |
43 |
| - |
44 |
| -To use SwiftPrometheus with swift-metrics, you need to configure the backend inside the `MetricsSystem`: |
45 |
| - |
46 |
| -```swift |
47 |
| -import Metrics |
48 |
| -import Prometheus |
49 |
| -let myProm = PrometheusClient() |
50 |
| -MetricsSystem.bootstrap(myProm) |
51 |
| -``` |
52 |
| - |
53 |
| -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`: |
54 |
| - |
55 |
| -```swift |
56 |
| -// This returns the same instance passed in to `.bootstrap()` earlier. |
57 |
| -let promInstance = try MetricsSystem.prometheus() |
58 |
| -print(promInstance.collect()) |
59 |
| -``` |
60 |
| - |
61 |
| -You can then use the same APIs described in the rest of this README. |
62 |
| - |
63 |
| -## Counter |
64 |
| - |
65 |
| -Counters go up (they can only increase in value), and reset when the process restarts. |
66 |
| - |
67 |
| -```swift |
68 |
| -let counter = myProm.createCounter(forType: Int.self, named: "my_counter") |
69 |
| -counter.inc() // Increment by 1 |
70 |
| -counter.inc(12) // Increment by given value |
71 |
| -``` |
72 |
| - |
73 |
| -## Gauge |
74 |
| - |
75 |
| -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. |
76 |
| - |
77 |
| -```swift |
78 |
| -let gauge = myProm.createGauge(forType: Int.self, named: "my_gauge") |
79 |
| -gauge.inc() // Increment by 1 |
80 |
| -gauge.dec(19) // Decrement by given value |
81 |
| -gauge.set(12) // Set to a given value |
82 |
| -``` |
83 |
| - |
84 |
| -## Histogram |
85 |
| - |
86 |
| -Histograms track the size and number of events in buckets. This allows for aggregatable calculation of quantiles. |
87 |
| - |
88 |
| -```swift |
89 |
| -let histogram = myProm.createHistogram(forType: Double.self, named: "my_histogram") |
90 |
| -histogram.observe(4.7) // Observe the given value |
91 |
| -``` |
92 |
| - |
93 |
| -## Summary |
94 |
| - |
95 |
| -Summaries track the size and number of events |
96 |
| - |
97 |
| -```swift |
98 |
| -let summary = myProm.createSummary(forType: Double.self, named: "my_summary") |
99 |
| -summary.observe(4.7) // Observe the given value |
100 |
| -``` |
101 |
| - |
102 |
| -## Labels |
103 |
| -All metric types support adding labels, allowing for grouping of related metrics. Labels are passed when recording values to your metric as an instance of `DimensionLabels`, or as an array of `(String, String)`. |
104 |
| - |
105 |
| -Example with a counter: |
106 |
| - |
107 |
| -```swift |
108 |
| -let counter = myProm.createCounter(forType: Int.self, named: "my_counter", helpText: "Just a counter") |
109 |
| - |
110 |
| -let counter = prom.createCounter(forType: Int.self, named: "my_counter", helpText: "Just a counter") |
111 |
| - |
112 |
| -counter.inc(12, .init([("route", "/users")])) |
113 |
| -// OR |
114 |
| -counter.inc(12, [("route", "/users")]) |
115 |
| -``` |
116 |
| - |
117 |
| -# Exporting |
118 |
| - |
119 |
| -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. |
120 |
| - |
121 |
| -By default, this should be accessible on your main serving port, at the `/metrics` endpoint. An example in [Vapor](https://vapor.codes) 4 syntax looks like: |
122 |
| - |
123 |
| -```swift |
124 |
| -app.get("metrics") { req async throws -> String in |
125 |
| - return try await MetricsSystem.prometheus().collect() |
126 |
| -} |
127 |
| -``` |
| 6 | +A prometheus client for Swift supporting counters, gauges and histograms. Swift Prometheus |
| 7 | +implements the Swift Metrics API. |
128 | 8 |
|
129 | 9 | ## Security
|
130 | 10 |
|
131 | 11 | Please see [SECURITY.md](SECURITY.md) for details on the security process.
|
132 | 12 |
|
133 |
| -# Contributing |
| 13 | +## Contributing |
134 | 14 |
|
135 | 15 | All contributions are most welcome!
|
136 | 16 |
|
137 |
| -If you think of some cool new feature that should be included, please [create an issue](https://github.com/swift-server-community/SwiftPrometheus/issues/new/choose). Or, if you want to implement it yourself, [fork this repo](https://github.com/swift-server-community/SwiftPrometheus/fork) and submit a PR! |
| 17 | +If you think of some cool new feature that should be included, please [create an issue](https://github.com/swift-server/swift-prometheus/issues/new). |
| 18 | +Or, if you want to implement it yourself, [fork this repo](https://github.com/swift-server/swift-prometheus/fork) and submit a PR! |
| 19 | + |
| 20 | +If you find a bug or have issues, please [create an issue](https://github.com/swift-server-community/SwiftPrometheus/issues/new) explaining your problems. Please include as much information as possible, so it's easier for us to reproduce (Framework, OS, Swift version, terminal output, etc.) |
138 | 21 |
|
139 |
| -If you find a bug or have issues, please [create an issue](https://github.com/swift-server-community/SwiftPrometheus/issues/new/choose) explaining your problems. Please include as much information as possible, so it's easier for me to reproduce (Framework, OS, Swift version, terminal output, etc.) |
| 22 | +[Documentation]: https://swiftpackageindex.com/swift-server/swift-prometheus/documentation/prometheus |
| 23 | +[SSWG-Incubation]: https://www.swift.org/sswg/incubation-process.html |
0 commit comments