diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 9d331f738..2cd831068 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -12,6 +12,7 @@ "encoding/base64" "encoding/json" "errors" + "expvar" "fmt" "io" "log" @@ -118,6 +119,12 @@ "tailscale.com/wgengine/wgcfg/nmcfg" ) +var metricAdvertisedRoutes expvar.Int + +func init() { + usermetric.Publish("tailscaled_advertised_routes", &metricAdvertisedRoutes) +} + var controlDebugFlags = getControlDebugFlags() func getControlDebugFlags() []string { @@ -4615,12 +4622,6 @@ func unmapIPPrefixes(ippsList ...[]netip.Prefix) (ret []netip.Prefix) { return ret } -var metricAdvertisedRoutes = usermetric.NewMap( - "tailscaled_advertised_routes", - "gauge", - "Number of subnet routes advertised by the node. (excluding exit node /0 routes)", -) - // b.mu must be held. func (b *LocalBackend) applyPrefsToHostinfoLocked(hi *tailcfg.Hostinfo, prefs ipn.PrefsView) { if h := prefs.Hostname(); h != "" { @@ -4632,13 +4633,11 @@ func (b *LocalBackend) applyPrefsToHostinfoLocked(hi *tailcfg.Hostinfo, prefs ip hi.AllowsUpdate = envknob.AllowsRemoteUpdate() || prefs.AutoUpdate().Apply.EqualBool(true) // count routes without exit node routes - routeCount := 0 for _, route := range hi.RoutableIPs { if route.Bits() != 0 { - routeCount++ + metricAdvertisedRoutes.Add(1) } } - metricAdvertisedRoutes.SetInt(struct{}{}, int64(routeCount)) var sshHostKeys []string if prefs.RunSSH() && envknob.CanSSHD() { diff --git a/util/usermetric/usermetric.go b/util/usermetric/usermetric.go index 75b7b900e..04fbc2ea4 100644 --- a/util/usermetric/usermetric.go +++ b/util/usermetric/usermetric.go @@ -33,19 +33,14 @@ func NewMultiLabelMap[T comparable](name string, promType, helpText string) *met return m } -// NewMap creates and register a new -// Map variable with the given name and returns it. +// Publish declares a named exported variable. This should be called from a +// package's init function when it creates its Vars. // // Note that usermetric are not protected against duplicate // metrics name. It is the caller's responsibility to ensure that // the name is unique. -func NewMap(name string, promType, helpText string) *metrics.MultiLabelMap[struct{}] { - m := &metrics.MultiLabelMap[struct{}]{ - Type: promType, - Help: helpText, - } - vars.Set(name, m) - return m +func Publish(name string, v expvar.Var) { + vars.Set(name, v) } // Handler returns a varz.Handler that serves the userfacing expvar contained