Operations 6 min read

Embedded Monitoring Best Practice: Use go-commons for Built-in Service Health Reports

This article demonstrates how to quickly add lightweight, plug‑and‑play monitoring to a Go service using the open‑source go-commons library, showing installation, a minimal 50‑line example that exposes business QPS and system metrics via a single /metrics endpoint, and how to integrate it with Prometheus and Grafana.

Go Development Architecture Practice
Go Development Architecture Practice
Go Development Architecture Practice
Embedded Monitoring Best Practice: Use go-commons for Built-in Service Health Reports

Small teams and individual developers often face three monitoring problems: the heavyweight node_exporter requires a separate deployment; viewing only CPU and memory forces the setup of a full Prometheus + Grafana stack; and business code and system metrics are scattered, preventing a unified view.

Quick installation

go get github.com/Rodert/go-commons

Minimal monitoring service

The following Go program (<50 lines) exposes a business metric (QPS) together with system metrics (memory usage and CPU usage) on a single /metrics endpoint.

package main

import (
    "fmt"
    "net/http"
    "sync/atomic"
    "time"

    "github.com/Rodert/go-commons/metrics"
)

var qpsCounter int64

func main() {
    // Simulated business handler
    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() // memory usage
        cpu := metrics.GetCPUUsage()    // CPU usage
        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 the service

go run main.go

Verify the monitoring output

Call the business endpoint: curl http://localhost:8080/hello Fetch the metrics: curl http://localhost:8080/metrics Sample output:

qps 3
memory_usage 42.78
cpu_usage 5.13

This behaves like a miniature node_exporter , but the metrics are integrated directly with the application, requiring no separate deployment.

Next step: integrate with Prometheus + Grafana

Add a job to the Prometheus configuration:

scrape_configs:
  - job_name: "go-app"
    static_configs:
      - targets: ["localhost:8080"]

Grafana can then display a dashboard that combines QPS and system metrics.

Why go‑commons is recommended

Zero‑cost integration: a single go get and a few lines of code.

Lightweight: no need to deploy a separate node_exporter.

Unified view: business and system metrics are served from the same /metrics endpoint.

Open‑source and actively developed; future versions may add network, disk, and goroutine metrics.

Contribute

The project is early‑stage and welcomes feedback, code contributions, and documentation improvements. Repository: https://github.com/Rodert/go-commons

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.

MonitoringGoMetricsPrometheusOpen-sourcego-commons
Go Development Architecture Practice
Written by

Go Development Architecture Practice

Daily sharing of Golang-related technical articles, practical resources, language news, tutorials, real-world projects, and more. Looking forward to growing together. Let's go!

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.