mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-28 10:08:11 -07:00
switch prometheus listener to fasthttp
This commit is contained in:
+22
-18
@@ -3,7 +3,6 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/pprof"
|
||||
@@ -11,13 +10,16 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/fasthttp/router"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/valyala/fasthttp"
|
||||
"github.com/valyala/fasthttp/fasthttpadaptor"
|
||||
|
||||
"github.com/sot-tech/mochi/pkg/log"
|
||||
)
|
||||
|
||||
const (
|
||||
readTimeout = 5 * time.Second
|
||||
readTimeout = time.Minute
|
||||
writeTimeout = readTimeout * 2
|
||||
)
|
||||
|
||||
@@ -34,7 +36,7 @@ func Enabled() bool {
|
||||
// Server represents a standalone HTTP server for serving a Prometheus metrics
|
||||
// endpoint.
|
||||
type Server struct {
|
||||
srv *http.Server
|
||||
srv *fasthttp.Server
|
||||
}
|
||||
|
||||
// AddressFamily returns the label value for reporting the address family of an IP address.
|
||||
@@ -51,35 +53,37 @@ func AddressFamily(ip netip.Addr) string {
|
||||
|
||||
// Close shuts down the server.
|
||||
func (s *Server) Close() error {
|
||||
return s.srv.Shutdown(context.Background())
|
||||
return s.srv.Shutdown()
|
||||
}
|
||||
|
||||
// NewServer creates a new instance of a Prometheus server that asynchronously
|
||||
// serves requests.
|
||||
func NewServer(addr string) *Server {
|
||||
mux := http.NewServeMux()
|
||||
if len(addr) == 0 {
|
||||
panic("metrics listen address not provided")
|
||||
}
|
||||
|
||||
mux.Handle("/metrics", promhttp.Handler())
|
||||
mux.HandleFunc("/debug/pprof/", pprof.Index)
|
||||
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
|
||||
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
|
||||
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
|
||||
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
|
||||
r := router.New()
|
||||
r.GET("/metrics", fasthttpadaptor.NewFastHTTPHandler(promhttp.Handler()))
|
||||
r.GET("/debug/pprof/", fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Index))
|
||||
r.GET("/debug/pprof/cmdline", fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Cmdline))
|
||||
r.GET("/debug/pprof/profile", fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Profile))
|
||||
r.GET("/debug/pprof/symbol", fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Symbol))
|
||||
r.GET("/debug/pprof/trace", fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Trace))
|
||||
|
||||
s := &Server{
|
||||
srv: &http.Server{
|
||||
Addr: addr,
|
||||
Handler: mux,
|
||||
ReadTimeout: readTimeout,
|
||||
ReadHeaderTimeout: readTimeout,
|
||||
WriteTimeout: writeTimeout,
|
||||
srv: &fasthttp.Server{
|
||||
Handler: r.Handler,
|
||||
GetOnly: true,
|
||||
ReadTimeout: readTimeout,
|
||||
WriteTimeout: writeTimeout,
|
||||
},
|
||||
}
|
||||
|
||||
go func() {
|
||||
atomic.AddInt32(serverCounter, 1)
|
||||
defer atomic.AddInt32(serverCounter, -1)
|
||||
if err := s.srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
|
||||
if err := s.srv.ListenAndServe(addr); !errors.Is(err, http.ErrServerClosed) {
|
||||
logger.Error().Err(err).Msg("failed while serving prometheus")
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user