Commit 6e4b2ca5 authored by 李科's avatar 李科

feat: 普罗米修斯指标标签值

parent 61c8a2bc
...@@ -3,3 +3,9 @@ package request ...@@ -3,3 +3,9 @@ package request
type PrometheusLabel struct { type PrometheusLabel struct {
LabelName string `json:"label_name" form:"label_name"` LabelName string `json:"label_name" form:"label_name"`
} }
type PrometheusLabelValue struct {
MetricName string `json:"metric_name" form:"metric_name" binding:"required"`
MetricLabel string `json:"metric_label" form:"metric_label" binding:"required"`
Value string `json:"value" form:"value"`
}
...@@ -23,3 +23,19 @@ func PrometheusLabel(c *gin.Context) { ...@@ -23,3 +23,19 @@ func PrometheusLabel(c *gin.Context) {
} }
SendJsonResponse(c, resp.OK, data) SendJsonResponse(c, resp.OK, data)
} }
func PrometheusLabelValue(c *gin.Context) {
var req request.PrometheusLabelValue
if err := c.ShouldBind(&req); err != nil {
SendJsonResponse(c, resp.InvalidParam.TranslateError(err), nil)
return
}
svc := service.PrometheusSvc{User: header.GetUser(c)}
data, err := svc.LabelValue(req)
if err != nil {
SendJsonResponse(c, resp.FAIL.WithError(err), nil)
return
}
SendJsonResponse(c, resp.OK, data)
}
...@@ -12,5 +12,6 @@ func InitPrometheusRouter(e *gin.Engine) { ...@@ -12,5 +12,6 @@ func InitPrometheusRouter(e *gin.Engine) {
group := e.Group(fmt.Sprintf("%s/prometheus", conf.Options.Prefix)) group := e.Group(fmt.Sprintf("%s/prometheus", conf.Options.Prefix))
{ {
group.GET("", controller.PrometheusLabel) group.GET("", controller.PrometheusLabel)
group.GET("value", controller.PrometheusLabelValue)
} }
} }
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/util" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/util"
"net/http" "net/http"
"sort" "sort"
"strings"
) )
type PrometheusSvc struct { type PrometheusSvc struct {
...@@ -48,3 +49,34 @@ func (p *PrometheusSvc) Label(req request.PrometheusLabel) (resp response.Promet ...@@ -48,3 +49,34 @@ func (p *PrometheusSvc) Label(req request.PrometheusLabel) (resp response.Promet
return return
} }
func (p *PrometheusSvc) LabelValue(req request.PrometheusLabelValue) (resp response.PrometheusList, err error) {
var (
prometheusSeries response.PrometheusSeries
metricLabelMap = make(map[string][]string, 0)
)
url := fmt.Sprintf("%s%s", conf.Options.PrometheusHost, "/api/v1/series")
bytes, _ := util.Request(url, http.MethodPost,
[]byte(fmt.Sprintf("match[]=%s", req.MetricName)),
map[string]string{"Content-Type": util.MediaTypeForm})
_ = json.Unmarshal(bytes, &prometheusSeries)
for _, v := range prometheusSeries.Data {
for key, value := range v {
metricLabelMap[key] = append(metricLabelMap[key], value)
}
}
if values, ok := metricLabelMap[req.MetricLabel]; ok {
values = funk.UniqString(values)
if req.Value != "" {
values = funk.FilterString(values, func(x string) bool {
return strings.Contains(x, req.Value)
})
}
resp.List = values
}
sort.Strings(resp.List)
resp.TotalCount = int64(len(resp.List))
return
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment