mirror of
https://github.com/sot-tech/mochi.git
synced 2026-06-21 03:38:26 -07:00
(minor) separate metrics server creation and start, allow pretty print log to file
This commit is contained in:
+3
-4
@@ -34,12 +34,11 @@ func ConfigureLogger(output, level string, formatted, colored bool) (err error)
|
||||
lvl := zerolog.WarnLevel
|
||||
output = strings.ToLower(output)
|
||||
var w io.Writer
|
||||
var stdAny bool
|
||||
switch output {
|
||||
case "stderr", "":
|
||||
w, stdAny = os.Stderr, true
|
||||
w = os.Stderr
|
||||
case "stdout":
|
||||
w, stdAny = os.Stdout, true
|
||||
w = os.Stdout
|
||||
default:
|
||||
if w, err = os.OpenFile(output, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o600); err == nil {
|
||||
customOutMu.Lock()
|
||||
@@ -52,7 +51,7 @@ func ConfigureLogger(output, level string, formatted, colored bool) (err error)
|
||||
return err
|
||||
}
|
||||
}
|
||||
if stdAny && formatted {
|
||||
if formatted {
|
||||
w = zerolog.ConsoleWriter{
|
||||
Out: w,
|
||||
NoColor: !colored,
|
||||
|
||||
+29
-14
@@ -36,7 +36,8 @@ func Enabled() bool {
|
||||
// Server represents a standalone HTTP server for serving a Prometheus metrics
|
||||
// endpoint.
|
||||
type Server struct {
|
||||
srv *fasthttp.Server
|
||||
listen string
|
||||
srv *fasthttp.Server
|
||||
}
|
||||
|
||||
// AddressFamily returns the label value for reporting the address family of an IP address.
|
||||
@@ -51,14 +52,37 @@ func AddressFamily(ip netip.Addr) string {
|
||||
}
|
||||
}
|
||||
|
||||
// Start starts metrics/profiling server
|
||||
func (s *Server) Start() (err error) {
|
||||
atomic.AddInt32(serverCounter, 1)
|
||||
defer atomic.AddInt32(serverCounter, -1)
|
||||
if err = s.srv.ListenAndServe(s.listen); err != nil {
|
||||
if errors.Is(err, http.ErrServerClosed) {
|
||||
err = nil
|
||||
} else {
|
||||
logger.Error().Err(err).Msg("failed while serving prometheus")
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Close shuts down the server.
|
||||
func (s *Server) Close() error {
|
||||
return s.srv.Shutdown()
|
||||
}
|
||||
|
||||
// NewServer creates a new instance of a Prometheus server that asynchronously
|
||||
// serves requests.
|
||||
// NewServer creates new metrics/profiling server and starts it.
|
||||
// Equivalent of New and async Server.Start.
|
||||
func NewServer(addr string) *Server {
|
||||
s := New(addr)
|
||||
go func() {
|
||||
_ = s.Start()
|
||||
}()
|
||||
return s
|
||||
}
|
||||
|
||||
// New creates a new instance of a Prometheus and pprof
|
||||
func New(addr string) *Server {
|
||||
if len(addr) == 0 {
|
||||
panic("metrics listen address not provided")
|
||||
}
|
||||
@@ -71,7 +95,8 @@ func NewServer(addr string) *Server {
|
||||
r.GET("/debug/pprof/symbol", fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Symbol))
|
||||
r.GET("/debug/pprof/trace", fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Trace))
|
||||
|
||||
s := &Server{
|
||||
return &Server{
|
||||
listen: addr,
|
||||
srv: &fasthttp.Server{
|
||||
Handler: r.Handler,
|
||||
GetOnly: true,
|
||||
@@ -79,14 +104,4 @@ func NewServer(addr string) *Server {
|
||||
WriteTimeout: writeTimeout,
|
||||
},
|
||||
}
|
||||
|
||||
go func() {
|
||||
atomic.AddInt32(serverCounter, 1)
|
||||
defer atomic.AddInt32(serverCounter, -1)
|
||||
if err := s.srv.ListenAndServe(addr); !errors.Is(err, http.ErrServerClosed) {
|
||||
logger.Error().Err(err).Msg("failed while serving prometheus")
|
||||
}
|
||||
}()
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user