Skip to content

Commit

Permalink
feat: add instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
2color committed Jul 31, 2024
1 parent 1613bb5 commit 812ceb0
Showing 1 changed file with 59 additions and 5 deletions.
64 changes: 59 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 812ceb0

Please sign in to comment.