mirror of
https://github.com/prometheus/docs.git
synced 2026-02-05 15:45:27 +01:00
Remove usage of global registry from Go example
Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
This commit is contained in:
committed by
Kemal Akkoyun
parent
ec0532c7c5
commit
292bc32602
@@ -29,11 +29,18 @@ package main
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"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())
|
||||
reg := prometheus.NewRegistry()
|
||||
reg.MustRegister(
|
||||
collectors.NewGoCollector(),
|
||||
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
|
||||
)
|
||||
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
|
||||
http.ListenAndServe(":2112", nil)
|
||||
}
|
||||
```
|
||||
@@ -66,26 +73,35 @@ import (
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
func recordMetrics() {
|
||||
type metrics struct {
|
||||
opsProcessed prometheus.Counter
|
||||
}
|
||||
|
||||
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 {
|
||||
opsProcessed.Inc()
|
||||
m.opsProcessed.Inc()
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
var (
|
||||
opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
|
||||
Name: "myapp_processed_ops_total",
|
||||
Help: "The total number of processed events",
|
||||
})
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
```
|
||||
|
||||
@@ -44,30 +44,42 @@ Now lets add a metric to the server which will instrument the number of requests
|
||||
Create a Prometheus counter
|
||||
|
||||
```go
|
||||
var pingCounter = prometheus.NewCounter(
|
||||
type metrics struct {
|
||||
pingCounter prometheus.Counter
|
||||
}
|
||||
|
||||
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",
|
||||
},
|
||||
)
|
||||
```
|
||||
|
||||
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")
|
||||
}),
|
||||
}
|
||||
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())
|
||||
reg := prometheus.NewRegistry()
|
||||
m := newMetrics(reg)
|
||||
|
||||
http.HandleFunc("/ping", ping(m))
|
||||
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
|
||||
http.ListenAndServe(":8090", nil)
|
||||
}
|
||||
```
|
||||
@@ -76,6 +88,7 @@ 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
|
||||
@@ -86,33 +99,44 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
var pingCounter = prometheus.NewCounter(
|
||||
type metrics struct {
|
||||
pingCounter prometheus.Counter
|
||||
}
|
||||
|
||||
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(w http.ResponseWriter, req *http.Request) {
|
||||
pingCounter.Inc()
|
||||
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.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
|
||||
|
||||
Reference in New Issue
Block a user