You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
"github.com/tal-tech/go-zero/core/logx"
|
|
|
|
"github.com/tal-tech/go-zero/core/syncx"
|
|
|
|
"github.com/tal-tech/go-zero/core/threading"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
once sync.Once
|
|
|
|
enabled syncx.AtomicBool
|
|
|
|
)
|
|
|
|
|
|
|
|
// Enabled returns if prometheus is enabled.
|
|
|
|
func Enabled() bool {
|
|
|
|
return enabled.True()
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartAgent starts a prometheus agent.
|
|
|
|
func StartAgent(c Config) {
|
|
|
|
if len(c.Host) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
once.Do(func() {
|
|
|
|
enabled.Set(true)
|
|
|
|
threading.GoSafe(func() {
|
|
|
|
http.Handle(c.Path, promhttp.Handler())
|
|
|
|
addr := fmt.Sprintf("%s:%d", c.Host, c.Port)
|
|
|
|
logx.Infof("Starting prometheus agent at %s", addr)
|
|
|
|
if err := http.ListenAndServe(addr, nil); err != nil {
|
|
|
|
logx.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|