From decf89f4028b04de2f719bf3e3113418e8780b3d Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 22 Oct 2024 13:53:34 -0500 Subject: [PATCH] cmd/printmetric: add start of tool to dump usermetrics to JSON Updates tailscale/corp#22075 Change-Id: I5b539fcb4aca1b93406cf139c719a5e3c64ff7f7 Signed-off-by: Brad Fitzpatrick --- cmd/printmetric/printmetric.go | 39 ++++++++++++++++++++++++++++++++++ tsnet/tsnet.go | 9 ++++++++ 2 files changed, 48 insertions(+) create mode 100644 cmd/printmetric/printmetric.go diff --git a/cmd/printmetric/printmetric.go b/cmd/printmetric/printmetric.go new file mode 100644 index 000000000..f22e7250c --- /dev/null +++ b/cmd/printmetric/printmetric.go @@ -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()) +} diff --git a/tsnet/tsnet.go b/tsnet/tsnet.go index 6751e0bb0..7b771c888 100644 --- a/tsnet/tsnet.go +++ b/tsnet/tsnet.go @@ -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