metrics

package
v0.1.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAge

func GetAge(startTime int64) string

GetAge returns a human-readable duration string representing the elapsed time since the given Unix timestamp (in seconds), formatted as "Xd Xh Xm Xs".

Types

type NameValue

type NameValue struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

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
		}
	}
}
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)
	}
}
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) != "")
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL