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