agent.go 579 B

123456789101112131415161718192021222324252627282930
  1. package prometheus
  2. import (
  3. "fmt"
  4. "net/http"
  5. "sync"
  6. "github.com/prometheus/client_golang/prometheus/promhttp"
  7. "github.com/tal-tech/go-zero/core/logx"
  8. "github.com/tal-tech/go-zero/core/threading"
  9. )
  10. var once sync.Once
  11. func StartAgent(c Config) {
  12. once.Do(func() {
  13. if len(c.Host) == 0 {
  14. return
  15. }
  16. threading.GoSafe(func() {
  17. http.Handle(c.Path, promhttp.Handler())
  18. addr := fmt.Sprintf("%s:%d", c.Host, c.Port)
  19. logx.Infof("Starting prometheus agent at %s", addr)
  20. if err := http.ListenAndServe(addr, nil); err != nil {
  21. logx.Error(err)
  22. }
  23. })
  24. })
  25. }