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