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 <[email protected]>
This commit is contained in:
Nick Khyl authored and Nick Khyl committed 2026-12-01 12:13:24 -08:00
1 parent ff9b8a19d9
commit fbec7529aa
2 files changed
+48 -4

No files matched your search

+48 -1
View File
@@ -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 {
-3
View File
@@ -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()