metrics

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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

Jump to

Keyboard shortcuts

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