Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/didi/nightingale
Browse files Browse the repository at this point in the history
  • Loading branch information
UlricQin committed Jul 21, 2021
2 parents 3e22aab + bedea9e commit 77c6e0d
Show file tree
Hide file tree
Showing 13 changed files with 501 additions and 76 deletions.
1 change: 1 addition & 0 deletions backend/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type DataSource interface {
PushEndpoint

QueryData(inputs vos.DataQueryParam) []*vos.DataQueryResp // 查询一段时间
QueryDataInstant(ql string) []*vos.DataQueryInstanceResp // 查询一个时间点数据 等同于prometheus instant_query
QueryTagKeys(recv vos.CommonTagQueryParam) *vos.TagKeyQueryResp // 获取标签的names
QueryTagValues(recv vos.CommonTagQueryParam) *vos.TagValueQueryResp // 根据一个label_name获取 values
QueryTagPairs(recv vos.CommonTagQueryParam) *vos.TagPairQueryResp // 根据匹配拿到所有 series 上面三个使用统一的结构体
Expand Down
51 changes: 42 additions & 9 deletions backend/prome/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
LABEL_IDENT = "ident"
LABEL_NAME = "__name__"
DEFAULT_QL = `{__name__=~".*a.*|.*e.*"}`
DEFAULT_STEP = 14
DEFAULT_STEP = 15
)

type commonQueryObj struct {
Expand Down Expand Up @@ -207,13 +207,15 @@ func (pd *PromeDataSource) QueryData(inputs vos.DataQueryParam) []*vos.DataQuery
startT := tsToUtcTs(inputs.Start)
endT := tsToUtcTs(inputs.End)

// TODO 前端传入分辨率还是后端计算,grafana和prometheus ui都是前端传入
delta := (inputs.End - inputs.Start) / 3600
if delta <= 0 {
delta = 1
resolution := time.Second * time.Duration(inputs.Step)
if inputs.Step == 0 {
// step==0 说明要自己算 grafana和prometheus ui都是前端传入
delta := (inputs.End - inputs.Start) / 3600
if delta <= 0 {
delta = 1
}
resolution = time.Second * time.Duration(delta*DEFAULT_STEP)
}
resolution := time.Second * time.Duration(delta*DEFAULT_STEP)

q, err := pd.QueryEngine.NewRangeQuery(pd.Queryable, qlStrFinal, startT, endT, resolution)
if err != nil {
logger.Errorf("[prome_query_error][QueryData_error_may_be_parse_ql_error][args:%+v][err:%+v]", input, err)
Expand Down Expand Up @@ -253,7 +255,8 @@ func (pd *PromeDataSource) QueryData(inputs vos.DataQueryParam) []*vos.DataQuery
pNum := len(m.Points)
for _, p := range m.Points {
tmpP := &vos.Point{
Timestamp: p.T,
// 毫秒时间时间戳转 秒时间戳
Timestamp: p.T / 1e3,
Value: vos.JsonFloat(p.V),
}
oneResp.Values = append(oneResp.Values, tmpP)
Expand All @@ -266,7 +269,7 @@ func (pd *PromeDataSource) QueryData(inputs vos.DataQueryParam) []*vos.DataQuery
}
tagStr = strings.TrimRight(tagStr, ",")
oneResp.Tags = tagStr
oneResp.Resolution = delta * DEFAULT_STEP
oneResp.Resolution = int64(resolution / time.Second)
oneResp.PNum = pNum
respD = append(respD, oneResp)

Expand Down Expand Up @@ -616,6 +619,36 @@ func (pd *PromeDataSource) QueryTagPairs(recv vos.CommonTagQueryParam) *vos.TagP
return respD
}

func (pd *PromeDataSource) QueryDataInstant(ql string) []*vos.DataQueryInstanceResp {
respD := make([]*vos.DataQueryInstanceResp, 0)
pv := pd.QueryVector(ql)
if pv == nil {

return respD
}

for _, s := range pv {
metricOne := make(map[string]interface{})
valueOne := make([]float64, 0)

for _, l := range s.Metric {
if l.Name == LABEL_NAME {
continue
}
metricOne[l.Name] = l.Value
}
// 毫秒时间时间戳转 秒时间戳
valueOne = append(valueOne, float64(s.Point.T)/1e3)
valueOne = append(valueOne, s.Point.V)
respD = append(respD, &vos.DataQueryInstanceResp{
Metric: metricOne,
Value: valueOne,
})

}
return respD
}

func (pd *PromeDataSource) QueryVector(ql string) promql.Vector {
t := time.Now()
q, err := pd.QueryEngine.NewInstantQuery(pd.Queryable, ql, t)
Expand Down
Loading

0 comments on commit 77c6e0d

Please sign in to comment.