From 292bc32602b48445d386741267edaa6db03145a7 Mon Sep 17 00:00:00 2001 From: Kemal Akkoyun Date: Mon, 7 Nov 2022 21:15:18 +0100 Subject: [PATCH] Remove usage of global registry from Go example Signed-off-by: Kemal Akkoyun --- docs/guides/go-application.md | 66 +++++++----- .../instrumenting_http_server_in_go.md | 102 +++++++++++------- 2 files changed, 104 insertions(+), 64 deletions(-) diff --git a/docs/guides/go-application.md b/docs/guides/go-application.md index a581d946..f1a70957 100644 --- a/docs/guides/go-application.md +++ b/docs/guides/go-application.md @@ -27,14 +27,21 @@ This minimal application, for example, would expose the default metrics for Go a package main import ( - "net/http" + "net/http" - "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/collectors" + "github.com/prometheus/client_golang/prometheus/promhttp" ) func main() { - http.Handle("/metrics", promhttp.Handler()) - http.ListenAndServe(":2112", nil) + reg := prometheus.NewRegistry() + reg.MustRegister( + collectors.NewGoCollector(), + collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), + ) + http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{})) + http.ListenAndServe(":2112", nil) } ``` @@ -58,35 +65,44 @@ The application [above](#how-go-exposition-works) exposes only the default Go me package main import ( - "net/http" - "time" + "net/http" + "time" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" - "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/prometheus/client_golang/prometheus/promhttp" ) -func recordMetrics() { - go func() { - for { - opsProcessed.Inc() - time.Sleep(2 * time.Second) - } - }() +type metrics struct { + opsProcessed prometheus.Counter } -var ( - opsProcessed = promauto.NewCounter(prometheus.CounterOpts{ - Name: "myapp_processed_ops_total", - Help: "The total number of processed events", - }) -) +func newMetrics(reg prometheus.Registerer) *metrics { + m := &metrics{ + opsProcessed: promauto.With(reg).NewCounter(prometheus.CounterOpts{ + Name: "myapp_processed_ops_total", + Help: "The total number of processed events", + }), + } + return m +} + +func recordMetrics(m *metrics) { + go func() { + for { + m.opsProcessed.Inc() + time.Sleep(2 * time.Second) + } + }() +} func main() { - recordMetrics() + reg := prometheus.NewRegistry() + m := newMetrics(reg) + recordMetrics(m) - http.Handle("/metrics", promhttp.Handler()) - http.ListenAndServe(":2112", nil) + http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{})) + http.ListenAndServe(":2112", nil) } ``` diff --git a/docs/tutorials/instrumenting_http_server_in_go.md b/docs/tutorials/instrumenting_http_server_in_go.md index cb6a161b..a5cf455f 100644 --- a/docs/tutorials/instrumenting_http_server_in_go.md +++ b/docs/tutorials/instrumenting_http_server_in_go.md @@ -21,7 +21,7 @@ func ping(w http.ResponseWriter, req *http.Request){ } func main() { - http.HandleFunc("/ping",ping) + http.HandleFunc("/ping", ping) http.ListenAndServe(":8090", nil) } @@ -39,36 +39,48 @@ Now open `http://localhost:8090/ping` in your browser and you must see `pong`. [![Server](/assets/docs/tutorial/server.png)](/assets/docs/tutorial/server.png) -Now lets add a metric to the server which will instrument the number of requests made to the ping endpoint,the counter metric type is suitable for this as we know the request count doesn’t go down and only increases. +Now lets add a metric to the server which will instrument the number of requests made to the ping endpoint, the counter metric type is suitable for this as we know the request count doesn’t go down and only increases. Create a Prometheus counter ```go -var pingCounter = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "ping_request_count", - Help: "No of request handled by Ping handler", - }, -) -``` +type metrics struct { + pingCounter prometheus.Counter +} -Next lets update the ping Handler to increase the count of the counter using `pingCounter.Inc()`. - -```go -func ping(w http.ResponseWriter, req *http.Request) { - pingCounter.Inc() - fmt.Fprintf(w, "pong") +func newMetrics(reg prometheus.Registerer) *metrics { + m := &metrics{ + pingCounter: promauto.With(reg).NewCounter( + prometheus.CounterOpts{ + Name: "ping_request_count", + Help: "No of request handled by Ping handler", + }), + } + return m } ``` -Then register the counter to the Default Register and expose the metrics. +Next lets update the ping Handler to increase the count of the counter using `metrics.pingCounter.Inc()`. + +```go +func ping(m *metrics) func(w http.ResponseWriter, req *http.Request) { + return func(w http.ResponseWriter, req *http.Request) { + m.pingCounter.Inc() + fmt.Fprintf(w, "pong") + } +} +``` + +Then register the metrics (in this case only one counter) to a Prometheus Register and expose the metrics. ```go func main() { - prometheus.MustRegister(pingCounter) - http.HandleFunc("/ping", ping) - http.Handle("/metrics", promhttp.Handler()) - http.ListenAndServe(":8090", nil) + reg := prometheus.NewRegistry() + m := newMetrics(reg) + + http.HandleFunc("/ping", ping(m)) + http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{})) + http.ListenAndServe(":8090", nil) } ``` @@ -76,43 +88,55 @@ The `prometheus.MustRegister` function registers the pingCounter to the default To expose the metrics the Go Prometheus client library provides the promhttp package. `promhttp.Handler()` provides a `http.Handler` which exposes the metrics registered in the Default Register. +The sample code depends on the The sample code depends on the ```go package main import ( - "fmt" - "net/http" + "fmt" + "net/http" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/prometheus/client_golang/prometheus/promhttp" ) -var pingCounter = prometheus.NewCounter( - prometheus.CounterOpts{ - Name: "ping_request_count", - Help: "No of request handled by Ping handler", - }, -) +type metrics struct { + pingCounter prometheus.Counter +} -func ping(w http.ResponseWriter, req *http.Request) { - pingCounter.Inc() - fmt.Fprintf(w, "pong") +func newMetrics(reg prometheus.Registerer) *metrics { + m := &metrics{ + pingCounter: promauto.With(reg).NewCounter( + prometheus.CounterOpts{ + Name: "ping_request_count", + Help: "No of request handled by Ping handler", + }), + } + return m +} + +func ping(m *metrics) func(w http.ResponseWriter, req *http.Request) { + return func(w http.ResponseWriter, req *http.Request) { + m.pingCounter.Inc() + fmt.Fprintf(w, "pong") + } } func main() { - prometheus.MustRegister(pingCounter) + reg := prometheus.NewRegistry() + m := newMetrics(reg) - http.HandleFunc("/ping", ping) - http.Handle("/metrics", promhttp.Handler()) - http.ListenAndServe(":8090", nil) + http.HandleFunc("/ping", ping(m)) + http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{})) + http.ListenAndServe(":8090", nil) } ``` Run the example - -```sh +```bash go mod init prom_example go mod tidy go run server.go