Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
## 0.3.0 / 2019-03-24
# Changelog

* [FEATURE] Add inflight requests metric per handler.
## [Unreleased]

## 0.2.0 / 2019-03-22
### Breaking changes
* The Recorder methods now receive a context argument.

* [FEATURE] Add metrics of HTTP response size in bytes.
* [ENHANCEMENT] Make the label names of Prometheus recorder configurable.
### Added
* OpenCensus recorder implementation.

## 0.1.0 / 2019-03-18
## [0.3.0] - 2019-03-24

* [FEATURE] Add gorestful compatible middleware.
* [FEATURE] Add httprouter compatible middleware.
* [FEATURE] Add Negroni compatible middleware.
* [FEATURE] Add option to group by status codes.
* [FEATURE] Add predefined handler label.
* [FEATURE] Add URL infered handler label.
* [FEATURE] Add middleware.
* [FEATURE] Add HTTP latency requests.
* [FEATURE] Add Prometheus recorder.
### Added
* Inflight requests metric per handler.

## [0.2.0] - 2019-03-22

### Added
* Metrics of HTTP response size in bytes.
* Make the label names of Prometheus recorder configurable.

## [0.1.0] - 2019-03-18

### Added
* Gorestful compatible middleware.
* Httprouter compatible middleware.
* Negroni compatible middleware.
* Option to group by status codes.
* Predefined handler label.
* URL infered handler label.
* Middleware.
* HTTP latency requests.
* Prometheus recorder.


[unreleased]: https://github.com/slok/go-http-metrics/compare/v0.3.0...HEAD
[0.3.0]: https://github.com/slok/go-http-metrics/compare/v0.2.0...v0.3.0
[0.2.0]: https://github.com/slok/go-http-metrics/compare/v0.1.0...v0.2.0
[0.1.0]: https://github.com/slok/go-http-metrics/releases/tag/v0.1.0
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The metrics obtained with this middleware are the [most important ones][red] for
go-http-metrics is easy to extend to different metric backends by implementing `metrics.Recorder` interface.

- [Prometheus][prometheus-recorder]
- [OpenCensus][opencensus-recorder]

## Framework compatibility middlewares

Expand Down Expand Up @@ -203,3 +204,4 @@ BenchmarkMiddlewareHandler/benchmark_with_predefined_handler_ID-4 1000000
[httprouter-example]: examples/httprouter
[gorestful-example]: examples/gorestful
[prometheus-recorder]: metrics/prometheus
[opencensus-recorder]: metrics/opencensus
59 changes: 59 additions & 0 deletions examples/gin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

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

"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"
prommiddleware "github.com/slok/go-prometheus-middleware"
promgin "github.com/slok/go-prometheus-middleware/gin"
)

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

func main() {
// Create our middleware.
mdlw := prommiddleware.NewDefault()

// Create our gin instance.
r := gin.New()

// Add the middlewares to all gin routes.
r.Use(
promgin.Handler("", mdlw),
gin.Logger(),
)

// Add our handler
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Hello world!")
})

// Serve our handler.
go func() {
log.Printf("server listening at %s", srvAddr)
if err := r.Run(srvAddr); 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
}
77 changes: 77 additions & 0 deletions examples/opencensus/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

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

ocmmetrics "github.com/slok/go-http-metrics/metrics/opencensus"
"github.com/slok/go-http-metrics/middleware"
ocprometheus "go.opencensus.io/exporter/prometheus"
"go.opencensus.io/stats/view"
)

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

// This example will show how you could use opencensus with go-http-middleware
// and serve the metrics in Prometheus format (through OpenCensus).
func main() {
// Create OpenCensus with Prometheus.
ocexporter, err := ocprometheus.NewExporter(ocprometheus.Options{})
if err != nil {
log.Panicf("error creating OpenCensus exporter: %s", err)
}
view.RegisterExporter(ocexporter)
rec, err := ocmmetrics.NewRecorder(ocmmetrics.Config{})
if err != nil {
log.Panicf("error creating OpenCensus metrics recorder: %s", err)
}

// Create our middleware.
mdlw := middleware.New(middleware.Config{
Recorder: rec,
})

// 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 our main handler, we pass empty handler ID so the middleware inferes
// the handler label from the URL.
h := mdlw.Handler("", 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, ocexporter); 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
}
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ module github.com/slok/go-http-metrics

require (
github.com/emicklei/go-restful v2.9.0+incompatible
github.com/gin-gonic/gin v1.3.0
github.com/golang/mock v1.2.0 // indirect
github.com/json-iterator/go v1.1.6 // indirect
github.com/julienschmidt/httprouter v1.2.0
github.com/prometheus/client_golang v0.9.2
github.com/kr/pretty v0.1.0 // indirect
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829
github.com/slok/go-prometheus-middleware v0.4.0
github.com/stretchr/testify v1.2.2
github.com/urfave/negroni v1.0.0
go.opencensus.io v0.19.2
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
)
Loading