sockstats: switch label to enum

Makes it cheaper/simpler to persist values, and encourages reuse of
labels as opposed to generating an arbitrary number.

Updates tailscale/corp#9230
Updates #3363

Signed-off-by: Mihai Parparita <mihai@tailscale.com>
This commit is contained in:
Mihai Parparita
2023-03-06 15:35:50 -08:00
committed by Mihai Parparita
parent 9687f3700d
commit 6ac6ddbb47
13 changed files with 83 additions and 22 deletions

View File

@@ -15,10 +15,32 @@ import (
)
type SockStats struct {
Stats map[string]SockStat
Stats map[Label]SockStat
Interfaces []string
}
// Label is an identifier for a socket that stats are collected for. A finite
// set of values that may be used to label a socket to encourage grouping and
// to make storage more efficient.
type Label uint8
//go:generate go run golang.org/x/tools/cmd/stringer -type Label -trimprefix Label
// Labels are named after the package and function/struct that uses the socket.
// Values may be persisted and thus existing entries should not be re-numbered.
const (
LabelControlClientAuto Label = 0 // control/controlclient/auto.go
LabelControlClientDialer Label = 1 // control/controlhttp/client.go
LabelDERPHTTPClient Label = 2 // derp/derphttp/derphttp_client.go
LabelLogtailLogger Label = 3 // logtail/logtail.go
LabelDNSForwarderDoH Label = 4 // net/dns/resolver/forwarder.go
LabelDNSForwarderUDP Label = 5 // net/dns/resolver/forwarder.go
LabelNetcheckClient Label = 6 // net/netcheck/netcheck.go
LabelPortmapperClient Label = 7 // net/portmapper/portmapper.go
LabelMagicsockConnUDP4 Label = 8 // wgengine/magicsock/magicsock.go
LabelMagicsockConnUDP6 Label = 9 // wgengine/magicsock/magicsock.go
)
type SockStat struct {
TxBytes int64
RxBytes int64
@@ -26,7 +48,7 @@ type SockStat struct {
RxBytesByInterface map[string]int64
}
func WithSockStats(ctx context.Context, label string) context.Context {
func WithSockStats(ctx context.Context, label Label) context.Context {
return withSockStats(ctx, label)
}