Files
x/metrics/metrics.go
T
ginuerzh bc3d12ec2c fix(metrics): add doc comments, fix labels mutation and nil returns, add idempotent Close, add nil guards, add tests
- Fix promMetrics.Gauge/Counter/Observer mutating caller's labels map by
  using maps.Copy instead of direct assignment
- Fix Gauge/Counter/Observer returning nil for unknown metric names
  (now return noop implementations)
- Fix metricService.Close() race with sync.Once for safe concurrent calls
- Fix SetDSCP returning nil instead of errUnsupport on unsupported ops
- Add nil guards to WrapConn, WrapPacketConn, WrapUDPConn, WrapListener
- Add doc comments to all exported symbols
- Add 45 unit tests (metrics, noop, prom, service, wrapper)
2026-05-24 22:50:41 +08:00

74 lines
2.9 KiB
Go

package metrics
import (
"sync/atomic"
"github.com/go-gost/core/metrics"
)
const (
// Number of services. Labels: host.
MetricServicesGauge metrics.MetricName = "gost_services"
// Total service requests. Labels: host, service, client.
MetricServiceRequestsCounter metrics.MetricName = "gost_service_requests_total"
// Number of in-flight requests. Labels: host, service, client.
MetricServiceRequestsInFlightGauge metrics.MetricName = "gost_service_requests_in_flight"
// Request duration histogram. Labels: host, service.
MetricServiceRequestsDurationObserver metrics.MetricName = "gost_service_request_duration_seconds"
// Total service input data transfer size in bytes. Labels: host, service, client.
MetricServiceTransferInputBytesCounter metrics.MetricName = "gost_service_transfer_input_bytes_total"
// Total service output data transfer size in bytes. Labels: host, service, client.
MetricServiceTransferOutputBytesCounter metrics.MetricName = "gost_service_transfer_output_bytes_total"
// Chain node connect duration histogram. Labels: host, chain, node.
MetricNodeConnectDurationObserver metrics.MetricName = "gost_chain_node_connect_duration_seconds"
// Total service handler errors. Labels: host, service, client.
MetricServiceHandlerErrorsCounter metrics.MetricName = "gost_service_handler_errors_total"
// Total chain connect errors. Labels: host, chain, node.
MetricChainErrorsCounter metrics.MetricName = "gost_chain_errors_total"
// Total recorder records. Labels: host, recorder.
MetricRecorderRecordsCounter metrics.MetricName = "gost_recorder_records_total"
)
var (
defaultMetrics metrics.Metrics = NewMetrics()
enabled atomic.Bool
)
// Enable enables or disables metrics collection globally. When disabled, all
// GetCounter, GetGauge, and GetObserver calls return noop implementations.
func Enable(b bool) {
enabled.Store(b)
}
// IsEnabled reports whether metrics collection is enabled.
func IsEnabled() bool {
return enabled.Load()
}
// GetCounter returns a Counter for the given name and labels. When metrics are
// disabled, a noop implementation is returned.
func GetCounter(name metrics.MetricName, labels metrics.Labels) metrics.Counter {
if IsEnabled() {
return defaultMetrics.Counter(name, labels)
}
return noop.Counter(name, labels)
}
// GetGauge returns a Gauge for the given name and labels. When metrics are
// disabled, a noop implementation is returned.
func GetGauge(name metrics.MetricName, labels metrics.Labels) metrics.Gauge {
if IsEnabled() {
return defaultMetrics.Gauge(name, labels)
}
return noop.Gauge(name, labels)
}
// GetObserver returns an Observer for the given name and labels. When metrics are
// disabled, a noop implementation is returned.
func GetObserver(name metrics.MetricName, labels metrics.Labels) metrics.Observer {
if IsEnabled() {
return defaultMetrics.Observer(name, labels)
}
return noop.Observer(name, labels)
}