github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/cmd/dockerd/metrics.go (about)

     1  package main
     2  
     3  import (
     4  	"net"
     5  	"net/http"
     6  	"strings"
     7  
     8  	metrics "github.com/docker/go-metrics"
     9  	"github.com/sirupsen/logrus"
    10  )
    11  
    12  func startMetricsServer(addr string) error {
    13  	if addr == "" {
    14  		return nil
    15  	}
    16  	if err := allocateDaemonPort(addr); err != nil {
    17  		return err
    18  	}
    19  	l, err := net.Listen("tcp", addr)
    20  	if err != nil {
    21  		return err
    22  	}
    23  	mux := http.NewServeMux()
    24  	mux.Handle("/metrics", metrics.Handler())
    25  	go func() {
    26  		logrus.Infof("metrics API listening on %s", l.Addr())
    27  		if err := http.Serve(l, mux); err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
    28  			logrus.WithError(err).Error("error serving metrics API")
    29  		}
    30  	}()
    31  	return nil
    32  }