62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package data
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/config"
|
|
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/pkg/log"
|
|
"github.com/prometheus/client_golang/api"
|
|
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
|
"net"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
var V1api v1.API
|
|
|
|
type authRoundTripper struct {
|
|
auth string
|
|
originalRT http.RoundTripper
|
|
}
|
|
|
|
func (rt *authRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
req.Header.Set("Authorization", rt.auth)
|
|
return rt.originalRT.RoundTrip(req)
|
|
}
|
|
|
|
func newAuthRoundTripper(auth *config.PrometheusConfig, rt http.RoundTripper) http.RoundTripper {
|
|
encoded := base64.StdEncoding.EncodeToString([]byte(auth.User + ":" + auth.Password))
|
|
return &authRoundTripper{auth: "Basic " + encoded, originalRT: rt}
|
|
}
|
|
|
|
func initPrometheus() {
|
|
if !config.Config.CfnConfig.Enable {
|
|
return
|
|
}
|
|
|
|
apiConfig := api.Config{
|
|
Address: config.Config.Prometheus.Url,
|
|
}
|
|
|
|
if config.Config.Prometheus.AuthEnable {
|
|
roundTripper := &http.Transport{
|
|
Proxy: http.ProxyFromEnvironment,
|
|
DialContext: (&net.Dialer{
|
|
Timeout: 30 * time.Second,
|
|
KeepAlive: 30 * time.Second,
|
|
}).DialContext,
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
}
|
|
|
|
apiConfig.RoundTripper = newAuthRoundTripper(&config.Config.Prometheus, roundTripper)
|
|
}
|
|
|
|
log.Infof("Prometheus客户端初始化, 配置: %s", apiConfig.Address)
|
|
client, err := api.NewClient(apiConfig)
|
|
|
|
if err != nil {
|
|
log.Info("Error creating prometheus client: ", err.Error())
|
|
}
|
|
|
|
V1api = v1.NewAPI(client)
|
|
}
|