How to Use Go’s expvar Package for Real‑Time Metrics and Performance Monitoring

This article explains how to leverage Go's built‑in expvar package to expose application state and custom metrics via a JSON endpoint, compare it with pprof profiling, and visualize the data using tools like expvarmon for effective backend monitoring.

Radish, Keep Going!
Radish, Keep Going!
Radish, Keep Going!
How to Use Go’s expvar Package for Real‑Time Metrics and Performance Monitoring

When profiling Go applications, pprof is commonly used to collect CPU and memory data, but it can miss real‑time context in complex, concurrent programs. To capture performance bottlenecks more efficiently, you can expose probing data through the expvar package, which publishes variables as JSON at /debug/vars.

Package expvar provides a standardized interface to public variables, such as operation counters in servers. It exposes these variables via HTTP at /debug/vars in JSON format.

Below is a minimal example that starts an HTTP server and registers the default expvar handler:

package main

import (
    _ "expvar"
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/hi", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("hi
"))
    })
    fmt.Println(http.ListenAndServe("localhost:8080", nil))
}

Visiting http://localhost:8080/debug/vars returns a JSON object that includes the default cmdline and memstats fields:

{
    "cmdline": ["/path/to/your/app"],
    "memstats": { /* runtime.Memstats fields */ }
}

To expose custom metrics, define an expvar.Map, set values, and publish it:

package main

import (
    "expvar"
    "fmt"
    "net/http"
)

var customVar = expvar.NewMap("custom")

func init() {
    customVar.Set("hi_count", new(expvar.Int))
    expvar.Publish("custom", customVar)
}

func main() {
    http.HandleFunc("/hi", func(w http.ResponseWriter, r *http.Request) {
        customVar.Add("hi_count", 1)
        w.Write([]byte("hi
"))
    })
    fmt.Println(http.ListenAndServe("localhost:8080", nil))
}

After calling /hi twice, the JSON endpoint shows the incremented counter:

{
    "cmdline": ["/path/to/app"],
    "custom": {"hi_count": 2},
    "memstats": "..."
}

For visualizing the exported data, you can use expvarmon. Install it with:

go get github.com/divan/expvarmon

Then run:

expvarmon -ports="8080" -vars="custom.hi_count,mem:memstats.Alloc,mem:memstats.Sys,mem:memstats.HeapAlloc,mem:memstats.HeapInuse,duration:memstats.PauseNs,duration:memstats.PauseTotalNs"

The tool renders the selected metrics in a terminal UI, making the output more readable.

Compared with pprof, which focuses on CPU and memory profiling, expvar provides a lightweight way to expose live application state and custom counters, suitable for long‑running services that need continuous health monitoring.

In summary, expvar is a powerful, easy‑to‑use package for exporting runtime statistics and custom metrics, and it integrates well with external monitoring systems such as Prometheus, InfluxDB, or Grafana.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

backendMetricsprofilingexpvar
Radish, Keep Going!
Written by

Radish, Keep Going!

Personal sharing

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.