-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2da8413
Showing
13 changed files
with
932 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: Go | ||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
jobs: | ||
test: | ||
name: Test | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Install go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: 1.16 | ||
|
||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Test | ||
run: make test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
vendor | ||
cover.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2022 Kaspar Birk | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
GOWORK=off | ||
|
||
build: | ||
@go build ./... | ||
|
||
cover: | ||
@go test -coverprofile=cover.out -covermode=atomic -coverpkg=./... ./... | ||
@go tool cover -html=cover.out -o cover.html | ||
|
||
test: | ||
@go test ./... -v | ||
|
||
doc: | ||
@godoc -http=:6060 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# promware [![Test Status](https://github.com/imbue11235/promware/workflows/Go/badge.svg)](https://github.com/imbue11235/promware/actions?query=workflow:Go) [![Go Reference](https://pkg.go.dev/badge/github.com/imbue11235/promware.svg)](https://pkg.go.dev/github.com/imbue11235/promware) | ||
|
||
> A simple, configurable middleware for recording request metrics with [Prometheus](https://github.com/prometheus/prometheus) | ||
## 🛠 Installation | ||
|
||
Make sure to have Go installed (Version `1.16` or higher). | ||
|
||
Install `promware` with `go get`: | ||
|
||
```sh | ||
$ go get -u github.com/imbue11235/promware | ||
``` | ||
|
||
## 💻 Usage | ||
|
||
### With standard library | ||
|
||
```go | ||
middleware := promware.Default() | ||
|
||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
fmt.Fprintln(w, "hello world") | ||
}) | ||
|
||
mux := http.NewServeMux() | ||
mux.Handle("/metrics", promhttp.Handler()) | ||
mux.Handle("/", middleware(handler)) | ||
|
||
http.ListenAndServe("<addr>", mux) | ||
``` | ||
|
||
### Options | ||
```go | ||
middleware := promware.New( | ||
// Skip depending on request data | ||
promware.WithSkipFunc(func(r *http.Request) bool { | ||
return r.URL.Path == "/something" | ||
}) | ||
|
||
// Set subsystem | ||
promware.WithSubsystem("my-subsystem") | ||
|
||
// Set namespace | ||
promware.WithNamespace("my-namespace") | ||
|
||
// Add request counter, set name | ||
promware.WithRequestCounter("total-requests") | ||
|
||
// Add latency histogram, set name and buckets | ||
promware.WithLatencyHistogram("request-latency", []float64{0.5, 1, 2}) | ||
) | ||
``` | ||
|
||
## 📜 License | ||
|
||
This project is licensed under the [MIT license](LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package promware | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"net/http" | ||
) | ||
|
||
const defaultMetricsPath = "/metrics" | ||
|
||
// SkipFunc allows for certain requests to be skipped | ||
// based on information obtained from the http.Request, e.g. URLPath. | ||
type SkipFunc func(r *http.Request) bool | ||
|
||
// collector holds information and collectors needed by prometheus | ||
type collector struct { | ||
namespace string | ||
subsystem string | ||
|
||
requestCounter *prometheus.CounterVec | ||
latencyHistogram *prometheus.HistogramVec | ||
|
||
shouldSkip SkipFunc | ||
} | ||
|
||
// newCollector creates a new collector with empty values | ||
func newCollector() *collector { | ||
return &collector{ | ||
namespace: "", | ||
subsystem: "", | ||
shouldSkip: func(r *http.Request) bool { | ||
// default skips on `/metrics` endpoint, which | ||
// is commonly used with Prometheus | ||
return r.URL.Path == defaultMetricsPath | ||
}, | ||
} | ||
} | ||
|
||
// apply adds options to the recorder | ||
func (c *collector) apply(options ...option) { | ||
for _, opt := range options { | ||
opt(c) | ||
} | ||
} | ||
|
||
// register registers all the prometheus.Collectors | ||
func (c *collector) register() error { | ||
collectors := []prometheus.Collector{ | ||
c.requestCounter, | ||
c.latencyHistogram, | ||
} | ||
|
||
for _, collector := range collectors { | ||
if collector == nil { | ||
continue | ||
} | ||
|
||
if err := prometheus.Register(collector); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// addObservationToLatencyHistogram adds an observation to the latency histogram collector | ||
func (c *collector) addObservationToLatencyHistogram(code, method, path string, seconds float64) { | ||
if c.latencyHistogram == nil { | ||
return | ||
} | ||
|
||
c.latencyHistogram.WithLabelValues(code, method, path).Observe(seconds) | ||
} | ||
|
||
// incrementRequestCounter increments the current request counter collector by 1 | ||
func (c *collector) incrementRequestCounter(code, method, path string) { | ||
if c.requestCounter == nil { | ||
return | ||
} | ||
|
||
c.requestCounter.WithLabelValues(code, method, path).Inc() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/imbue11235/promware" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
addr := ":8080" | ||
|
||
middleware := promware.Default() | ||
|
||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
fmt.Fprintln(w, "hello world") | ||
}) | ||
|
||
mux := http.NewServeMux() | ||
mux.Handle("/metrics", promhttp.Handler()) | ||
mux.Handle("/", middleware(handler)) | ||
|
||
log.Printf("Serving on addr %s", addr) | ||
|
||
http.ListenAndServe(addr, mux) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module github.com/imbue11235/promware | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/appleboy/gofight/v2 v2.1.2 // indirect | ||
github.com/prometheus/client_golang v1.12.2 | ||
github.com/stretchr/testify v1.8.0 // indirect | ||
) |
Oops, something went wrong.