Skip to content

Getting started

Install

go get gitlab.com/phpboyscout/go/transport-metrics

Mount /metrics

Register mounts a Prometheus endpoint onto your mux and registers the Go runtime, process, and build-info collectors by default:

package main

import (
    "net/http"

    metrics "gitlab.com/phpboyscout/go/transport-metrics"
)

func main() {
    mux := http.NewServeMux()

    mux.HandleFunc("GET /v1/hello", func(w http.ResponseWriter, _ *http.Request) {
        _, _ = w.Write([]byte(`{"message":"hi"}`))
    })

    // Mount /metrics. Pass your auth middleware, or bind to loopback (below).
    if err := metrics.Register(mux, metrics.WithMiddleware(authMiddleware)); err != nil {
        panic(err)
    }

    _ = http.ListenAndServe("127.0.0.1:8080", mux)
}

Scrape it:

curl -s localhost:8080/metrics | grep -E '^(go_|process_|build_info)'
# go_goroutines 12
# process_resident_memory_bytes 2.1e+07
# build_info{goversion="go1.26.5",revision="…",version="v1.0.0"} 1

You get, with no configuration:

  • go_* — goroutines, heap, GC pauses, threads, …
  • process_* — RSS, CPU, open FDs, start time (uptime).
  • build_info — a constant 1 labelled with the version, VCS revision and Go version, so any dashboard can join a metric to the build that produced it.

Guard it

A metrics endpoint must never be an unauthenticated open port. Either pass your service's auth middleware (as above) or bind the server to a loopback/internal address. Mounting without a guard logs a warning. See Guard the endpoint.

A dedicated build_info

Override the auto-detected build metadata (e.g. from your -ldflags version):

metrics.Register(mux,
    metrics.WithMiddleware(authMiddleware),
    metrics.WithBuildInfo(metrics.BuildInfo{Version: version, Revision: commit, Name: "mytool"}),
)

Next