2020-10-19 15:00:55 +00:00
|
|
|
package prom
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-10-19 20:00:09 +00:00
|
|
|
"time"
|
2020-10-19 15:00:55 +00:00
|
|
|
|
2020-10-19 20:00:09 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2020-10-19 15:00:55 +00:00
|
|
|
)
|
|
|
|
|
2020-10-19 20:00:09 +00:00
|
|
|
// HTTPMiddleware http connection metric reader
|
2020-10-19 15:00:55 +00:00
|
|
|
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()
|
2020-10-19 15:00:55 +00:00
|
|
|
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 15:00:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-19 20:00:09 +00:00
|
|
|
// WSMiddleware websocket connection counter
|
2020-10-19 15:00:55 +00:00
|
|
|
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 15:00:55 +00:00
|
|
|
})
|
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
}
|