Sobes.tech
Junior — Middle

Kuidas pääseda süsteemi põhimeetrikatele?

sobes.tech AI

Vastus AI-lt

In Go, the expvar package is often used to obtain key system metrics, which allows exporting metrics in JSON format over HTTP. Additionally, third-party libraries like the Prometheus client for Go are popular, collecting and providing metrics in a monitoring-friendly format.

Example using expvar:

import (
    "expvar"
    "net/http"
)

var requests = expvar.NewInt("requests")

func handler(w http.ResponseWriter, r *http.Request) {
    requests.Add(1)
    w.Write([]byte("Hello, metrics!"))
}

func main() {
    http.HandleFunc("/", handler)
    // Metrics will be available at /debug/vars
    http.ListenAndServe(":8080", nil)
}

It is also possible to use standard system calls and libraries to obtain information about memory, CPU load, and other metrics, for example, via the runtime package.