summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@tailscale.com>2024-10-22 13:53:34 -0500
committerBrad Fitzpatrick <bradfitz@tailscale.com>2024-10-22 13:53:34 -0500
commitdecf89f4028b04de2f719bf3e3113418e8780b3d (patch)
tree752f211a59013ed953de425450dcc998d65a8afd
parentae5bc88ebea2f96f67e54ba6886c63ee0af14b54 (diff)
downloadtailscale-bradfitz/cmd_printmetric.tar.xz
tailscale-bradfitz/cmd_printmetric.zip
cmd/printmetric: add start of tool to dump usermetrics to JSONbradfitz/cmd_printmetric
Updates tailscale/corp#22075 Change-Id: I5b539fcb4aca1b93406cf139c719a5e3c64ff7f7 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
-rw-r--r--cmd/printmetric/printmetric.go39
-rw-r--r--tsnet/tsnet.go9
2 files changed, 48 insertions, 0 deletions
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