cmd/printmetric: add start of tool to dump usermetrics to JSON

Updates tailscale/corp#22075

Change-Id: I5b539fcb4aca1b93406cf139c719a5e3c64ff7f7
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2024-10-22 13:53:34 -05:00
parent ae5bc88ebe
commit decf89f402
2 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,39 @@
// The printmetric command prints out JSON of the usermetric definitions.
package main
import (
"io/ioutil"
"log"
"net/http/httptest"
"os"
"tailscale.com/ipn/store/mem"
"tailscale.com/tsnet"
"tailscale.com/tstest/integration/testcontrol"
)
func main() {
var control testcontrol.Server
ts := httptest.NewServer(&control)
defer ts.Close()
td, err := ioutil.TempDir("", "testcontrol")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(td)
tsn := &tsnet.Server{
Dir: td,
Store: new(mem.Store),
UserLogf: log.Printf,
Ephemeral: true,
ControlURL: ts.URL,
}
if err := tsn.Start(); err != nil {
log.Fatal(err)
}
rec := httptest.NewRecorder()
tsn.Sys().UserMetricsRegistry().Handler(rec, httptest.NewRequest("GET", "/unused", nil))
os.Stdout.Write(rec.Body.Bytes())
}

View File

@ -126,6 +126,7 @@ type Server struct {
initOnce sync.Once
initErr error
lb *ipnlocal.LocalBackend
sys *tsd.System
netstack *netstack.Impl
netMon *netmon.Monitor
rootPath string // the state directory
@ -518,6 +519,7 @@ func (s *Server) start() (reterr error) {
}
sys := new(tsd.System)
s.sys = sys
if err := s.startLogger(&closePool, sys.HealthTracker(), tsLogf); err != nil {
return err
}
@ -1227,6 +1229,13 @@ func (s *Server) CapturePcap(ctx context.Context, pcapFile string) error {
return nil
}
// Sys returns a handle to the Tailscale subsystems of this node.
//
// This is not a stable API, nor are the APIs of the returned subsystems.
func (s *Server) Sys() *tsd.System {
return s.sys
}
type listenKey struct {
network string
host netip.Addr // or zero value for unspecified