Skip to content

Commit

Permalink
Merge pull request #42 from slok/slok/std-provider
Browse files Browse the repository at this point in the history
Add handler provider for multiple frameworks and support chi, alice and gorilla frameworks
  • Loading branch information
slok authored Oct 3, 2020
2 parents 4013ab5 + c540391 commit 6b820cf
Show file tree
Hide file tree
Showing 13 changed files with 440 additions and 56 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ jobs:
name: Check
runs-on: ubuntu-latest
# Execute the checks inside the container instead the VM.
container: golangci/golangci-lint:v1.27.0-alpine
container: golangci/golangci-lint:v1.31.0-alpine
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- run: golangci-lint run -E goimports

unit-test:
name: Unit test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- uses: actions/setup-go@v1
with:
go-version: 1.14
go-version: 1.15
- run: make test

integration-test:
name: Integration test
runs-on: ubuntu-latest
needs: [check, unit-test]
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- uses: actions/setup-go@v1
with:
go-version: 1.14
go-version: 1.15
- run: make integration-test
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## [Unreleased]

### Added

- New helper method to get an `std` HTTP provider (`std.HandlerProvider`) (used by various frameworks like Gorilla).
- Support Chi library.
- Support Alice library.
- Support Gorilla library.

## [0.8.0] - 2020-06-04

### Added
Expand Down Expand Up @@ -92,7 +99,7 @@ Breaking change: The library has been refactored to be more flexible when adding
- Prometheus recorder.

[unreleased]: https://github.com/slok/go-http-metrics/compare/v0.8.0...HEAD
[0.8.0]: https://github.com/slok/go-http-metrics/compare/v0.7.0...v0.8.0
[0.8.0]: https://github.com/slok/go-http-metrics/compare/v0.7.0...v0.8.0
[0.7.0]: https://github.com/slok/go-http-metrics/compare/v0.6.1...v0.7.0
[0.6.1]: https://github.com/slok/go-http-metrics/compare/v0.6.0...v0.6.1
[0.6.0]: https://github.com/slok/go-http-metrics/compare/v0.5.0...v0.6.0
Expand Down
13 changes: 11 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ The middleware is mainly focused to be compatible with Go std library using http
- [Gin][gin-example]
- [Echo][echo-example]
- [Goji][goji-example]
- [Chi][chi-example]
- [Alice][alice-example]
- [gorilla][gorilla-example]

It supports any framework that supports http.Handler provider type middleware `func(http.Handler) http.Handler` (e.g Chi, Alice, Gorilla...). Use [`std.HandlerProvider`][handler-provider-docs]

## Getting Started

Expand Down Expand Up @@ -200,8 +205,8 @@ This Option is used to unregister the Recorder views before are being registered
[github-actions-url]: https://github.com/slok/go-http-metrics/actions
[goreport-image]: https://goreportcard.com/badge/github.com/slok/go-http-metrics
[goreport-url]: https://goreportcard.com/report/github.com/slok/go-http-metrics
[godoc-image]: https://godoc.org/github.com/slok/go-http-metrics?status.svg
[godoc-url]: https://godoc.org/github.com/slok/go-http-metrics
[godoc-image]: https://pkg.go.dev/badge/github.com/slok/go-http-metrics
[godoc-url]: https://pkg.go.dev/github.com/slok/go-http-metrics
[docs]: https://godoc.org/github.com/slok/go-http-metrics
[examples]: examples/
[red]: https://www.weave.works/blog/the-red-method-key-metrics-for-microservices-architecture/
Expand All @@ -214,5 +219,9 @@ This Option is used to unregister the Recorder views before are being registered
[gin-example]: examples/gin
[echo-example]: examples/echo
[goji-example]: examples/goji
[chi-example]: examples/chi
[alice-example]: examples/alice
[gorilla-example]: examples/gorilla
[prometheus-recorder]: metrics/prometheus
[opencensus-recorder]: metrics/opencensus
[handler-provider-docs]: https://pkg.go.dev/github.com/slok/go-http-metrics/middleware/std#HandlerProvider
64 changes: 64 additions & 0 deletions examples/alice/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/justinas/alice"
"github.com/prometheus/client_golang/prometheus/promhttp"
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
"github.com/slok/go-http-metrics/middleware"
"github.com/slok/go-http-metrics/middleware/std"
)

const (
srvAddr = ":8080"
metricsAddr = ":8081"
)

func main() {
// Create our middleware.
mdlw := middleware.New(middleware.Config{
Recorder: metrics.NewRecorder(metrics.Config{}),
})

// Create our server.
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(200 * time.Millisecond)
w.WriteHeader(http.StatusOK)
})
mux.HandleFunc("/test1", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) })
mux.HandleFunc("/test1/test2", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusAccepted) })
mux.HandleFunc("/test1/test4", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNonAuthoritativeInfo) })
mux.HandleFunc("/test2", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) })
mux.HandleFunc("/test3", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusResetContent) })

// Wrap with middleware.
h := alice.New(std.HandlerProvider("", mdlw)).Then(mux)

