ipn/ipnlocal: reject load metrics on App Connectors and off Linux

App Connectors advertise a route per resolved DNS address, so per-route series
would grow with end-user browsing rather than with admin configuration. Rather
than silently emitting thousands of series, enabling the pref on one now fails
with an explanation. There is no override; App Connector support is a later
milestone.

The check lives in checkPrefsLocked, beside checkAdvertiseRoutes, so it
applies to tailscale set, tailscale up, and config-file loads alike. Because
it validates the merged prefs, it also catches turning on AppConnector while
load metrics are already enabled.

Also rejected off Linux, where nothing on the packet path honors the pref.

Updates tailscale/corp#45684

Change-Id: Ida2c7d90bf00e4d007858bca42d622ebea7f4885
Signed-off-by: Kabir Sikand <[email protected]>
This commit is contained in:
Kabir Sikand committed 2026-07-29 02:03:10 +00:00
1 parent 4d8cb46329
commit a73f5930d9
2 files changed
+129

No files matched your search

+106
View File
@@ -0,0 +1,106 @@
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package ipnlocal
import (
"runtime"
"strings"
"testing"
"tailscale.com/ipn"
"tailscale.com/types/opt"
)
func TestCheckCollectLoadMetrics(t *testing.T) {
tests := []struct {
name string
prefs *ipn.Prefs
wantErr string // substring; "" means the prefs must be accepted
}{
{
name: "unset is always fine",
prefs: &ipn.Prefs{},
},
{
name: "explicitly disabled is always fine",
prefs: &ipn.Prefs{CollectLoadMetrics: opt.NewBool(false)},
},
{
name: "disabled on an App Connector is fine",
prefs: &ipn.Prefs{
CollectLoadMetrics: opt.NewBool(false),
AppConnector: ipn.AppConnectorPrefs{Advertise: true},
},
},
{
name: "enabled on an App Connector is rejected",
prefs: &ipn.Prefs{
CollectLoadMetrics: opt.NewBool(true),
AppConnector: ipn.AppConnectorPrefs{Advertise: true},
},
wantErr: "not supported on App Connectors",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := checkCollectLoadMetrics(tt.prefs)
if tt.wantErr == "" {
if err != nil {
t.Fatalf("checkCollectLoadMetrics() = %v; want nil", err)
}
return
}
if err == nil {
t.Fatalf("checkCollectLoadMetrics() = nil; want error containing %q", tt.wantErr)
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("checkCollectLoadMetrics() = %v; want error containing %q", err, tt.wantErr)
}
})
}
}
// TestCheckCollectLoadMetricsPlatform covers the platform gate: the counting
// hook is only wired up on Linux, so accepting the pref elsewhere would leave
// it silently inert.
func TestCheckCollectLoadMetricsPlatform(t *testing.T) {
p := &ipn.Prefs{CollectLoadMetrics: opt.NewBool(true)}
err := checkCollectLoadMetrics(p)
if runtime.GOOS == "linux" {
if err != nil {
t.Fatalf("checkCollectLoadMetrics() on linux = %v; want nil", err)
}
return
}
if err == nil {
t.Fatalf("checkCollectLoadMetrics() on %s = nil; want an error", runtime.GOOS)
}
if !strings.Contains(err.Error(), "Linux") {
t.Errorf("checkCollectLoadMetrics() on %s = %v; want the error to mention Linux", runtime.GOOS, err)
}
}
// TestCheckPrefsRejectsLoadMetricsOnAppConnector verifies the check is actually
// reached through checkPrefsLocked, which is the shared gate for tailscale set,
// tailscale up, and config-file loads.
func TestCheckPrefsRejectsLoadMetricsOnAppConnector(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skipf("pref is Linux-only; running on %s", runtime.GOOS)
}
b := newTestLocalBackend(t)
p := &ipn.Prefs{
ControlURL: ipn.DefaultControlURL,
CollectLoadMetrics: opt.NewBool(true),
AppConnector: ipn.AppConnectorPrefs{Advertise: true},
}
err := b.CheckPrefs(p)
if err == nil {
t.Fatal("CheckPrefs accepted load metrics on an App Connector; want an error")
}
if !strings.Contains(err.Error(), "not supported on App Connectors") {
t.Errorf("CheckPrefs error = %v; want it to mention App Connectors", err)
}
}
+23
View File
@@ -4971,6 +4971,9 @@ func (b *LocalBackend) checkPrefsLocked(p *ipn.Prefs) error {
if err := checkAdvertiseRoutes(p); err != nil {
errs = append(errs, err)
}
if err := checkCollectLoadMetrics(p); err != nil {
errs = append(errs, err)
}
return errors.Join(errs...)
}
@@ -5080,6 +5083,26 @@ func checkAdvertiseRoutes(p *ipn.Prefs) error {
return errors.Join(errs...)
}
// checkCollectLoadMetrics validates the CollectLoadMetrics pref.
//
// Per-route load metrics are rejected on App Connectors: those advertise a
// route per resolved DNS address, so the number of series would grow with
// end-user browsing rather than with admin configuration. They are also
// rejected off Linux, where nothing on the packet path honors the pref, so
// accepting it would leave it silently inert.
func checkCollectLoadMetrics(p *ipn.Prefs) error {
if !p.CollectLoadMetrics.EqualBool(true) {
return nil
}
if buildfeatures.HasAppConnectors && p.AppConnector.Advertise {
return errors.New("load metrics are not supported on App Connectors: this node advertises routes dynamically per resolved DNS address, which would produce unbounded metric cardinality. Support for App Connectors is planned; for now use network flow logs for per-destination visibility on this node")
}
if runtime.GOOS != "linux" {
return fmt.Errorf("load metrics are only supported on Linux, not %s", runtime.GOOS)
}
return nil
}
// SetUseExitNodeEnabled turns on or off the most recently selected exit node.
//
// On success, it returns the resulting prefs (or current prefs, in the case of no change).