In this tutorial we will create a simple Go HTTP server and instrumentation 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.

    1. package main
    2. import (
    3. "fmt"
    4. "net/http"
    5. )
    6. func ping(w http.ResponseWriter, req *http.Request){
    7. fmt.Fprintf(w,"pong")
    8. }
    9. func main() {
    10. http.HandleFunc("/ping",ping)
    11. http.ListenAndServe(":8090", nil)
    12. }

    Compile and run the server

    1. go build server.go
    2. ./server.go

    Now open http://localhost:8090/ping in your browser and you must see pong.

    Server

    Now lets 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

    1. var pingCounter = prometheus.NewCounter(
    2. prometheus.CounterOpts{
    3. Name: "ping_request_count",
    4. Help: "No of request handled by Ping handler",
    5. },
    6. )

    Next lets update the ping Handler to increase the count of the counter using pingCounter.Inc().

    1. func ping(w http.ResponseWriter, req *http.Request) {
    2. pingCounter.Inc()
    3. fmt.Fprintf(w, "pong")
    4. }

    Then register the counter to the Default Register and expose the metrics.

    1. func main() {
    2. prometheus.MustRegister(pingCounter)
    3. http.HandleFunc("/ping", ping)
    4. http.Handle("/metrics", promhttp.Handler())
    5. http.ListenAndServe(":8090", nil)
    6. }

    The prometheus.MustRegister function registers the pingCounter to the default Register. To expose the metrics the Go Prometheus client library provides the promhttp package. promhttp.Handler() provides a http.Handler which exposes the metrics registered in the Default Register.

    The sample code depends on the

    1. package main
    2. import (
    3. "fmt"
    4. "net/http"
    5. "github.com/prometheus/client_golang/prometheus"
    6. "github.com/prometheus/client_golang/prometheus/promhttp"
    7. )
    8. var pingCounter = prometheus.NewCounter(
    9. prometheus.CounterOpts{
    10. Name: "ping_request_count",
    11. Help: "No of request handled by Ping handler",
    12. },
    13. )
    14. func ping(w http.ResponseWriter, req *http.Request) {
    15. pingCounter.Inc()
    16. fmt.Fprintf(w, "pong")
    17. }
    18. func main() {
    19. prometheus.MustRegister(pingCounter)
    20. http.HandleFunc("/ping", ping)
    21. http.Handle("/metrics", promhttp.Handler())
    22. http.ListenAndServe(":8090", nil)
    23. }

    Run the example

    1. go mod init prom_example
    2. go mod tidy
    3. go run main.go

    Now hit the localhost:8090/ping endpoint a couple of times and sending a request to localhost:8090 will provide the metrics.

    Ping Metric

    Here the ping_request_count shows that /ping endpoint was called 3 times.

    The Default Register comes with a collector for go runtime metrics and that is why we see other metrics like go_threads, go_goroutines etc.

    We have built our first metric exporter. Let’s update our Prometheus config to scrape the metrics from our server.

    1. global:
    2. scrape_interval: 15s
    3. scrape_configs:
    4. - job_name: prometheus
    5. static_configs:
    6. - targets: ["localhost:9090"]
    7. - job_name: simple_server
    8. static_configs:
    9. - targets: ["localhost:8090"]

    prometheus --config.file=prometheus.yml

    This documentation is open-source. Please help improve it by filing issues or pull requests.