Exposing Gin metrics with Prometheus
In the previous chapter, you learned how to automate the deployment process for a Gin application. However, no app is immune from downtime or external attacks (DDoS). That's why you need to set up the right tools to constantly monitor the performance of your application. Prometheus (https://prometheus.io) is a common open source tool for monitoring applications.
You can install the Go client by running the following command from your terminal session:
go get github.com/prometheus/client_golang
Next, update the main.go
file so that it exposes an HTTP route on the /prometheus
path. The route handler will call the Prometheus HTTP handler, which will return a list of runtime and application metrics:
router.GET("/prometheus", gin.WrapH(promhttp.Handler()))
Then, import the following package to use the promhttp
struct:
"github.com/prometheus/client_golang/prometheus/promhttp"
Next, redeploy the application...