fix(metrics): add cross-platform process metrics collector for non-Linux systems

The standard Prometheus ProcessCollector uses procfs (Linux-only /proc),
so process_resident_memory_bytes and process_virtual_memory_bytes were
silently absent on FreeBSD, Darwin, and other non-Linux platforms.

- Switch to a custom prometheus.Registry to isolate GOST metrics from
  the default registry auto-registrations
- Add gopsutil/v3-based process collector (process_other.go) for
  platforms without procfs (!linux && !windows)
- Keep standard ProcessCollector (process_standard.go) on Linux/Windows
- Update metrics service and handler/metrics to serve from
  promhttp.HandlerFor(customRegistry) instead of promhttp.Handler()
- Add Registry() accessor for the custom registry

Fixes go-gost/gost#509
This commit is contained in:
ginuerzh
2026-06-21 21:55:27 +08:00
parent 3ec3362ac4
commit 55a246869e
8 changed files with 218 additions and 9 deletions
+25 -2
View File
@@ -4,6 +4,8 @@ import (
"sync/atomic"
"github.com/go-gost/core/metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
)
const (
@@ -30,10 +32,31 @@ const (
)
var (
defaultMetrics metrics.Metrics = NewMetrics()
enabled atomic.Bool
defaultMetrics metrics.Metrics
defaultRegistry *prometheus.Registry
enabled atomic.Bool
)
func init() {
defaultRegistry = prometheus.NewRegistry()
// Register Go and process collectors on our custom registry so the metrics
// endpoint is self-contained. The process collector is platform-specific:
// Linux/Windows use the standard procfs/Win32-based collector; all other
// platforms (FreeBSD, Darwin, etc.) use a gopsutil-based collector.
defaultRegistry.MustRegister(collectors.NewGoCollector())
registerProcessCollector(defaultRegistry)
defaultMetrics = NewMetrics(defaultRegistry)
}
// Registry returns the custom Prometheus registry that holds all GOST metrics
// plus the Go and process collectors. The metrics HTTP endpoint should serve
// from this registry rather than prometheus.DefaultGatherer.
func Registry() *prometheus.Registry {
return defaultRegistry
}
// Enable enables or disables metrics collection globally. When disabled, all
// GetCounter, GetGauge, and GetObserver calls return noop implementations.
func Enable(b bool) {