Skip to content

Commit 571b9aa

Browse files
mihaipcoadler
authored andcommitted
ipn/ipnlocal: add a few metrics for PeerAPI and LocalAPI
Mainly motivated by wanting to know how much Taildrop is used, but also useful when tracking down how many invalid requests are generated. Signed-off-by: Mihai Parparita <[email protected]>
1 parent fe1ba7d commit 571b9aa

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

ipn/ipnlocal/peerapi.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ func peerAPIRequestShouldGetSecurityHeaders(r *http.Request) bool {
678678

679679
func (h *peerAPIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
680680
if err := h.validatePeerAPIRequest(r); err != nil {
681+
metricInvalidRequests.Add(1)
681682
h.logf("invalid request from %v: %v", h.remoteAddr, err)
682683
http.Error(w, "invalid peerapi request", http.StatusForbidden)
683684
return
@@ -688,10 +689,12 @@ func (h *peerAPIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
688689
w.Header().Set("X-Content-Type-Options", "nosniff")
689690
}
690691
if strings.HasPrefix(r.URL.Path, "/v0/put/") {
692+
metricPutCalls.Add(1)
691693
h.handlePeerPut(w, r)
692694
return
693695
}
694696
if strings.HasPrefix(r.URL.Path, "/dns-query") {
697+
metricDNSCalls.Add(1)
695698
h.handleDNSQuery(w, r)
696699
return
697700
}
@@ -712,12 +715,14 @@ func (h *peerAPIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
712715
h.handleServeDNSFwd(w, r)
713716
return
714717
case "/v0/wol":
718+
metricWakeOnLANCalls.Add(1)
715719
h.handleWakeOnLAN(w, r)
716720
return
717721
case "/v0/interfaces":
718722
h.handleServeInterfaces(w, r)
719723
return
720724
case "/v0/ingress":
725+
metricIngressCalls.Add(1)
721726
h.handleServeIngress(w, r)
722727
return
723728
}
@@ -1441,3 +1446,13 @@ func (fl *fakePeerAPIListener) Accept() (net.Conn, error) {
14411446
}
14421447

14431448
func (fl *fakePeerAPIListener) Addr() net.Addr { return fl.addr }
1449+
1450+
var (
1451+
metricInvalidRequests = clientmetric.NewCounter("peerapi_invalid_requests")
1452+
1453+
// Non-debug PeerAPI endpoints.
1454+
metricPutCalls = clientmetric.NewCounter("peerapi_put")
1455+
metricDNSCalls = clientmetric.NewCounter("peerapi_dns")
1456+
metricWakeOnLANCalls = clientmetric.NewCounter("peerapi_wol")
1457+
metricIngressCalls = clientmetric.NewCounter("peerapi_ingress")
1458+
)

ipn/localapi/localapi.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
146146
return
147147
}
148148
if r.Referer() != "" || r.Header.Get("Origin") != "" || !validHost(r.Host) {
149+
metricInvalidRequests.Add(1)
149150
http.Error(w, "invalid localapi request", http.StatusForbidden)
150151
return
151152
}
@@ -156,10 +157,12 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
156157
if h.RequiredPassword != "" {
157158
_, pass, ok := r.BasicAuth()
158159
if !ok {
160+
metricInvalidRequests.Add(1)
159161
http.Error(w, "auth required", http.StatusUnauthorized)
160162
return
161163
}
162164
if pass != h.RequiredPassword {
165+
metricInvalidRequests.Add(1)
163166
http.Error(w, "bad password", http.StatusForbidden)
164167
return
165168
}
@@ -895,6 +898,8 @@ func (h *Handler) serveFileTargets(w http.ResponseWriter, r *http.Request) {
895898
//
896899
// - PUT /localapi/v0/file-put/:stableID/:escaped-filename
897900
func (h *Handler) serveFilePut(w http.ResponseWriter, r *http.Request) {
901+
metricFilePutCalls.Add(1)
902+
898903
if !h.PermitWrite {
899904
http.Error(w, "file access denied", http.StatusForbidden)
900905
return
@@ -1425,3 +1430,10 @@ func defBool(a string, def bool) bool {
14251430
}
14261431
return v
14271432
}
1433+
1434+
var (
1435+
metricInvalidRequests = clientmetric.NewCounter("localapi_invalid_requests")
1436+
1437+
// User-visible LocalAPI endpoints.
1438+
metricFilePutCalls = clientmetric.NewCounter("localapi_file_put")
1439+
)

0 commit comments

Comments
 (0)