-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from slok/opencensus
Add OpenCensus implementation
- Loading branch information
Showing
17 changed files
with
769 additions
and
77 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 |
---|---|---|
@@ -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 |
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
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,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 | ||
} |
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,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 | ||
} |
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
Oops, something went wrong.