فهرست منبع

cmd/stcrashreceiver: Add metrics for incoming reports

Jakob Borg 2 سال پیش
والد
کامیت
a8b9096353
3فایلهای تغییر یافته به همراه42 افزوده شده و 0 حذف شده
  1. 8 0
      cmd/stcrashreceiver/main.go
  2. 25 0
      cmd/stcrashreceiver/metrics.go
  3. 9 0
      cmd/stcrashreceiver/stcrashreceiver.go

+ 8 - 0
cmd/stcrashreceiver/main.go

@@ -24,6 +24,7 @@ import (
 	"time"
 	"time"
 
 
 	"github.com/alecthomas/kong"
 	"github.com/alecthomas/kong"
+	"github.com/prometheus/client_golang/prometheus/promhttp"
 	"github.com/syncthing/syncthing/lib/sha256"
 	"github.com/syncthing/syncthing/lib/sha256"
 	"github.com/syncthing/syncthing/lib/ur"
 	"github.com/syncthing/syncthing/lib/ur"
 
 
@@ -72,6 +73,7 @@ func main() {
 	mux.HandleFunc("/ping", func(w http.ResponseWriter, req *http.Request) {
 	mux.HandleFunc("/ping", func(w http.ResponseWriter, req *http.Request) {
 		w.Write([]byte("OK"))
 		w.Write([]byte("OK"))
 	})
 	})
+	mux.Handle("/metrics", promhttp.Handler())
 
 
 	if params.DSN != "" {
 	if params.DSN != "" {
 		mux.HandleFunc("/newcrash/failure", handleFailureFn(params.DSN, filepath.Join(params.Dir, "failure_reports")))
 		mux.HandleFunc("/newcrash/failure", handleFailureFn(params.DSN, filepath.Join(params.Dir, "failure_reports")))
@@ -85,6 +87,11 @@ func main() {
 
 
 func handleFailureFn(dsn, failureDir string) func(w http.ResponseWriter, req *http.Request) {
 func handleFailureFn(dsn, failureDir string) func(w http.ResponseWriter, req *http.Request) {
 	return func(w http.ResponseWriter, req *http.Request) {
 	return func(w http.ResponseWriter, req *http.Request) {
+		result := "failure"
+		defer func() {
+			metricFailureReportsTotal.WithLabelValues(result).Inc()
+		}()
+
 		lr := io.LimitReader(req.Body, maxRequestSize)
 		lr := io.LimitReader(req.Body, maxRequestSize)
 		bs, err := io.ReadAll(lr)
 		bs, err := io.ReadAll(lr)
 		req.Body.Close()
 		req.Body.Close()
@@ -135,6 +142,7 @@ func handleFailureFn(dsn, failureDir string) func(w http.ResponseWriter, req *ht
 				log.Println("Failed to send failure report:", err)
 				log.Println("Failed to send failure report:", err)
 			} else {
 			} else {
 				log.Println("Sent failure report:", r.Description)
 				log.Println("Sent failure report:", r.Description)
+				result = "success"
 			}
 			}
 		}
 		}
 	}
 	}

+ 25 - 0
cmd/stcrashreceiver/metrics.go

@@ -0,0 +1,25 @@
+// 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 main
+
+import (
+	"github.com/prometheus/client_golang/prometheus"
+	"github.com/prometheus/client_golang/prometheus/promauto"
+)
+
+var (
+	metricCrashReportsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
+		Namespace: "syncthing",
+		Subsystem: "crashreceiver",
+		Name:      "crash_reports_total",
+	}, []string{"result"})
+	metricFailureReportsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
+		Namespace: "syncthing",
+		Subsystem: "crashreceiver",
+		Name:      "failure_reports_total",
+	}, []string{"result"})
+)

+ 9 - 0
cmd/stcrashreceiver/stcrashreceiver.go

@@ -71,6 +71,11 @@ func (r *crashReceiver) serveHead(reportID string, w http.ResponseWriter, _ *htt
 
 
 // servePut accepts and stores the given report.
 // servePut accepts and stores the given report.
 func (r *crashReceiver) servePut(reportID string, w http.ResponseWriter, req *http.Request) {
 func (r *crashReceiver) servePut(reportID string, w http.ResponseWriter, req *http.Request) {
+	result := "receive_failure"
+	defer func() {
+		metricCrashReportsTotal.WithLabelValues(result).Inc()
+	}()
+
 	// Read at most maxRequestSize of report data.
 	// Read at most maxRequestSize of report data.
 	log.Println("Receiving report", reportID)
 	log.Println("Receiving report", reportID)
 	lr := io.LimitReader(req.Body, maxRequestSize)
 	lr := io.LimitReader(req.Body, maxRequestSize)
@@ -81,13 +86,17 @@ func (r *crashReceiver) servePut(reportID string, w http.ResponseWriter, req *ht
 		return
 		return
 	}
 	}
 
 
+	result = "success"
+
 	// Store the report
 	// Store the report
 	if !r.store.Put(reportID, bs) {
 	if !r.store.Put(reportID, bs) {
 		log.Println("Failed to store report (queue full):", reportID)
 		log.Println("Failed to store report (queue full):", reportID)
+		result = "queue_failure"
 	}
 	}
 
 
 	// Send the report to Sentry
 	// Send the report to Sentry
 	if !r.sentry.Send(reportID, userIDFor(req), bs) {
 	if !r.sentry.Send(reportID, userIDFor(req), bs) {
 		log.Println("Failed to send report to sentry (queue full):", reportID)
 		log.Println("Failed to send report to sentry (queue full):", reportID)
+		result = "sentry_failure"
 	}
 	}
 }
 }