From 4a59a2781a7d4d5b5f1d11142826bac8ebacf8eb Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 18 Nov 2021 07:54:24 -0800 Subject: [PATCH] ipn/ipnlocal: export client metrics over peerapi to owner Updates #3307 Change-Id: I41b1f3c16af5f385575e8d6cea70ae8386504dd8 Signed-off-by: Brad Fitzpatrick --- ipn/ipnlocal/peerapi.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/ipn/ipnlocal/peerapi.go b/ipn/ipnlocal/peerapi.go index 4ccf231ec..c7641122b 100644 --- a/ipn/ipnlocal/peerapi.go +++ b/ipn/ipnlocal/peerapi.go @@ -36,6 +36,7 @@ import ( "tailscale.com/net/interfaces" "tailscale.com/syncs" "tailscale.com/tailcfg" + "tailscale.com/util/clientmetric" "tailscale.com/wgengine" ) @@ -502,13 +503,16 @@ func (h *peerAPIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.handlePeerPut(w, r) return } - if r.URL.Path == "/v0/goroutines" { + switch r.URL.Path { + case "/v0/goroutines": h.handleServeGoroutines(w, r) return - } - if r.URL.Path == "/v0/env" { + case "/v0/env": h.handleServeEnv(w, r) return + case "/v0/metrics": + h.handleServeMetrics(w, r) + return } who := h.peerUser.DisplayName fmt.Fprintf(w, ` @@ -736,3 +740,12 @@ func (h *peerAPIHandler) handleServeEnv(w http.ResponseWriter, r *http.Request) w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(data) } + +func (h *peerAPIHandler) handleServeMetrics(w http.ResponseWriter, r *http.Request) { + if !h.isSelf { + http.Error(w, "not owner", http.StatusForbidden) + return + } + w.Header().Set("Content-Type", "text/plain") + clientmetric.WritePrometheusExpositionFormat(w) +}