47 lines
921 B
Go
Executable File
47 lines
921 B
Go
Executable File
package prometheus
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"sync"
|
|
|
|
"git.wishpal.cn/wishpal_ironfan/xframe/base/syncx"
|
|
"git.wishpal.cn/wishpal_ironfan/xframe/base/threading"
|
|
"git.wishpal.cn/wishpal_ironfan/xframe/component/logger"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
)
|
|
|
|
var (
|
|
once sync.Once
|
|
enabled syncx.AtomicBool
|
|
)
|
|
|
|
// Enabled returns if prometheus is enabled.
|
|
func Enabled() bool {
|
|
return enabled.True()
|
|
}
|
|
|
|
// Enable enables prometheus.
|
|
func Enable() {
|
|
enabled.Set(true)
|
|
}
|
|
|
|
// StartAgent starts a prometheus agent.
|
|
func StartAgent(c Config) {
|
|
if len(c.Host) == 0 {
|
|
return
|
|
}
|
|
|
|
once.Do(func() {
|
|
enabled.Set(true)
|
|
threading.GoSafeVoid(func() {
|
|
http.Handle(c.Path, promhttp.Handler())
|
|
addr := fmt.Sprintf("%s:%d", c.Host, c.Port)
|
|
logger.Infof("Starting prometheus agent at %s", addr)
|
|
if err := http.ListenAndServe(addr, nil); err != nil {
|
|
logger.Errorln(err)
|
|
}
|
|
})
|
|
})
|
|
}
|