|
| 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 | +} |
0 commit comments