Search⌘ K
AI Features

Exposing the Desired Metrics

Explore how to expose Go runtime metrics such as goroutines count and memory usage to Prometheus for effective monitoring. Learn to implement HTTP handlers that serve these metrics, use Go modules and external packages, and run the application with Docker. This lesson guides you through running and testing the metrics endpoint locally and preparing the environment for Prometheus integration.

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 ...