How to Add Built‑In System Health Reports to Go Services with go‑commons
This article shows how small teams can replace heavyweight node_exporter setups by using the lightweight go‑commons library to expose business and system metrics in under 50 lines of Go code, then integrate the endpoint with Prometheus and Grafana for full observability.
Small teams often consider node_exporter heavy and need a full Prometheus + Grafana stack just to view basic CPU and memory usage.
The go-commons library provides a lightweight, plug‑and‑play alternative that exposes system metrics from the same Go process as the application.
Installation
go get github.com/Rodert/go-commonsMinimal monitoring service (<50 lines)
package main
import (
"fmt"
"net/http"
"sync/atomic"
"time"
"github.com/Rodert/go-commons/metrics"
)
var qpsCounter int64
func main() {
// business endpoint
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt64(&qpsCounter, 1)
fmt.Fprintln(w, "Hello, go-commons!")
})
// metrics endpoint
http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
mem := metrics.GetMemoryUsage() // returns float64 percentage
cpu := metrics.GetCPUUsage() // returns float64 percentage
qps := atomic.SwapInt64(&qpsCounter, 0)
fmt.Fprintf(w, "qps %d
", qps)
fmt.Fprintf(w, "memory_usage %.2f
", mem)
fmt.Fprintf(w, "cpu_usage %.2f
", cpu)
})
// reset QPS every second
go func() {
for range time.Tick(time.Second) {
atomic.StoreInt64(&qpsCounter, 0)
}
}()
fmt.Println("server started at :8080")
http.ListenAndServe(":8080", nil)
}Run with go run main.go; the server listens on port 8080.
Verification
Business request: curl http://localhost:8080/hello Metrics request: curl http://localhost:8080/metrics Typical output:
qps 3
memory_usage 42.78
cpu_usage 5.13The service acts as a miniature node_exporter without requiring a separate binary.
Prometheus integration
Add a scrape job:
scrape_configs:
- job_name: "go-app"
static_configs:
- targets: ["localhost:8080"]Grafana can then display QPS, memory, and CPU on a single dashboard.
Key characteristics
Zero‑cost onboarding: a single go get and a few lines of code.
Lightweight: no separate node_exporter deployment.
Unified endpoint: business and system metrics share /metrics.
Open‑source repository at https://github.com/Rodert/go-commons; future releases may add network, disk, and goroutine metrics.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Golang Shines
We share daily the latest Golang technical articles, practical resources, language news, tutorials, and real-world projects to help everyone learn and improve.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
