Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions docs/tutorials/instrumenting_http_server_in_go.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ func ping(m *metrics) func(w http.ResponseWriter, req *http.Request) {
}
```

Then register the metrics (in this case, only one counter) with a Prometheus registry and expose the metrics.
Then register the metrics with a Prometheus registry and expose them. Here we use a custom registry and also register the Go runtime collector with it.

```go
func main() {
reg := prometheus.NewRegistry()
reg.MustRegister(collectors.NewGoCollector())
m := newMetrics(reg)

http.HandleFunc("/ping", ping(m))
Expand All @@ -84,9 +85,9 @@ func main() {
}
```

The `prometheus.MustRegister` function registers the pingCounter with the default registry.
The `promauto.With(reg).NewCounter()` function creates the pingCounter and registers it with the `reg` registry.
To expose the metrics, the Go Prometheus client library provides the promhttp package.
`promhttp.Handler()` provides an `http.Handler` which exposes the metrics registered in the default registry.
`promhttp.HandlerFor(reg, promhttp.HandlerOpts{})` provides an `http.Handler` which exposes the metrics registered in the `reg` registry.

The sample code is now:

Expand All @@ -98,6 +99,7 @@ import (
"net/http"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
Expand Down Expand Up @@ -126,6 +128,7 @@ func ping(m *metrics) func(w http.ResponseWriter, req *http.Request) {

func main() {
reg := prometheus.NewRegistry()
reg.MustRegister(collectors.NewGoCollector())
m := newMetrics(reg)

http.HandleFunc("/ping", ping(m))
Expand All @@ -147,7 +150,7 @@ Now hit the localhost:8090/ping endpoint a couple of times and then send a reque

Here, the `ping_request_count` shows that the `/ping` endpoint was called 3 times.

The default registry comes with a collector for Go runtime metrics, and that is why we see other metrics like `go_threads`, `go_goroutines`, etc.
Unlike the default registry, a custom registry (`prometheus.NewRegistry()`) starts empty, so we registered the Go runtime collector explicitly with `reg.MustRegister(collectors.NewGoCollector())` (from `github.com/prometheus/client_golang/prometheus/collectors`). That is why we see other metrics like `go_threads`, `go_goroutines`, etc.

We have built our first metric exporter. Let’s update our Prometheus config to scrape the metrics from our server.

Expand Down
Binary file modified public/assets/docs/tutorial/ping_metric.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading