Skip to content

Commit 0ca7d47

Browse files
authored
Merge pull request #16 from slok/opencensus
Add OpenCensus implementation
2 parents 28d5e4a + 8ed45ff commit 0ca7d47

File tree

17 files changed

+769
-77
lines changed

17 files changed

+769
-77
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/gin/main.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
"os"
7+
"os/signal"
8+
"syscall"
9+
10+
"github.com/gin-gonic/gin"
11+
"github.com/prometheus/client_golang/prometheus/promhttp"
12+
prommiddleware "github.com/slok/go-prometheus-middleware"
13+
promgin "github.com/slok/go-prometheus-middleware/gin"
14+
)
15+
16+
const (
17+
srvAddr = ":8080"
18+
metricsAddr = ":8081"
19+
)
20+
21+
func main() {
22+
// Create our middleware.
23+
mdlw := prommiddleware.NewDefault()
24+
25+
// Create our gin instance.
26+
r := gin.New()
27+
28+
// Add the middlewares to all gin routes.
29+
r.Use(
30+
promgin.Handler("", mdlw),
31+
gin.Logger(),
32+
)
33+
34+
// Add our handler
35+
r.GET("/", func(c *gin.Context) {
36+
c.String(http.StatusOK, "Hello world!")
37+
})
38+
39+
// Serve our handler.
40+
go func() {
41+
log.Printf("server listening at %s", srvAddr)
42+
if err := r.Run(srvAddr); err != nil {
43+
log.Panicf("error while serving: %s", err)
44+
}
45+
}()
46+
47+
// Serve our metrics.
48+
go func() {
49+
log.Printf("metrics listening at %s", metricsAddr)
50+
if err := http.ListenAndServe(metricsAddr, promhttp.Handler()); err != nil {
51+
log.Panicf("error while serving metrics: %s", err)
52+
}
53+
}()
54+
55+
// Wait until some signal is captured.
56+
sigC := make(chan os.Signal, 1)
57+
signal.Notify(sigC, syscall.SIGTERM, syscall.SIGINT)
58+
<-sigC
59+
}

examples/opencensus/main.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 how you could use opencensus with go-http-middleware
23+
// and serve the metrics in Prometheus format (through OpenCensus).
24+
func main() {
25+
// Create OpenCensus with Prometheus.
26+
ocexporter, err := ocprometheus.NewExporter(ocprometheus.Options{})
27+
if err != nil {
28+
log.Panicf("error creating OpenCensus exporter: %s", err)
29+
}
30+
view.RegisterExporter(ocexporter)
31+
rec, err := ocmmetrics.NewRecorder(ocmmetrics.Config{})
32+
if err != nil {
33+
log.Panicf("error creating OpenCensus metrics recorder: %s", err)
34+
}
35+
36+
// Create our middleware.
37+
mdlw := middleware.New(middleware.Config{
38+
Recorder: rec,
39+
})
40+
41+
// Create our server.
42+
mux := http.NewServeMux()
43+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
44+
time.Sleep(200 * time.Millisecond)
45+
w.WriteHeader(http.StatusOK)
46+
})
47+
mux.HandleFunc("/test1", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) })
48+
mux.HandleFunc("/test1/test2", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusAccepted) })
49+
mux.HandleFunc("/test1/test4", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNonAuthoritativeInfo) })
50+
mux.HandleFunc("/test2", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) })
51+
mux.HandleFunc("/test3", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusResetContent) })
52+
53+
// Wrap our main handler, we pass empty handler ID so the middleware inferes
54+
// the handler label from the URL.
55+
h := mdlw.Handler("", mux)
56+
57+
// Serve our handler.
58+
go func() {
59+
log.Printf("server listening at %s", srvAddr)
60+
if err := http.ListenAndServe(srvAddr, h); err != nil {
61+
log.Panicf("error while serving: %s", err)
62+
}
63+
}()
64+
65+
// Serve our metrics.
66+
go func() {
67+
log.Printf("metrics listening at %s", metricsAddr)
68+
if err := http.ListenAndServe(metricsAddr, ocexporter); err != nil {
69+
log.Panicf("error while serving metrics: %s", err)
70+
}
71+
}()
72+
73+
// Wait until some signal is captured.
74+
sigC := make(chan os.Signal, 1)
75+
signal.Notify(sigC, syscall.SIGTERM, syscall.SIGINT)
76+
<-sigC
77+
}

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)