Skip to content

Commit

Permalink
Merge pull request #9 from Yasumoto/one-library
Browse files Browse the repository at this point in the history
πŸ‘―β€β™€οΈ Merge Prometheus and PrometheusMetrics targets into one library
  • Loading branch information
MrLotU authored Jul 13, 2019
2 parents 0d04bb8 + 645207e commit 955b9d4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 48 deletions.
12 changes: 6 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ let package = Package(
.library(
name: "SwiftPrometheus",
targets: ["Prometheus"]),
.executable(
name: "PrometheusExample",
targets: ["PrometheusExample"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-metrics.git", from: "1.0.0"),
Expand All @@ -16,15 +19,12 @@ let package = Package(
targets: [
.target(
name: "Prometheus",
dependencies: ["NIOConcurrencyHelpers"]),
.target(
name: "PrometheusMetrics",
dependencies: ["Prometheus", "CoreMetrics"]),
dependencies: ["CoreMetrics", "NIOConcurrencyHelpers"]),
.target(
name: "PrometheusExample",
dependencies: ["PrometheusMetrics", "Metrics"]),
dependencies: ["Prometheus", "Metrics"]),
.testTarget(
name: "SwiftPrometheusTests",
dependencies: ["Prometheus", "PrometheusMetrics"]),
dependencies: ["Prometheus"]),
]
)
58 changes: 20 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,50 @@ A prometheus client for Swift supporting counters, gauges, histograms, summaries

# Usage

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

First, we have to create an instance of our `PrometheusClient`:

```swift
import Prometheus
let myProm = PrometheusClient()
```

## Usage with Swift-Metrics
_For more details about swift-metrics, check the GitHub repo [here](https://github.com/apple/swift-metrics)_
## Usage with SwiftMetrics
_For more details about swift-metrics, please view [the GitHub repo](https://github.com/apple/swift-metrics)._

To use SwiftPrometheus with swift-metrics, you need to configure the backend inside the `MetricsSystem`:

To use SwiftPrometheus with swift-metrics, all the setup required is this:
```swift
import Metrics
import Prometheus
import PrometheusMetrics
let myProm = PrometheusClient()
MetricsSystem.bootstrap(myProm)
```

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

```swift
// This is the same instance was used in `.bootstrap()` earlier.
// This returns the same instance passed in to `.bootstrap()` earlier.
let promInstance = try MetricsSystem.prometheus()
print(promInstance.collect())
```
You can than use the same APIs that are layed out in the rest of this README

You can then use the same APIs described in the rest of this README.

## Counter

Counters go up, and reset when the process restarts.
Counters go up (they can only increase in value), and reset when the process restarts.

```swift
let counter = myProm.createCounter(forType: Int.self, named: "my_counter")
counter.inc() // Increment by 1
counter.inc(12) // Increment by given value
counter.inc(12) // Increment by given value
```

## Gauge

Gauges can go up and down
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.

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

## Info

Info tracks key-value information, usually about a whole target.

```swift
struct MyInfoStruct: MetricLabels {
let value: String

init() {
self.value = "abc"
}

init(_ v: String) {
self.value = v
}
}

let info = myProm.createInfo(named: "my_info", helpText: "Just some info", labelType: MyInfoStruct.self)

let info = prom.createInfo(named: "my_info", helpText: "Just some info", labelType: MyInfoStruct.self)

info.info(MyInfoStruct("def"))
```

## Labels
All metric types support adding labels, allowing for grouping of related metrics.

Example with a counter:

```swift
struct RouteLabels: MetricLabels {
var route: String = "*"
Expand All @@ -113,15 +95,15 @@ counter.inc(12, .init(route: "/"))

# Exporting

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

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:

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

# Contributing

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Prometheus
import CoreMetrics

private class MetricsCounter: CounterHandler {
Expand Down
2 changes: 0 additions & 2 deletions Sources/PrometheusExample/main.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import Prometheus
import Metrics
import PrometheusMetrics
import Foundation

let myProm = PrometheusClient()

Expand Down
1 change: 0 additions & 1 deletion Tests/SwiftPrometheusTests/PrometheusMetricsTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import XCTest
@testable import Prometheus
@testable import CoreMetrics
@testable import PrometheusMetrics

final class PrometheusMetricsTests: XCTestCase {

Expand Down

0 comments on commit 955b9d4

Please sign in to comment.