Exposing the Desired Metrics

Let’s learn how to expose the metrics we want to track.

This lesson illustrates how to expose metrics from the runtime/metrics package to Prometheus. In our case, we use /sched/goroutines:goroutines and /memory/classes/total:bytes. We already know about the former, which is the total number of goroutines. The latter metric is the amount of memory mapped by the Go runtime into the current process as read-write.

Note: Because the presented code uses an external package, it should be put inside ~/go/src and Go modules should be enabled using go mod init.

Coding example

The Go code of prometheus.go is as follows:

Get hands-on with 1200+ tech skills courses.