Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type NameValue ¶
NameValue holds a name-value pair used in metrics and chart data formats.
Example ¶
ExampleNameValue shows how to construct a NameValue pair directly. NameValue is the element type returned by GetMetrics.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/metrics"
)
func main() {
nv := metrics.NameValue{Name: "goroutines", Value: "42"}
fmt.Printf("%s=%s\n", nv.Name, nv.Value)
}
Output: goroutines=42
func GetMetrics ¶
func GetMetrics() []NameValue
GetMetrics collects and returns a snapshot of key runtime and system metrics as a slice of NameValue pairs. The snapshot includes CPU and memory usage, thread and goroutine counts, GOMAXPROCS, NumCPU, cgroup CPU/memory request and limit values, and the process uptime age.
Example ¶
ExampleGetMetrics shows how to call GetMetrics and look up a specific entry by name. GetMetrics samples CPU usage over one second internally, so this example is compiled but not executed during automated tests.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/metrics"
)
func main() {
snapshot := metrics.GetMetrics()
for _, nv := range snapshot {
if nv.Name == "goroutines" {
fmt.Println("goroutines:", nv.Value)
break
}
}
}
Output:
Example (Iterate) ¶
ExampleGetMetrics_iterate shows how to iterate over all metrics entries and print each name-value pair — useful for logging the full snapshot at startup.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/metrics"
)
func main() {
snapshot := metrics.GetMetrics()
for _, nv := range snapshot {
fmt.Printf("%-14s %s\n", nv.Name, nv.Value)
}
}
Output:
Example (Json) ¶
ExampleGetMetrics_json shows how to serialize the metrics snapshot to JSON, suitable for a /metrics or /health HTTP endpoint response.
package main
import (
"encoding/json"
"fmt"
"github.com/phcp-tech/common-library-golang/metrics"
)
func main() {
snapshot := metrics.GetMetrics()
b, err := json.Marshal(snapshot)
if err != nil {
fmt.Println("marshal error:", err)
return
}
fmt.Println(string(b) != "")
}
Output:
type ProcessInfo ¶
type ProcessInfo struct {
CpuPercent float64 // CPU usage as a percentage across all cores over the last second.
MemorySize uint64 // Resident set size (RSS) memory usage in megabytes.
Threads int // Number of OS threads created by the process.
Goroutines int // Number of live goroutines.
}
ProcessInfo holds a snapshot of the current process's resource usage.
func GetProcessInfo ¶
func GetProcessInfo() ProcessInfo
GetProcessInfo returns a ProcessInfo snapshot for the currently running process, including CPU percentage (measured over one second), RSS memory in megabytes, OS thread count, and goroutine count.