Instrumenting Any Application for Custom metric using Prometheus.

 

Today we will be learning how to add monitoring support to our Go application. Before starting we will learn more about Prometheus.

go get github.com/prometheus/client_golang/prometheus
go get github.com/prometheus/client_golang/prometheus/promauto
go get github.com/prometheus/client_golang/prometheus/promhttp
package main

import (
"net/http"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func recordCustomMetrics() {
go func() {
for {
mymetric.Inc()
time.Sleep(5 * time.Second)
}
}()
}

var (
mymetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "my_custom_metric",
Help: "total number for my custom action",
})
)

func main() {
recordCustomMetrics()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8080", nil)
}
go run main.go

Comments