ipld-eth-server/pkg/prom/middleware.go

49 lines
936 B
Go
Raw Normal View History

package prom
import (
"net/http"
2020-10-19 20:00:09 +00:00
"time"
2020-10-19 20:00:09 +00:00
"github.com/ethereum/go-ethereum/rpc"
)
2020-10-19 20:00:09 +00:00
// HTTPMiddleware http connection metric reader
func HTTPMiddleware(next http.Handler) http.Handler {
if !metrics {
return next
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
httpCount.Inc()
2020-10-19 20:00:09 +00:00
start := time.Now()
next.ServeHTTP(w, r)
2020-10-19 20:00:09 +00:00
duration := time.Now().Sub(start)
httpDuration.Observe(float64(duration.Seconds()))
})
}
2020-10-19 20:00:09 +00:00
// WSMiddleware websocket connection counter
func WSMiddleware(next http.Handler) http.Handler {
if !metrics {
return next
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wsCount.Inc()
next.ServeHTTP(w, r)
2020-10-19 20:00:09 +00:00
wsCount.Dec()
})
}
2020-10-19 20:00:09 +00:00
// IPCMiddleware unix-socket connection counter
func IPCMiddleware(server *rpc.Server, client rpc.Conn) {
if metrics {
ipcCount.Inc()
}
server.ServeCodec(rpc.NewCodec(client), 0)
if metrics {
ipcCount.Dec()
}
}