From fbec7529aaa326acd551bc155e8ce0df49ab39fd Mon Sep 17 00:00:00 2001 From: Nick Khyl Date: Mon, 30 Nov 2026 19:35:54 -0800 Subject: [PATCH] cmd/tailscaled: upload the client metrics of the Windows subprocess On Windows, tailscaled runs as a service that starts a subprocess to do the actual work. Only the service process uploads logs, and only its own client metrics went with them, which left out nearly all of the metrics, as they are counted in the subprocess. Have the subprocess write the changes in its metrics to its output ahead of its log lines, and have the service process merge them into its own metrics with clientmetric.Remote rather than log them. Stop skipping TestClientMetricsUploaded for the Windows service. Fixes tailscale/corp#6528 Signed-off-by: Nick Khyl --- cmd/tailscaled/tailscaled_windows.go | 49 +++++++++++++++++++++++++- tstest/integration/integration_test.go | 3 -- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/cmd/tailscaled/tailscaled_windows.go b/cmd/tailscaled/tailscaled_windows.go index b94576d65..51479baf6 100644 --- a/cmd/tailscaled/tailscaled_windows.go +++ b/cmd/tailscaled/tailscaled_windows.go @@ -20,6 +20,7 @@ package main // import "tailscale.com/cmd/tailscaled" import ( "bufio" + "bytes" "context" "encoding/json" "errors" @@ -31,6 +32,7 @@ import ( "os/exec" "os/signal" "path/filepath" + "strings" "sync" "syscall" "time" @@ -304,6 +306,43 @@ func cmdName(c svc.Cmd) string { return fmt.Sprintf("Unknown-Service-Cmd-%d", c) } +// subprocMetricsPrefix is the prefix of the lines that the subprocess writes +// to report its client metrics to the service process. See [subprocLogWriter]. +const subprocMetricsPrefix = "subproc-metrics: " + +// subprocLogWriter is the log output of the subprocess. Only the service +// process uploads logs, along with its own client metrics, so the subprocess +// writes the changes in its metrics ahead of the log lines that follow them, +// for [babysitProc] to merge into the metrics of the service process. +// It indents the log lines that start with [subprocMetricsPrefix], +// so that only the lines it writes itself are taken for the metrics. +type subprocLogWriter struct { + w io.Writer +} + +func (w subprocLogWriter) Write(p []byte) (int, error) { + if delta := clientmetric.EncodeLogTailMetricsDelta(); delta != "" { + if _, err := io.WriteString(w.w, subprocMetricsPrefix+delta+"\n"); err != nil { + return 0, err + } + } + if !bytes.Contains(p, []byte(subprocMetricsPrefix)) { + return w.w.Write(p) + } + // Indent the log lines that [babysitProc] would take for the metrics. + var buf []byte + for _, line := range bytes.SplitAfter(p, []byte("\n")) { + if bytes.HasPrefix(line, []byte(subprocMetricsPrefix)) { + buf = append(buf, ' ') + } + buf = append(buf, line...) + } + if _, err := w.w.Write(buf); err != nil { + return 0, err + } + return len(p), nil +} + func beWindowsSubprocess() bool { if beFirewallKillswitch() { return true @@ -316,6 +355,7 @@ func beWindowsSubprocess() bool { // Remove the date/time prefix; the logtail + file loggers add it. log.SetFlags(0) + log.SetOutput(subprocLogWriter{os.Stderr}) log.Printf("Program starting: v%v: %#v", version.Long(), os.Args) log.Printf("subproc mode: logid=%v", logID) @@ -520,10 +560,17 @@ func babysitProc(ctx context.Context, args []string, logf logger.Logf) { } go func(r *os.File) { defer r.Close() + // The metrics of this run of the subprocess. + var metrics clientmetric.Remote + defer metrics.Reset() rb := bufio.NewReader(r) for { s, err := rb.ReadString('\n') - if s != "" { + if delta, ok := strings.CutPrefix(s, subprocMetricsPrefix); ok { + if err := metrics.Update(strings.TrimSpace(delta)); err != nil { + logf("invalid subprocess metrics: %v", err) + } + } else if s != "" { logf("%s", s) } if err != nil { diff --git a/tstest/integration/integration_test.go b/tstest/integration/integration_test.go index f22c87718..e8e6b7f6c 100644 --- a/tstest/integration/integration_test.go +++ b/tstest/integration/integration_test.go @@ -305,9 +305,6 @@ func TestClientMetricsUploaded(t *testing.T) { env := NewTestEnv(t) env.LogCatcher.StoreRawJSON() n := NewTestNode(t, env) - if runtime.GOOS == "windows" && n.tunMode { - t.Skip("the Windows service doesn't upload the metrics of its subprocess; see tailscale/corp#6528") - } n.StartDaemon() n.AwaitResponding()