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:
committed by
Dave Anderson
parent
74ee749386
commit
daa5635ba6
+11
-2
@@ -14,7 +14,7 @@ import (
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"tailscale.com/tsweb/promvarz"
|
||||
"tailscale.com/feature"
|
||||
"tailscale.com/tsweb/varz"
|
||||
"tailscale.com/version"
|
||||
)
|
||||
@@ -37,6 +37,11 @@ type DebugHandler struct {
|
||||
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/,
|
||||
// creating it if necessary.
|
||||
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.KV("Version", version.Long())
|
||||
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:
|
||||
// goroutine, threadcreate, heap, allocs, block, mutex
|
||||
|
||||
@@ -11,12 +11,21 @@ import (
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/common/expfmt"
|
||||
"tailscale.com/tsweb"
|
||||
"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.
|
||||
func Handler(w http.ResponseWriter, r *http.Request) {
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
if err := gatherNativePrometheusMetrics(w); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
|
||||
@@ -23,7 +23,7 @@ func TestHandler(t *testing.T) {
|
||||
testVar1.Set(42)
|
||||
testVar2.Set(4242)
|
||||
|
||||
svr := httptest.NewServer(http.HandlerFunc(Handler))
|
||||
svr := httptest.NewServer(http.HandlerFunc(handler))
|
||||
defer svr.Close()
|
||||
|
||||
want := `
|
||||
|
||||
Reference in New Issue
Block a user