metrics.go 676 B

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