Instrumenting HTTP server written in Go
In this tutorial we will create a simple Go HTTP server and instrument it by adding a counter metric to keep count of the total number of requests processed by the server.
Here we have a simple HTTP server with /ping endpoint which returns pong as response.
package main
import (
"fmt"
"net/http"
)
func ping(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w,"pong")
}
func main() {
http.HandleFunc("/ping", ping)
http.ListenAndServe(":8090", nil)
}
Compile and run the server
go build server.go
./server
Now open http://localhost:8090/ping in your browser and you must see pong.
Now let's add a metric to the server which will instrument the number of requests made to the ping endpoint. The counter metric type is suitable for this as we know the request count doesn’t go down and only increases.
Create a Prometheus counter
type metrics struct {
pingCounter prometheus.Counter
}
func newMetrics(reg prometheus.Registerer) *metrics {
m := &metrics{
pingCounter: promauto.With(reg).NewCounter(
prometheus.CounterOpts {
Name: "ping_request_count",
Help: "No of requests handled by Ping handler",
}),
}
return m
}
Next, let's update the ping Handler to increase the count of the counter using metrics.pingCounter.Inc().
func ping(m *metrics) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
m.pingCounter.Inc()
fmt.Fprintf(w, "pong")
}
}
Then register the metrics (in this case, only one counter) with a Prometheus registry and expose the metrics.
func main() {
reg := prometheus.NewRegistry()
m := newMetrics(reg)
http.HandleFunc("/ping", ping(m))
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
http.ListenAndServe(":8090", nil)
}
The prometheus.MustRegister function registers the pingCounter with the default registry.
To expose the metrics, the Go Prometheus client library provides the promhttp package.
promhttp.Handler() provides an http.Handler which exposes the metrics registered in the default registry.
The sample code is now:
package main
import (
"fmt"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type metrics struct {
pingCounter prometheus.Counter
}
func newMetrics(reg prometheus.Registerer) *metrics {
m := &metrics{
pingCounter: promauto.With(reg).NewCounter(
prometheus.CounterOpts{
Name: "ping_request_count",
Help: "No of request handled by Ping handler"