From 812ceb0fa02d14b391963d2a990e115f5e551a31 Mon Sep 17 00:00:00 2001 From: Daniel N <2color@users.noreply.github.com> Date: Wed, 31 Jul 2024 17:20:37 +0200 Subject: [PATCH] feat: add instrumentation --- main.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 785156b..a57b8c2 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,9 @@ import ( "net/http" "os" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/urfave/cli/v2" ) @@ -50,17 +53,16 @@ func startServer(ctx context.Context, d *daemon, tcpListener string) error { return err } + log.Printf("listening on %v\n", l.Addr()) + log.Printf("Libp2p host peer id %s\n", d.h.ID()) + log.Printf("Libp2p host listening on %v\n", d.h.Addrs()) log.Printf("listening on %v\n", l.Addr()) d.mustStart() log.Printf("ready to start serving") - // 1. Is the peer findable in the DHT? - // 2. Does the multiaddr work? If not, what's the error? - // 3. Is the CID in the DHT? - // 4. Does the peer respond that it has the given data over Bitswap? - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + checkHandler := func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Access-Control-Allow-Origin", "*") data, err := d.runCheck(r.URL.Query()) if err == nil { @@ -70,8 +72,60 @@ func startServer(ctx context.Context, d *daemon, tcpListener string) error { w.WriteHeader(http.StatusInternalServerError) _, _ = w.Write([]byte(err.Error())) } + } + + // Create a custom registry + reg := prometheus.NewRegistry() + + requestsTotal := prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "http_requests_total", + Help: "Total number of slow requests", + }, + []string{"code"}, + ) + + requestDuration := prometheus.NewHistogramVec( + prometheus.HistogramOpts{ + Name: "http_request_duration_seconds", + Help: "Duration of slow requests", + Buckets: prometheus.DefBuckets, + }, + []string{"code"}, + ) + + requestsInFlight := prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "slow_requests_in_flight", + Help: "Number of slow requests currently being served", }) + // Register metrics with our custom registry + reg.MustRegister(requestsTotal) + reg.MustRegister(requestDuration) + reg.MustRegister(requestsInFlight) + // Instrument the slowHandler + instrumentedHandler := promhttp.InstrumentHandlerCounter( + requestsTotal, + promhttp.InstrumentHandlerDuration( + requestDuration, + promhttp.InstrumentHandlerInFlight( + requestsInFlight, + http.HandlerFunc(checkHandler), + ), + ), + ) + + // 1. Is the peer findable in the DHT? + // 2. Does the multiaddr work? If not, what's the error? + // 3. Is the CID in the DHT? + // 4. Does the peer respond that it has the given data over Bitswap? + http.Handle("/check", instrumentedHandler) + http.Handle("/debug/libp2p", promhttp.Handler()) + http.Handle("/debug/http", promhttp.HandlerFor( + reg, + promhttp.HandlerOpts{}, + )) + done := make(chan error, 1) go func() { defer close(done)