1
0
mirror of https://github.com/prometheus/docs.git synced 2026-02-05 15:45:27 +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
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)
}
```

View File

@@ -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 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
```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