Skip to content

Commit 955b9d4

Browse files
authored
Merge pull request #9 from Yasumoto/one-library
πŸ‘―β€β™€οΈ Merge Prometheus and PrometheusMetrics targets into one library
2 parents 0d04bb8 + 645207e commit 955b9d4

File tree

5 files changed

+26
-48
lines changed

5 files changed

+26
-48
lines changed

β€ŽPackage.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ let package = Package(
88
.library(
99
name: "SwiftPrometheus",
1010
targets: ["Prometheus"]),
11+
.executable(
12+
name: "PrometheusExample",
13+
targets: ["PrometheusExample"]),
1114
],
1215
dependencies: [
1316
.package(url: "https://github.com/apple/swift-metrics.git", from: "1.0.0"),
@@ -16,15 +19,12 @@ let package = Package(
1619
targets: [
1720
.target(
1821
name: "Prometheus",
19-
dependencies: ["NIOConcurrencyHelpers"]),
20-
.target(
21-
name: "PrometheusMetrics",
22-
dependencies: ["Prometheus", "CoreMetrics"]),
22+
dependencies: ["CoreMetrics", "NIOConcurrencyHelpers"]),
2323
.target(
2424
name: "PrometheusExample",
25-
dependencies: ["PrometheusMetrics", "Metrics"]),
25+
dependencies: ["Prometheus", "Metrics"]),
2626
.testTarget(
2727
name: "SwiftPrometheusTests",
28-
dependencies: ["Prometheus", "PrometheusMetrics"]),
28+
dependencies: ["Prometheus"]),
2929
]
3030
)

β€ŽREADME.md

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,50 @@ A prometheus client for Swift supporting counters, gauges, histograms, summaries
66

77
# Usage
88

9-
For examples, see [main.swift](./Sources/PrometheusExample/main.swift)
9+
To see a working demo, see [PrometheusExample](./Sources/PrometheusExample/main.swift).
1010

1111
First, we have to create an instance of our `PrometheusClient`:
12+
1213
```swift
1314
import Prometheus
1415
let myProm = PrometheusClient()
1516
```
1617

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`:
1922

20-
To use SwiftPrometheus with swift-metrics, all the setup required is this:
2123
```swift
24+
import Metrics
2225
import Prometheus
23-
import PrometheusMetrics
2426
let myProm = PrometheusClient()
2527
MetricsSystem.bootstrap(myProm)
2628
```
2729

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+
2932
```swift
30-
// This is the same instance was used in `.bootstrap()` earlier.
33+
// This returns the same instance passed in to `.bootstrap()` earlier.
3134
let promInstance = try MetricsSystem.prometheus()
35+
print(promInstance.collect())
3236
```
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.
3439

3540
## Counter
3641

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.
3843

3944
```swift
4045
let counter = myProm.createCounter(forType: Int.self, named: "my_counter")
4146
counter.inc() // Increment by 1
42-
counter.inc(12) // Increment by given value
47+
counter.inc(12) // Increment by given value
4348
```
4449

4550
## Gauge
4651

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.
4853

4954
```swift
5055
let gauge = myProm.createGauge(forType: Int.self, named: "my_gauge")
@@ -71,34 +76,11 @@ let summary = myProm.createSummary(forType: Double.self, named: "my_summary")
7176
summary.observe(4.7) // Observe the given value
7277
```
7378

74-
## Info
75-
76-
Info tracks key-value information, usually about a whole target.
77-
78-
```swift
79-
struct MyInfoStruct: MetricLabels {
80-
let value: String
81-
82-
init() {
83-
self.value = "abc"
84-
}
85-
86-
init(_ v: String) {
87-
self.value = v
88-
}
89-
}
90-
91-
let info = myProm.createInfo(named: "my_info", helpText: "Just some info", labelType: MyInfoStruct.self)
92-
93-
let info = prom.createInfo(named: "my_info", helpText: "Just some info", labelType: MyInfoStruct.self)
94-
95-
info.info(MyInfoStruct("def"))
96-
```
97-
9879
## Labels
9980
All metric types support adding labels, allowing for grouping of related metrics.
10081

10182
Example with a counter:
83+
10284
```swift
10385
struct RouteLabels: MetricLabels {
10486
var route: String = "*"
@@ -113,15 +95,15 @@ counter.inc(12, .init(route: "/"))
11395

11496
# Exporting
11597

116-
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:
117101

118-
This could look something like this:
119102
```swift
120103
router.get("/metrics") { request -> String in
121104
return myProm.collect()
122105
}
123106
```
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.
125107

126108
# Contributing
127109

β€ŽSources/PrometheusMetrics/PrometheusMetrics.swift renamed to β€ŽSources/Prometheus/PrometheusMetrics.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Prometheus
21
import CoreMetrics
32

43
private class MetricsCounter: CounterHandler {

β€ŽSources/PrometheusExample/main.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import Prometheus
22
import Metrics
3-
import PrometheusMetrics
4-
import Foundation
53

64
let myProm = PrometheusClient()
75

β€ŽTests/SwiftPrometheusTests/PrometheusMetricsTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import XCTest
22
@testable import Prometheus
33
@testable import CoreMetrics
4-
@testable import PrometheusMetrics
54

65
final class PrometheusMetricsTests: XCTestCase {
76

0 commit comments

Comments
Β (0)