Skip to content

Monitoring Go Applications with Prometheus

Professional

You can instrument your Go application with the official Prometheus Go client library and let the Glouton agent scrape its /metrics endpoint.

Terminal window
go get github.com/prometheus/client_golang/prometheus
go get github.com/prometheus/client_golang/prometheus/promhttp

The snippet below defines a counter and exposes it on http://localhost:8080/metrics:

package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var requestsTotal = promauto.NewCounter(prometheus.CounterOpts{
Name: "myapp_requests_total",
Help: "Total number of processed requests.",
})
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
requestsTotal.Inc()
w.Write([]byte("Hello"))
})
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8080", nil)
}

Configure Glouton to scrape your application by adding the following to /etc/glouton/conf.d/50-prometheus-metrics.conf:

metric:
prometheus:
targets:
- url: "http://localhost:8080/metrics"
name: "my_go_application"
allow_metrics:
- "myapp_requests_total"

Restart the agent to apply the configuration. See Prometheus Monitoring for the full list of options, including Docker label auto-discovery and Kubernetes annotations.