Skip to content

Commit

Permalink
Update comment in OpenCensus example
Browse files Browse the repository at this point in the history
Signed-off-by: Xabier Larrakoetxea <[email protected]>
  • Loading branch information
slok committed Mar 27, 2019
1 parent 12e2969 commit a28f76c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
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
}
9 changes: 2 additions & 7 deletions examples/opencensus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ const (
metricsAddr = ":8081"
)

// this example will show the simplest way of enabling the middleware
// using go std handlers without frameworks with the Prometheues recorder.
// it uses the prometheus default registry, the middleware default options
// and doesn't set the handler ID, so the middleware will set the handler id
// (handler label) to the request URL, WARNING: this creates high cardinality because
// `/profile/123` and `/profile/567` are different handlers. If you want to be safer
// you will need to tell the middleware what is the handler id.
// 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{})
Expand Down

0 comments on commit a28f76c

Please sign in to comment.