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
+11 -1
View File
@@ -7,7 +7,10 @@ import (
"github.com/go-gost/core/auth"
"github.com/go-gost/core/service"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
xmetrics "github.com/go-gost/x/metrics"
)
const (
@@ -46,6 +49,8 @@ type metricService struct {
}
// NewService creates a metrics Service that exposes Prometheus metrics over HTTP.
// It serves from the GOST custom registry rather than the default one so that
// process metrics are available on all platforms.
func NewService(network, addr string, opts ...Option) (service.Service, error) {
if network == "" {
network = "tcp"
@@ -82,7 +87,12 @@ func NewService(network, addr string, opts ...Option) (service.Service, error) {
return
}
}
promhttp.Handler().ServeHTTP(w, r)
reg := xmetrics.Registry()
if reg == nil {
reg = prometheus.DefaultRegisterer.(*prometheus.Registry)
}
promhttp.HandlerFor(reg, promhttp.HandlerOpts{}).ServeHTTP(w, r)
}))
return &metricService{
s: &http.Server{