Swift Prometheus: Client-side Prometheus implementation in Swift
This module, Prometheus
, would include Prometheus metric types for developers to create and use, to serve prometheus formatted metrics any way they want.
Pitch:
A low level Client-Side Prometheus implementation.
Motivation:
A lot of companies and employers don't want their serverside applications running without any sort of insight whatsoever. Prometheus is a Monitoring system used by many big names out there to keep track of what their applications are doing.
This package is a Client-Side implementation of Prometheus, allowing you to serve up Prometheus formatted data that a Server-Side Prometheus application can read out, and store.
There currently are some swift solutions out there that provide Prometheus monitoring, for example SwiftMetrics, but none of them are framework independent, and none of them expose the ability for the end user to add metrics of their own without editing package source.
API Design
Note: All of this is still in early stages, and might very well change based on community input
The Swift Prometheus API is designed to be simple. It has no dependencies, only uses Swift builtin types, and requires no setup.
Using prometheus in your project is as simple as this:
import Prometheus
let counter = Prometheus.shared.createCounter(forType: Int.self, named: "my_counter")
counter.inc() // Increment value by 1
// OR use counter.inc(12) to increment the counter by a given value
// To get a hold of your metrics
Prometheus.shared.getMetrics()
In this case, only incrementing the counter once, with a value of 1, this will give the follwing output:
# TYPE my_counter counter
my_counter 1
Also supported are help texts, that can be used to clarify what metric holds what:
import Prometheus
let counter = Prometheus.shared.createCounter(forType: Int.self, named: "my_counter", helpText: "Counts up")
counter.inc()
Prometheus.shared.getMetrics()
which will result in:
# HELP my_counter Counts up
# TYPE my_counter counter
my_counter 1
An example of how this could end up looking in an application (using Vapor syntax here, but framework should not matter)
import Prometheus
router.get("/status") { req -> String in
return Prometheus.shared.getMetrics()
}
Metrics currently supported are:
- Counter
- Gauge
- Histogram
- Summary
- Info
To check out the implementations, and full overview of the module, it's on GitHub here
Into the future
Right now, the API I created has some quirks and code I'm not fully happy with. I'll work on cleaning these flaws up, but wanted to share this here already, since the API is functional. Next to that, the code is only partially documented, and has no tests yet. I'll also provide those over the coming week(s)
Next to changes to this module I'll preform, I think it'd be a nice to have, to have default implementations on a per framework basis, covering metrics that most anyone would want, like response times, system information, and other defaults.
Thanks for reading