Skip to content

Commit 12e2969

Browse files
committed
Update Readme and changelog
Signed-off-by: Xabier Larrakoetxea <[email protected]>
1 parent 62199ce commit 12e2969

File tree

7 files changed

+654
-23
lines changed

7 files changed

+654
-23
lines changed

CHANGELOG

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
1-
## 0.3.0 / 2019-03-24
1+
# Changelog
22

3-
* [FEATURE] Add inflight requests metric per handler.
3+
## [Unreleased]
44

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

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

10-
## 0.1.0 / 2019-03-18
11+
## [0.3.0] - 2019-03-24
1112

12-
* [FEATURE] Add gorestful compatible middleware.
13-
* [FEATURE] Add httprouter compatible middleware.
14-
* [FEATURE] Add Negroni compatible middleware.
15-
* [FEATURE] Add option to group by status codes.
16-
* [FEATURE] Add predefined handler label.
17-
* [FEATURE] Add URL infered handler label.
18-
* [FEATURE] Add middleware.
19-
* [FEATURE] Add HTTP latency requests.
20-
* [FEATURE] Add Prometheus recorder.
13+
### Added
14+
* Inflight requests metric per handler.
15+
16+
## [0.2.0] - 2019-03-22
17+
18+
### Added
19+
* Metrics of HTTP response size in bytes.
20+
* Make the label names of Prometheus recorder configurable.
21+
22+
## [0.1.0] - 2019-03-18
23+
24+
### Added
25+
* Gorestful compatible middleware.
26+
* Httprouter compatible middleware.
27+
* Negroni compatible middleware.
28+
* Option to group by status codes.
29+
* Predefined handler label.
30+
* URL infered handler label.
31+
* Middleware.
32+
* HTTP latency requests.
33+
* Prometheus recorder.
34+
35+
36+
[unreleased]: https://github.com/slok/go-http-metrics/compare/v0.3.0...HEAD
37+
[0.3.0]: https://github.com/slok/go-http-metrics/compare/v0.2.0...v0.3.0
38+
[0.2.0]: https://github.com/slok/go-http-metrics/compare/v0.1.0...v0.2.0
39+
[0.1.0]: https://github.com/slok/go-http-metrics/releases/tag/v0.1.0

Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ The metrics obtained with this middleware are the [most important ones][red] for
3939
go-http-metrics is easy to extend to different metric backends by implementing `metrics.Recorder` interface.
4040

4141
- [Prometheus][prometheus-recorder]
42+
- [OpenCensus][opencensus-recorder]
4243

4344
## Framework compatibility middlewares
4445

@@ -203,3 +204,4 @@ BenchmarkMiddlewareHandler/benchmark_with_predefined_handler_ID-4 1000000
203204
[httprouter-example]: examples/httprouter
204205
[gorestful-example]: examples/gorestful
205206
[prometheus-recorder]: metrics/prometheus
207+
[opencensus-recorder]: metrics/opencensus

examples/opencensus/main.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
"os"
7+
"os/signal"
8+
"syscall"
9+
"time"
10+
11+
ocmmetrics "github.com/slok/go-http-metrics/metrics/opencensus"
12+
"github.com/slok/go-http-metrics/middleware"
13+
ocprometheus "go.opencensus.io/exporter/prometheus"
14+
"go.opencensus.io/stats/view"
15+
)
16+
17+
const (
18+
srvAddr = ":8080"
19+
metricsAddr = ":8081"
20+
)
21+
22+
// this example will show the simplest way of enabling the middleware
23+
// using go std handlers without frameworks with the Prometheues recorder.
24+
// it uses the prometheus default registry, the middleware default options
25+
// and doesn't set the handler ID, so the middleware will set the handler id
26+
// (handler label) to the request URL, WARNING: this creates high cardinality because
27+
// `/profile/123` and `/profile/567` are different handlers. If you want to be safer
28+
// you will need to tell the middleware what is the handler id.
29+
func main() {
30+
// Create OpenCensus with Prometheus.
31+
ocexporter, err := ocprometheus.NewExporter(ocprometheus.Options{})
32+
if err != nil {
33+
log.Panicf("error creating OpenCensus exporter: %s", err)
34+
}
35+
view.RegisterExporter(ocexporter)
36+
rec, err := ocmmetrics.NewRecorder(ocmmetrics.Config{})
37+
if err != nil {
38+
log.Panicf("error creating OpenCensus metrics recorder: %s", err)
39+
}
40+
41+
// Create our middleware.
42+
mdlw := middleware.New(middleware.Config{
43+
Recorder: rec,
44+
})
45+
46+
// Create our server.
47+
mux := http.NewServeMux()
48+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
49+
time.Sleep(200 * time.Millisecond)
50+
w.WriteHeader(http.StatusOK)
51+
})
52+
mux.HandleFunc("/test1", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) })
53+
mux.HandleFunc("/test1/test2", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusAccepted) })
54+
mux.HandleFunc("/test1/test4", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNonAuthoritativeInfo) })
55+
mux.HandleFunc("/test2", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) })
56+
mux.HandleFunc("/test3", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusResetContent) })
57+
58+
// Wrap our main handler, we pass empty handler ID so the middleware inferes
59+
// the handler label from the URL.
60+
h := mdlw.Handler("", mux)
61+
62+
// Serve our handler.
63+
go func() {
64+
log.Printf("server listening at %s", srvAddr)
65+
if err := http.ListenAndServe(srvAddr, h); err != nil {
66+
log.Panicf("error while serving: %s", err)
67+
}
68+
}()
69+
70+
// Serve our metrics.
71+
go func() {
72+
log.Printf("metrics listening at %s", metricsAddr)
73+
if err := http.ListenAndServe(metricsAddr, ocexporter); err != nil {
74+
log.Panicf("error while serving metrics: %s", err)
75+
}
76+
}()
77+
78+
// Wait until some signal is captured.
79+
sigC := make(chan os.Signal, 1)
80+
signal.Notify(sigC, syscall.SIGTERM, syscall.SIGINT)
81+
<-sigC
82+
}

go.mod

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ module github.com/slok/go-http-metrics
22

33
require (
44
github.com/emicklei/go-restful v2.9.0+incompatible
5+
github.com/gin-gonic/gin v1.3.0
6+
github.com/golang/mock v1.2.0 // indirect
7+
github.com/json-iterator/go v1.1.6 // indirect
58
github.com/julienschmidt/httprouter v1.2.0
6-
github.com/prometheus/client_golang v0.9.2
9+
github.com/kr/pretty v0.1.0 // indirect
10+
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829
11+
github.com/slok/go-prometheus-middleware v0.4.0
712
github.com/stretchr/testify v1.2.2
813
github.com/urfave/negroni v1.0.0
14+
go.opencensus.io v0.19.2
15+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
16+
gopkg.in/yaml.v2 v2.2.2 // indirect
917
)

0 commit comments

Comments
 (0)