1
0
mirror of https://github.com/prometheus/docs.git synced 2026-02-05 06:45:01 +01:00

Merge pull request #2231 from kakkoyun/remove_usage_of_global_registry

Remove usage of global registry from Go examples
This commit is contained in:
Kemal Akkoyun
2026-01-21 10:45:25 +01:00
committed by GitHub
2 changed files with 104 additions and 64 deletions

View File

@@ -27,14 +27,21 @@ This minimal application, for example, would expose the default metrics for Go a
package main package main
import ( 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() { func main() {
http.Handle("/metrics", promhttp.Handler()) reg := prometheus.NewRegistry()
http.ListenAndServe(":2112", nil) 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 package main
import ( import (
"net/http" "net/http"
"time" "time"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
) )
func recordMetrics() { type metrics struct {
go func() { opsProcessed prometheus.Counter
for {
opsProcessed.Inc()
time.Sleep(2 * time.Second)
}
}()
} }
var ( func newMetrics(reg prometheus.Registerer) *metrics {
opsProcessed = promauto.NewCounter(prometheus.CounterOpts{ m := &metrics{
Name: "myapp_processed_ops_total", opsProcessed: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Help: "The total number of processed events", 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() { func main() {
recordMetrics() reg := prometheus.NewRegistry()
m := newMetrics(reg)
recordMetrics(m)
http.Handle("/metrics", promhttp.Handler()) http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
http.ListenAndServe(":2112", nil) http.ListenAndServe(":2112", nil)
} }
``` ```

View File

@@ -21,7 +21,7 @@ func ping(w http.ResponseWriter, req *http.Request){
} }
func main() { func main() {
http.HandleFunc("/ping",ping) http.HandleFunc("/ping", ping)
http.ListenAndServe(":8090", nil) 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) [![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 doesnt 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 doesnt go down and only increases.
Create a Prometheus counter Create a Prometheus counter
```go ```go
var pingCounter = prometheus.NewCounter( type metrics struct {
prometheus.CounterOpts{ pingCounter prometheus.Counter
Name: "ping_request_count", }
Help: "No of request handled by Ping handler",
},
)
```
Next lets update the ping Handler to increase the count of the counter using `pingCounter.Inc()`. func newMetrics(reg prometheus.Registerer) *metrics {
m := &metrics{
```go pingCounter: promauto.With(reg).NewCounter(
func ping(w http.ResponseWriter, req *http.Request) { prometheus.CounterOpts{
pingCounter.Inc() Name: "ping_request_count",
fmt.Fprintf(w, "pong") 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 ```go
func main() { func main() {
prometheus.MustRegister(pingCounter) reg := prometheus.NewRegistry()
http.HandleFunc("/ping", ping) m := newMetrics(reg)
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)
} }
``` ```
@@ -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. 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. `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 The sample code depends on the
```go ```go
package main package main
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
) )
var pingCounter = prometheus.NewCounter( type metrics struct {
prometheus.CounterOpts{ pingCounter prometheus.Counter
Name: "ping_request_count", }
Help: "No of request handled by Ping handler",
},
)
func ping(w http.ResponseWriter, req *http.Request) { func newMetrics(reg prometheus.Registerer) *metrics {
pingCounter.Inc() m := &metrics{
fmt.Fprintf(w, "pong") 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() { func main() {
prometheus.MustRegister(pingCounter) reg := prometheus.NewRegistry()
m := newMetrics(reg)
http.HandleFunc("/ping", ping) http.HandleFunc("/ping", ping(m))
http.Handle("/metrics", promhttp.Handler()) http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
http.ListenAndServe(":8090", nil) http.ListenAndServe(":8090", nil)
} }
``` ```
Run the example Run the example
```bash
```sh
go mod init prom_example go mod init prom_example
go mod tidy go mod tidy
go run server.go go run server.go