code refactor
This commit is contained in:
parent
05651ad744
commit
92354d5765
|
@ -2,8 +2,6 @@ package prom
|
|||
|
||||
type ClientOptions struct {
|
||||
BasicAuthUser string
|
||||
|
||||
BasicAuthPass string
|
||||
|
||||
Headers []string
|
||||
}
|
||||
|
|
|
@ -113,7 +113,7 @@ func (r RuleEval) Work() {
|
|||
var err error
|
||||
if r.rule.Algorithm == "" {
|
||||
var warnings prom.Warnings
|
||||
value, warnings, err = reader.Reader.Client.Query(context.Background(), promql, time.Now())
|
||||
value, warnings, err = reader.Client.Query(context.Background(), promql, time.Now())
|
||||
if err != nil {
|
||||
logger.Errorf("rule_eval:%d promql:%s, error:%v", r.RuleID(), promql, err)
|
||||
// 告警查询prometheus逻辑出错,发告警信息给管理员
|
||||
|
@ -558,7 +558,7 @@ func (r RecordingRuleEval) Work() {
|
|||
return
|
||||
}
|
||||
|
||||
value, warnings, err := reader.Reader.Client.Query(context.Background(), promql, time.Now())
|
||||
value, warnings, err := reader.Client.Query(context.Background(), promql, time.Now())
|
||||
if err != nil {
|
||||
logger.Errorf("recording_rule_eval:%d promql:%s, error:%v", r.RuleID(), promql, err)
|
||||
return
|
||||
|
|
|
@ -10,12 +10,7 @@ import (
|
|||
"github.com/prometheus/client_golang/api"
|
||||
)
|
||||
|
||||
type ReaderType struct {
|
||||
Opts config.ReaderOptions
|
||||
Client prom.API
|
||||
}
|
||||
|
||||
var Reader ReaderType
|
||||
var Client prom.API
|
||||
|
||||
func Init(opts config.ReaderOptions) error {
|
||||
cli, err := api.NewClient(api.Config{
|
||||
|
@ -41,14 +36,11 @@ func Init(opts config.ReaderOptions) error {
|
|||
return err
|
||||
}
|
||||
|
||||
Reader = ReaderType{
|
||||
Opts: opts,
|
||||
Client: prom.NewAPI(cli, prom.ClientOptions{
|
||||
Client = prom.NewAPI(cli, prom.ClientOptions{
|
||||
BasicAuthUser: opts.BasicAuthUser,
|
||||
BasicAuthPass: opts.BasicAuthPass,
|
||||
Headers: opts.Headers,
|
||||
}),
|
||||
}
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ func queryPromql(c *gin.Context) {
|
|||
var f promqlForm
|
||||
ginx.BindJSON(c, &f)
|
||||
|
||||
value, warnings, err := reader.Reader.Client.Query(c.Request.Context(), f.PromQL, time.Now())
|
||||
value, warnings, err := reader.Client.Query(c.Request.Context(), f.PromQL, time.Now())
|
||||
if err != nil {
|
||||
c.String(500, "promql:%s error:%v", f.PromQL, err)
|
||||
return
|
||||
|
|
|
@ -30,7 +30,7 @@ type Usage struct {
|
|||
}
|
||||
|
||||
func getSamples() (float64, error) {
|
||||
value, warns, err := reader.Reader.Client.Query(context.Background(), request, time.Now())
|
||||
value, warns, err := reader.Client.Query(context.Background(), request, time.Now())
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ func initClustersFromConfig() error {
|
|||
opts := config.C.Clusters
|
||||
|
||||
for i := 0; i < len(opts); i++ {
|
||||
cluster := newClusterTypeByOption(opts[i])
|
||||
cluster := newClusterByOption(opts[i])
|
||||
Clusters.Put(opts[i].Name, cluster)
|
||||
}
|
||||
|
||||
|
@ -165,15 +165,13 @@ func loadClustersFromAPI() {
|
|||
MaxIdleConnsPerHost: 32,
|
||||
}
|
||||
|
||||
cluster := newClusterTypeByOption(opt)
|
||||
|
||||
Clusters.Put(item.Name, cluster)
|
||||
Clusters.Put(item.Name, newClusterByOption(opt))
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newClusterTypeByOption(opt config.ClusterOptions) *ClusterType {
|
||||
func newClusterByOption(opt config.ClusterOptions) *ClusterType {
|
||||
transport := &http.Transport{
|
||||
// TLSClientConfig: tlsConfig,
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
|
@ -202,5 +200,6 @@ func newClusterTypeByOption(opt config.ClusterOptions) *ClusterType {
|
|||
Headers: opt.Headers,
|
||||
}),
|
||||
}
|
||||
|
||||
return cluster
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package router
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
|
@ -13,7 +12,7 @@ import (
|
|||
"github.com/gin-gonic/gin"
|
||||
"github.com/toolkits/pkg/ginx"
|
||||
|
||||
. "github.com/didi/nightingale/v5/src/pkg/prom"
|
||||
pkgprom "github.com/didi/nightingale/v5/src/pkg/prom"
|
||||
"github.com/didi/nightingale/v5/src/webapi/config"
|
||||
"github.com/didi/nightingale/v5/src/webapi/prom"
|
||||
"github.com/prometheus/common/model"
|
||||
|
@ -30,14 +29,8 @@ type batchQueryForm struct {
|
|||
Queries []queryFormItem `json:"queries" binding:"required"`
|
||||
}
|
||||
|
||||
type batchQueryRes struct {
|
||||
Data []model.Value `json:"data"`
|
||||
}
|
||||
|
||||
func promBatchQueryRange(c *gin.Context) {
|
||||
|
||||
xcluster := c.GetHeader("X-Cluster")
|
||||
|
||||
if xcluster == "" {
|
||||
c.String(500, "X-Cluster is blank")
|
||||
return
|
||||
|
@ -46,12 +39,35 @@ func promBatchQueryRange(c *gin.Context) {
|
|||
var f batchQueryForm
|
||||
err := c.BindJSON(&f)
|
||||
if err != nil {
|
||||
c.String(500, "%s", err.Error())
|
||||
c.String(500, err.Error())
|
||||
return
|
||||
}
|
||||
res, err := batchQueryRange(xcluster, f.Queries)
|
||||
|
||||
ginx.NewRender(c).Data(res, err)
|
||||
cluster, exist := prom.Clusters.Get(xcluster)
|
||||
if !exist {
|
||||
c.String(http.StatusBadRequest, "cluster(%s) not found", xcluster)
|
||||
return
|
||||
}
|
||||
|
||||
var lst []model.Value
|
||||
|
||||
for _, item := range f.Queries {
|
||||
r := pkgprom.Range{
|
||||
Start: time.Unix(item.Start, 0),
|
||||
End: time.Unix(item.End, 0),
|
||||
Step: time.Duration(item.Step) * time.Second,
|
||||
}
|
||||
|
||||
resp, _, err := cluster.PromClient.QueryRange(context.Background(), item.Query, r)
|
||||
if err != nil {
|
||||
c.String(500, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
lst = append(lst, resp)
|
||||
}
|
||||
|
||||
c.JSON(200, lst)
|
||||
}
|
||||
|
||||
func prometheusProxy(c *gin.Context) {
|
||||
|
@ -134,27 +150,3 @@ func clustersGets(c *gin.Context) {
|
|||
}
|
||||
ginx.NewRender(c).Data(names, nil)
|
||||
}
|
||||
|
||||
func batchQueryRange(clusterName string, data []queryFormItem) (batchQueryRes, error) {
|
||||
|
||||
var res batchQueryRes
|
||||
|
||||
clusterType, exist := prom.Clusters.Get(clusterName)
|
||||
if !exist {
|
||||
return batchQueryRes{}, errors.New("cluster client not exist")
|
||||
}
|
||||
for _, item := range data {
|
||||
|
||||
r := Range{
|
||||
Start: time.Unix(item.Start, 0),
|
||||
End: time.Unix(item.End, 0),
|
||||
Step: time.Duration(item.Step) * time.Second,
|
||||
}
|
||||
resp, _, err := clusterType.PromClient.QueryRange(context.Background(), item.Query, r)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Data = append(res.Data, resp)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue