tsweb: split promvarz into an optional dependency

Allows the use of tsweb without pulling in all of the heavy prometheus
client libraries, protobuf and so on.

Updates #15160

Signed-off-by: David Anderson <dave@tailscale.com>
This commit is contained in:
David Anderson
2025-03-13 15:29:58 -07:00
committed by Dave Anderson
parent 74ee749386
commit daa5635ba6
12 changed files with 58 additions and 26 deletions

View File

@@ -96,6 +96,7 @@ tailscale.com/cmd/derper dependencies: (generated by github.com/tailscale/depawa
tailscale.com/disco from tailscale.com/derp tailscale.com/disco from tailscale.com/derp
tailscale.com/drive from tailscale.com/client/local+ tailscale.com/drive from tailscale.com/client/local+
tailscale.com/envknob from tailscale.com/client/local+ tailscale.com/envknob from tailscale.com/client/local+
tailscale.com/feature from tailscale.com/tsweb
tailscale.com/health from tailscale.com/net/tlsdial+ tailscale.com/health from tailscale.com/net/tlsdial+
tailscale.com/hostinfo from tailscale.com/net/netmon+ tailscale.com/hostinfo from tailscale.com/net/netmon+
tailscale.com/ipn from tailscale.com/client/local tailscale.com/ipn from tailscale.com/client/local
@@ -128,8 +129,8 @@ tailscale.com/cmd/derper dependencies: (generated by github.com/tailscale/depawa
tailscale.com/tstime from tailscale.com/derp+ tailscale.com/tstime from tailscale.com/derp+
tailscale.com/tstime/mono from tailscale.com/tstime/rate tailscale.com/tstime/mono from tailscale.com/tstime/rate
tailscale.com/tstime/rate from tailscale.com/derp tailscale.com/tstime/rate from tailscale.com/derp
tailscale.com/tsweb from tailscale.com/cmd/derper tailscale.com/tsweb from tailscale.com/cmd/derper+
tailscale.com/tsweb/promvarz from tailscale.com/tsweb tailscale.com/tsweb/promvarz from tailscale.com/cmd/derper
tailscale.com/tsweb/varz from tailscale.com/tsweb+ tailscale.com/tsweb/varz from tailscale.com/tsweb+
tailscale.com/types/dnstype from tailscale.com/tailcfg+ tailscale.com/types/dnstype from tailscale.com/tailcfg+
tailscale.com/types/empty from tailscale.com/ipn tailscale.com/types/empty from tailscale.com/ipn
@@ -309,7 +310,7 @@ tailscale.com/cmd/derper dependencies: (generated by github.com/tailscale/depawa
html from net/http/pprof+ html from net/http/pprof+
html/template from tailscale.com/cmd/derper html/template from tailscale.com/cmd/derper
internal/abi from crypto/x509/internal/macos+ internal/abi from crypto/x509/internal/macos+
internal/asan from syscall+ internal/asan from internal/runtime/maps+
internal/bisect from internal/godebug internal/bisect from internal/godebug
internal/bytealg from bytes+ internal/bytealg from bytes+
internal/byteorder from crypto/cipher+ internal/byteorder from crypto/cipher+
@@ -319,12 +320,12 @@ tailscale.com/cmd/derper dependencies: (generated by github.com/tailscale/depawa
internal/filepathlite from os+ internal/filepathlite from os+
internal/fmtsort from fmt+ internal/fmtsort from fmt+
internal/goarch from crypto/internal/fips140deps/cpu+ internal/goarch from crypto/internal/fips140deps/cpu+
internal/godebug from crypto/tls+ internal/godebug from crypto/internal/fips140deps/godebug+
internal/godebugs from internal/godebug+ internal/godebugs from internal/godebug+
internal/goexperiment from runtime+ internal/goexperiment from hash/maphash+
internal/goos from crypto/x509+ internal/goos from crypto/x509+
internal/itoa from internal/poll+ internal/itoa from internal/poll+
internal/msan from syscall+ internal/msan from internal/runtime/maps+
internal/nettrace from net+ internal/nettrace from net+
internal/oserror from io/fs+ internal/oserror from io/fs+
internal/poll from net+ internal/poll from net+

View File

@@ -49,6 +49,9 @@ import (
"tailscale.com/types/key" "tailscale.com/types/key"
"tailscale.com/types/logger" "tailscale.com/types/logger"
"tailscale.com/version" "tailscale.com/version"
// Support for prometheus varz in tsweb
_ "tailscale.com/tsweb/promvarz"
) )
var ( var (

View File

@@ -15,6 +15,9 @@ import (
"tailscale.com/prober" "tailscale.com/prober"
"tailscale.com/tsweb" "tailscale.com/tsweb"
"tailscale.com/version" "tailscale.com/version"
// Support for prometheus varz in tsweb
_ "tailscale.com/tsweb/promvarz"
) )
var ( var (

View File

@@ -1151,7 +1151,7 @@ tailscale.com/cmd/k8s-operator dependencies: (generated by github.com/tailscale/
html from html/template+ html from html/template+
html/template from github.com/gorilla/csrf html/template from github.com/gorilla/csrf
internal/abi from crypto/x509/internal/macos+ internal/abi from crypto/x509/internal/macos+
internal/asan from syscall+ internal/asan from internal/runtime/maps+
internal/bisect from internal/godebug internal/bisect from internal/godebug
internal/bytealg from bytes+ internal/bytealg from bytes+
internal/byteorder from crypto/cipher+ internal/byteorder from crypto/cipher+
@@ -1163,11 +1163,11 @@ tailscale.com/cmd/k8s-operator dependencies: (generated by github.com/tailscale/
internal/goarch from crypto/internal/fips140deps/cpu+ internal/goarch from crypto/internal/fips140deps/cpu+
internal/godebug from archive/tar+ internal/godebug from archive/tar+
internal/godebugs from internal/godebug+ internal/godebugs from internal/godebug+
internal/goexperiment from runtime+ internal/goexperiment from hash/maphash+
internal/goos from crypto/x509+ internal/goos from crypto/x509+
internal/itoa from internal/poll+ internal/itoa from internal/poll+
internal/lazyregexp from go/doc internal/lazyregexp from go/doc
internal/msan from syscall+ internal/msan from internal/runtime/maps+
internal/nettrace from net+ internal/nettrace from net+
internal/oserror from io/fs+ internal/oserror from io/fs+
internal/poll from net+ internal/poll from net+

View File

@@ -49,6 +49,7 @@ tailscale.com/cmd/stund dependencies: (generated by github.com/tailscale/depawar
google.golang.org/protobuf/types/known/timestamppb from github.com/prometheus/client_golang/prometheus+ google.golang.org/protobuf/types/known/timestamppb from github.com/prometheus/client_golang/prometheus+
tailscale.com from tailscale.com/version tailscale.com from tailscale.com/version
tailscale.com/envknob from tailscale.com/tsweb+ tailscale.com/envknob from tailscale.com/tsweb+
tailscale.com/feature from tailscale.com/tsweb
tailscale.com/kube/kubetypes from tailscale.com/envknob tailscale.com/kube/kubetypes from tailscale.com/envknob
tailscale.com/metrics from tailscale.com/net/stunserver+ tailscale.com/metrics from tailscale.com/net/stunserver+
tailscale.com/net/netaddr from tailscale.com/net/tsaddr tailscale.com/net/netaddr from tailscale.com/net/tsaddr
@@ -57,8 +58,8 @@ tailscale.com/cmd/stund dependencies: (generated by github.com/tailscale/depawar
tailscale.com/net/tsaddr from tailscale.com/tsweb tailscale.com/net/tsaddr from tailscale.com/tsweb
tailscale.com/syncs from tailscale.com/metrics tailscale.com/syncs from tailscale.com/metrics
tailscale.com/tailcfg from tailscale.com/version tailscale.com/tailcfg from tailscale.com/version
tailscale.com/tsweb from tailscale.com/cmd/stund tailscale.com/tsweb from tailscale.com/cmd/stund+
tailscale.com/tsweb/promvarz from tailscale.com/tsweb tailscale.com/tsweb/promvarz from tailscale.com/cmd/stund
tailscale.com/tsweb/varz from tailscale.com/tsweb+ tailscale.com/tsweb/varz from tailscale.com/tsweb+
tailscale.com/types/dnstype from tailscale.com/tailcfg tailscale.com/types/dnstype from tailscale.com/tailcfg
tailscale.com/types/ipproto from tailscale.com/tailcfg tailscale.com/types/ipproto from tailscale.com/tailcfg
@@ -194,7 +195,7 @@ tailscale.com/cmd/stund dependencies: (generated by github.com/tailscale/depawar
hash/maphash from go4.org/mem hash/maphash from go4.org/mem
html from net/http/pprof+ html from net/http/pprof+
internal/abi from crypto/x509/internal/macos+ internal/abi from crypto/x509/internal/macos+
internal/asan from syscall+ internal/asan from internal/runtime/maps+
internal/bisect from internal/godebug internal/bisect from internal/godebug
internal/bytealg from bytes+ internal/bytealg from bytes+
internal/byteorder from crypto/cipher+ internal/byteorder from crypto/cipher+
@@ -204,12 +205,12 @@ tailscale.com/cmd/stund dependencies: (generated by github.com/tailscale/depawar
internal/filepathlite from os+ internal/filepathlite from os+
internal/fmtsort from fmt internal/fmtsort from fmt
internal/goarch from crypto/internal/fips140deps/cpu+ internal/goarch from crypto/internal/fips140deps/cpu+
internal/godebug from crypto/tls+ internal/godebug from crypto/internal/fips140deps/godebug+
internal/godebugs from internal/godebug+ internal/godebugs from internal/godebug+
internal/goexperiment from runtime+ internal/goexperiment from hash/maphash+
internal/goos from crypto/x509+ internal/goos from crypto/x509+
internal/itoa from internal/poll+ internal/itoa from internal/poll+
internal/msan from syscall+ internal/msan from internal/runtime/maps+
internal/nettrace from net+ internal/nettrace from net+
internal/oserror from io/fs+ internal/oserror from io/fs+
internal/poll from net+ internal/poll from net+

View File

@@ -15,6 +15,9 @@ import (
"tailscale.com/net/stunserver" "tailscale.com/net/stunserver"
"tailscale.com/tsweb" "tailscale.com/tsweb"
// Support for prometheus varz in tsweb
_ "tailscale.com/tsweb/promvarz"
) )
var ( var (

View File

@@ -333,7 +333,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep
image/color from github.com/skip2/go-qrcode+ image/color from github.com/skip2/go-qrcode+
image/png from github.com/skip2/go-qrcode image/png from github.com/skip2/go-qrcode
internal/abi from crypto/x509/internal/macos+ internal/abi from crypto/x509/internal/macos+
internal/asan from syscall+ internal/asan from internal/runtime/maps+
internal/bisect from internal/godebug internal/bisect from internal/godebug
internal/bytealg from bytes+ internal/bytealg from bytes+
internal/byteorder from crypto/cipher+ internal/byteorder from crypto/cipher+
@@ -345,10 +345,10 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep
internal/goarch from crypto/internal/fips140deps/cpu+ internal/goarch from crypto/internal/fips140deps/cpu+
internal/godebug from archive/tar+ internal/godebug from archive/tar+
internal/godebugs from internal/godebug+ internal/godebugs from internal/godebug+
internal/goexperiment from runtime+ internal/goexperiment from hash/maphash+
internal/goos from crypto/x509+ internal/goos from crypto/x509+
internal/itoa from internal/poll+ internal/itoa from internal/poll+
internal/msan from syscall+ internal/msan from internal/runtime/maps+
internal/nettrace from net+ internal/nettrace from net+
internal/oserror from io/fs+ internal/oserror from io/fs+
internal/poll from net+ internal/poll from net+

View File

@@ -589,7 +589,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
html from html/template+ html from html/template+
html/template from github.com/gorilla/csrf html/template from github.com/gorilla/csrf
internal/abi from crypto/x509/internal/macos+ internal/abi from crypto/x509/internal/macos+
internal/asan from syscall+ internal/asan from internal/runtime/maps+
internal/bisect from internal/godebug internal/bisect from internal/godebug
internal/bytealg from bytes+ internal/bytealg from bytes+
internal/byteorder from crypto/cipher+ internal/byteorder from crypto/cipher+
@@ -601,10 +601,10 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
internal/goarch from crypto/internal/fips140deps/cpu+ internal/goarch from crypto/internal/fips140deps/cpu+
internal/godebug from archive/tar+ internal/godebug from archive/tar+
internal/godebugs from internal/godebug+ internal/godebugs from internal/godebug+
internal/goexperiment from runtime+ internal/goexperiment from hash/maphash+
internal/goos from crypto/x509+ internal/goos from crypto/x509+
internal/itoa from internal/poll+ internal/itoa from internal/poll+
internal/msan from syscall+ internal/msan from internal/runtime/maps+
internal/nettrace from net+ internal/nettrace from net+
internal/oserror from io/fs+ internal/oserror from io/fs+
internal/poll from net+ internal/poll from net+

View File

@@ -18,6 +18,9 @@ import (
"tailscale.com/derp/xdp" "tailscale.com/derp/xdp"
"tailscale.com/net/netutil" "tailscale.com/net/netutil"
"tailscale.com/tsweb" "tailscale.com/tsweb"
// Support for prometheus varz in tsweb
_ "tailscale.com/tsweb/promvarz"
) )
var ( var (

View File

@@ -14,7 +14,7 @@ import (
"os" "os"
"runtime" "runtime"
"tailscale.com/tsweb/promvarz" "tailscale.com/feature"
"tailscale.com/tsweb/varz" "tailscale.com/tsweb/varz"
"tailscale.com/version" "tailscale.com/version"
) )
@@ -37,6 +37,11 @@ type DebugHandler struct {
title string // title displayed on index page title string // title displayed on index page
} }
// PrometheusHandler is an optional hook to enable native Prometheus
// support in the debug handler. It is disabled by default. Import the
// tailscale.com/tsweb/promvarz package to enable this feature.
var PrometheusHandler feature.Hook[func(*DebugHandler)]
// Debugger returns the DebugHandler registered on mux at /debug/, // Debugger returns the DebugHandler registered on mux at /debug/,
// creating it if necessary. // creating it if necessary.
func Debugger(mux *http.ServeMux) *DebugHandler { func Debugger(mux *http.ServeMux) *DebugHandler {
@@ -53,7 +58,11 @@ func Debugger(mux *http.ServeMux) *DebugHandler {
ret.KVFunc("Uptime", func() any { return varz.Uptime() }) ret.KVFunc("Uptime", func() any { return varz.Uptime() })
ret.KV("Version", version.Long()) ret.KV("Version", version.Long())
ret.Handle("vars", "Metrics (Go)", expvar.Handler()) ret.Handle("vars", "Metrics (Go)", expvar.Handler())
ret.Handle("varz", "Metrics (Prometheus)", http.HandlerFunc(promvarz.Handler)) if PrometheusHandler.IsSet() {
PrometheusHandler.Get()(ret)
} else {
ret.Handle("varz", "Metrics (Prometheus)", http.HandlerFunc(varz.Handler))
}
// pprof.Index serves everything that runtime/pprof.Lookup finds: // pprof.Index serves everything that runtime/pprof.Lookup finds:
// goroutine, threadcreate, heap, allocs, block, mutex // goroutine, threadcreate, heap, allocs, block, mutex

View File

@@ -11,12 +11,21 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt" "github.com/prometheus/common/expfmt"
"tailscale.com/tsweb"
"tailscale.com/tsweb/varz" "tailscale.com/tsweb/varz"
) )
// Handler returns Prometheus metrics exported by our expvar converter func init() {
tsweb.PrometheusHandler.Set(registerVarz)
}
func registerVarz(debug *tsweb.DebugHandler) {
debug.Handle("varz", "Metrics (Prometheus)", http.HandlerFunc(handler))
}
// handler returns Prometheus metrics exported by our expvar converter
// and the official Prometheus client. // and the official Prometheus client.
func Handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
if err := gatherNativePrometheusMetrics(w); err != nil { if err := gatherNativePrometheusMetrics(w); err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))

View File

@@ -23,7 +23,7 @@ func TestHandler(t *testing.T) {
testVar1.Set(42) testVar1.Set(42)
testVar2.Set(4242) testVar2.Set(4242)
svr := httptest.NewServer(http.HandlerFunc(Handler)) svr := httptest.NewServer(http.HandlerFunc(handler))
defer svr.Close() defer svr.Close()
want := ` want := `