stats

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2025 License: MIT Imports: 7 Imported by: 0

README

Go System Stats Collector

stats is a lightweight Go library that provides runtime stats, system information, and basic health check endpoints.

Features

  • Collect Go runtime information (Goroutine count, Memory stats, GC stats, etc.)
  • Collect process information (PID, Executable path, etc.)
  • Provide JSON stats via HTTP handler
  • Health check handler
  • Optional Prometheus exporter
  • Configurable environment variable filtering for security

Install

go get github.com/7csc/stats-handler
Usage
r := chi.NewRouter()
r.Get("/stats", stats.Handler)
r.Get("/healthz", stats.HealthzHandler)
Example Output
{
  "time": 1710000000000,
  "go_version": "go1.22",
  "go_os": "linux",
  "go_arch": "amd64",
  "cpus": 8,
  "goroutine_num": 15,
  "memory_alloc": 123456,
  "memory_total_alloc": 987654321,
  "memory_sys": 543210,
  "memory_usage_percent": 23.4,
  "gc_count": 12,
  "gc_pause_total_ns": 987654,
  "thread_count": 25,
  "pid": 12345,
  "ppid": 1,
  "executable": "/app/main",
  "uptime": 123456789,
  "env_vars": {
    "PATH": "/usr/bin:/bin",
    "SECRET_KEY": "******"
  }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(w http.ResponseWriter, r *http.Request)

func HealthHandler

func HealthHandler(w http.ResponseWriter, r *http.Request)

func SetEnvFilter

func SetEnvFilter(filter func(string, string) (string, string))

Types

type Stats

type Stats struct {
	Time             int64             `json:"time"`
	Version          string            `json:"go_version"`
	OS               string            `json:"go_os"`
	Arch             string            `json:"go_arch"`
	CPUs             int               `json:"cpus"`
	GoroutineNum     int               `json:"goroutine_num"`
	MemoryAlloc      uint64            `json:"memory_alloc"`
	MemoryTotalAlloc uint64            `json:"memory_total_alloc"`
	MemorySys        uint64            `json:"memory_sys"`
	MemoryUsage      float64           `json:"memory_usage_percent"`
	GCCount          uint32            `json:"gc_count"`
	GCPauseTotal     uint64            `json:"gc_pause_total_ns"`
	ThreadCount      int64             `json:"thread_count"`
	PID              int               `json:"pid"`
	PPID             int               `json:"ppid"`
	Executable       string            `json:"executable"`
	Uptime           int64             `json:"uptime"`
	EnvVars          map[string]string `json:"env_vars"`
	BuildInfo        string            `json:"build_info"`
}

func CollectStats

func CollectStats() *Stats

Jump to

Keyboard shortcuts

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