File tree Expand file tree Collapse file tree 2 files changed +61
-7
lines changed Expand file tree Collapse file tree 2 files changed +61
-7
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -19,13 +19,8 @@ const (
1919 metricsAddr = ":8081"
2020)
2121
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.
22+ // This example will show how you could use opencensus with go-http-middleware
23+ // and serve the metrics in Prometheus format (through OpenCensus).
2924func main () {
3025 // Create OpenCensus with Prometheus.
3126 ocexporter , err := ocprometheus .NewExporter (ocprometheus.Options {})
You can’t perform that action at this time.
0 commit comments