// Serve our handler.
go func() {
log.Printf("server listening at %s", srvAddr)
if err := http.ListenAndServe(srvAddr, h); err != nil {
log.Panicf("error while serving: %s", err)
}
}()

// Serve our metrics.
go func() {
log.Printf("metrics listening at %s", metricsAddr)
if err := http.ListenAndServe(metricsAddr, promhttp.Handler()); err != nil {
log.Panicf("error while serving metrics: %s", err)
}
}()

// Wait until some signal is captured.
sigC := make(chan os.Signal, 1)
signal.Notify(sigC, syscall.SIGTERM, syscall.SIGINT)
<-sigC
}
64 changes: 64 additions & 0 deletions examples/chi/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/go-chi/chi"
"github.com/prometheus/client_golang/prometheus/promhttp"
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
"github.com/slok/go-http-metrics/middleware"
"github.com/slok/go-http-metrics/middleware/std"
)

const (
srvAddr = ":8080"
metricsAddr = ":8081"
)

func main() {
// Create our middleware.
mdlw := middleware.New(middleware.Config{
Recorder: metrics.NewRecorder(metrics.Config{}),
})

// Create our router with the metrics middleware.
r := chi.NewRouter()
r.Use(std.HandlerProvider("", mdlw))

// Add paths.
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(200 * time.Millisecond)
w.WriteHeader(http.StatusOK)
})
r.Get("/test1", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) })
r.Get("/test1/test2", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusAccepted) })
r.Get("/test1/test4", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNonAuthoritativeInfo) })
r.Get("/test2", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) })
r.Get("/test3", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusResetContent) })

// Serve our handler.
go func() {
log.Printf("server listening at %s", srvAddr)
if err := http.ListenAndServe(srvAddr, r); err != nil {
log.Panicf("error while serving: %s", err)
}
}()

// Serve our metrics.
go func() {
log.Printf("metrics listening at %s", metricsAddr)
if err := http.ListenAndServe(metricsAddr, promhttp.Handler()); err != nil {
log.Panicf("error while serving metrics: %s", err)
}
}()

// Wait until some signal is captured.
sigC := make(chan os.Signal, 1)
signal.Notify(sigC, syscall.SIGTERM, syscall.SIGINT)
<-sigC
}
64 changes: 64 additions & 0 deletions examples/gorilla/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
"github.com/slok/go-http-metrics/middleware"
"github.com/slok/go-http-metrics/middleware/std"
)

const (
srvAddr = ":8080"
metricsAddr = ":8081"
)

func main() {
// Create our middleware.
mdlw := middleware.New(middleware.Config{
Recorder: metrics.NewRecorder(metrics.Config{}),
})

// Create our router with the metrics middleware.
r := mux.NewRouter()
r.Use(std.HandlerProvider("", mdlw))

// Add paths.
r.Methods("GET").Path("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(200 * time.Millisecond)
w.WriteHeader(http.StatusOK)
})
r.Methods("GET").Path("/test1").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) })
r.Methods("GET").Path("/test1/test2").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusAccepted) })
r.Methods("GET").Path("/test1/test4").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNonAuthoritativeInfo) })
r.Methods("GET").Path("/test2").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) })
r.Methods("GET").Path("/test3").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusResetContent) })

// Serve our handler.
go func() {
log.Printf("server listening at %s", srvAddr)
if err := http.ListenAndServe(srvAddr, r); err != nil {
log.Panicf("error while serving: %s", err)
}
}()

// Serve our metrics.
go func() {
log.Printf("metrics listening at %s", metricsAddr)
if err := http.ListenAndServe(metricsAddr, promhttp.Handler()); err != nil {
log.Panicf("error while serving metrics: %s", err)
}
}()

// Wait until some signal is captured.
sigC := make(chan os.Signal, 1)
signal.Notify(sigC, syscall.SIGTERM, syscall.SIGINT)
<-sigC
}
17 changes: 10 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
module github.com/slok/go-http-metrics

require (
contrib.go.opencensus.io/exporter/prometheus v0.1.0
github.com/emicklei/go-restful v2.12.0+incompatible
contrib.go.opencensus.io/exporter/prometheus v0.2.0
github.com/emicklei/go-restful v2.14.2+incompatible
github.com/gin-gonic/gin v1.6.3
github.com/go-chi/chi v4.1.2+incompatible
github.com/gorilla/mux v1.8.0
github.com/julienschmidt/httprouter v1.3.0
github.com/labstack/echo/v4 v4.1.16
github.com/prometheus/client_golang v1.6.0
github.com/stretchr/testify v1.6.0
github.com/justinas/alice v1.2.0
github.com/labstack/echo/v4 v4.1.17
github.com/prometheus/client_golang v1.7.1
github.com/stretchr/testify v1.6.1
github.com/urfave/negroni v1.0.0
go.opencensus.io v0.22.3
go.opencensus.io v0.22.4
goji.io v2.0.2+incompatible
)

go 1.14
go 1.15
Loading

0 comments on commit 6b820cf

Please sign in to comment.