feat: add instance_query api (#731)

This commit is contained in:
ning1875 2021-07-19 16:14:52 +08:00 committed by GitHub
parent 562f3ea937
commit bf1d8b1be4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 0 deletions

View File

@ -20,6 +20,7 @@ type DataSource interface {
PushEndpoint
QueryData(inputs vos.DataQueryParam) []*vos.DataQueryResp // 查询一段时间
QueryDataInstance(ql string) []*vos.DataQueryInstanceResp // 查询一个时间点数据 等同于prometheus instance_query
QueryTagKeys(recv vos.CommonTagQueryParam) *vos.TagKeyQueryResp // 获取标签的names
QueryTagValues(recv vos.CommonTagQueryParam) *vos.TagValueQueryResp // 根据一个label_name获取 values
QueryTagPairs(recv vos.CommonTagQueryParam) *vos.TagPairQueryResp // 根据匹配拿到所有 series 上面三个使用统一的结构体

View File

@ -616,6 +616,36 @@ func (pd *PromeDataSource) QueryTagPairs(recv vos.CommonTagQueryParam) *vos.TagP
return respD
}
func (pd *PromeDataSource) QueryDataInstance(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)

View File

@ -169,6 +169,14 @@ func configRoutes(r *gin.Engine) {
pages.GET("/tpl/content", tplGet)
pages.GET("/status", Status)
pages.POST("/query", GetData)
pages.POST("/query_instance", GetDataInstance)
pages.POST("/tag-keys", GetTagKeys)
pages.POST("/tag-values", GetTagValues)
pages.POST("/tag-metrics", GetMetrics)
pages.POST("/tag-pairs", GetTagPairs)
}
// for thirdparty, do not expose location in nginx.conf
@ -288,6 +296,8 @@ func configRoutes(r *gin.Engine) {
v1.GET("/status", Status)
v1.POST("/query", GetData)
v1.POST("/query_instance", GetDataInstance)
v1.GET("/check-promql", checkPromeQl)
v1.POST("/tag-keys", GetTagKeys)
v1.POST("/tag-values", GetTagValues)

View File

@ -205,3 +205,17 @@ func GetData(c *gin.Context) {
resp := dataSource.QueryData(input)
renderData(c, resp, nil)
}
func GetDataInstance(c *gin.Context) {
dataSource, err := backend.GetDataSourceFor("")
if err != nil {
logger.Warningf("could not find datasource")
renderMessage(c, err)
return
}
var input vos.DataQueryInstanceParam
dangerous(c.ShouldBindJSON(&input))
resp := dataSource.QueryDataInstance(input.PromeQl)
renderData(c, resp, nil)
}

View File

@ -33,6 +33,10 @@ type DataQueryParam struct {
End int64 `json:"end"`
}
type DataQueryInstanceParam struct {
PromeQl string `json:"prome_ql"`
}
type DataQueryParamOne struct {
PromeQl string `json:"prome_ql"`
Idents []string `json:"idents"`
@ -59,6 +63,11 @@ type DataQueryResp struct {
PNum int `json:"pNum"`
}
type DataQueryInstanceResp struct {
Metric map[string]interface{} `json:"metric"`
Value []float64 `json:"value"`
}
type DataQL struct {
Start int64 `json:"start"`
End int64 `json:"end"`