cmd/ursrv: Add metrics for incoming reports

This commit is contained in:
Jakob Borg 2023-10-16 16:20:47 +02:00
parent 6069cf39e5
commit 5328380691
2 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,26 @@
// Copyright (C) 2023 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package serve
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var metricReportsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "syncthing",
Subsystem: "ursrv",
Name: "reports_total",
}, []string{"version"})
func init() {
metricReportsTotal.WithLabelValues("fail")
metricReportsTotal.WithLabelValues("duplicate")
metricReportsTotal.WithLabelValues("v1")
metricReportsTotal.WithLabelValues("v2")
metricReportsTotal.WithLabelValues("v3")
}

View File

@ -11,6 +11,7 @@ import (
"database/sql" "database/sql"
"embed" "embed"
"encoding/json" "encoding/json"
"fmt"
"html/template" "html/template"
"io" "io"
"log" "log"
@ -26,6 +27,7 @@ import (
_ "github.com/lib/pq" // PostgreSQL driver _ "github.com/lib/pq" // PostgreSQL driver
"github.com/oschwald/geoip2-golang" "github.com/oschwald/geoip2-golang"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/text/cases" "golang.org/x/text/cases"
"golang.org/x/text/language" "golang.org/x/text/language"
@ -196,6 +198,7 @@ func (cli *CLI) Run() error {
http.HandleFunc("/performance.json", srv.performanceHandler) http.HandleFunc("/performance.json", srv.performanceHandler)
http.HandleFunc("/blockstats.json", srv.blockStatsHandler) http.HandleFunc("/blockstats.json", srv.blockStatsHandler)
http.HandleFunc("/locations.json", srv.locationsHandler) http.HandleFunc("/locations.json", srv.locationsHandler)
http.Handle("/metrics", promhttp.Handler())
http.Handle("/static/", http.FileServer(http.FS(statics))) http.Handle("/static/", http.FileServer(http.FS(statics)))
go srv.cacheRefresher() go srv.cacheRefresher()
@ -289,6 +292,12 @@ func (s *server) locationsHandler(w http.ResponseWriter, _ *http.Request) {
} }
func (s *server) newDataHandler(w http.ResponseWriter, r *http.Request) { func (s *server) newDataHandler(w http.ResponseWriter, r *http.Request) {
version := "fail"
defer func() {
// Version is "fail", "duplicate", "v2", "v3", ...
metricReportsTotal.WithLabelValues(version).Inc()
}()
defer r.Body.Close() defer r.Body.Close()
addr := r.Header.Get("X-Forwarded-For") addr := r.Header.Get("X-Forwarded-For")
@ -334,6 +343,7 @@ func (s *server) newDataHandler(w http.ResponseWriter, r *http.Request) {
if err.Error() == `pq: duplicate key value violates unique constraint "uniqueidjsonindex"` { if err.Error() == `pq: duplicate key value violates unique constraint "uniqueidjsonindex"` {
// We already have a report today for the same unique ID; drop // We already have a report today for the same unique ID; drop
// this one without complaining. // this one without complaining.
version = "duplicate"
return return
} }
log.Println("insert:", err) log.Println("insert:", err)
@ -343,6 +353,8 @@ func (s *server) newDataHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Database Error", http.StatusInternalServerError) http.Error(w, "Database Error", http.StatusInternalServerError)
return return
} }
version = fmt.Sprintf("v%d", rep.URVersion)
} }
func (s *server) summaryHandler(w http.ResponseWriter, r *http.Request) { func (s *server) summaryHandler(w http.ResponseWriter, r *http.Request) {