github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/cmd/dockerd/metrics.go (about)

     1  package main
     2  
     3  import (
     4  	"net"
     5  	"net/http"
     6  	"strings"
     7  	"time"
     8  
     9  	metrics "github.com/docker/go-metrics"
    10  	"github.com/sirupsen/logrus"
    11  )
    12  
    13  func startMetricsServer(addr string) error {
    14  	if addr == "" {
    15  		return nil
    16  	}
    17  	if err := allocateDaemonPort(addr); err != nil {
    18  		return err
    19  	}
    20  	l, err := net.Listen("tcp", addr)
    21  	if err != nil {
    22  		return err
    23  	}
    24  	mux := http.NewServeMux()
    25  	mux.Handle("/metrics", metrics.Handler())
    26  	go func() {
    27  		logrus.Infof("metrics API listening on %s", l.Addr())
    28  		srv := &http.Server{
    29  			Handler:           mux,
    30  			ReadHeaderTimeout: 5 * time.Minute, // "G112: Potential Slowloris Attack (gosec)"; not a real concern for our use, so setting a long timeout.
    31  		}
    32  		if err := srv.Serve(l); err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
    33  			logrus.WithError(err).Error("error serving metrics API")
    34  		}
    35  	}()
    36  	return nil
    37  }