Commit fb038039 authored by 李科's avatar 李科

feat: 普罗米修斯指标&标签查询

parent 0110717b
package request
type PrometheusLabel struct {
LabelName string `json:"label_name" form:"label_name"`
}
package response
type PrometheusItem struct {
Serial
}
type PrometheusList struct {
TotalCount int64 `json:"total_count"`
List []string `json:"list"`
}
type PrometheusLabel struct {
Status string `json:"status"`
Data []string `json:"data"`
}
type PrometheusSeries struct {
Status string `json:"status"`
Data []map[string]string `json:"data"`
}
type Serial struct {
Name string `json:"name"`
Value string `json:"value,omitempty"`
Children []Serial `json:"children,omitempty"`
}
...@@ -31,6 +31,7 @@ type Config struct { ...@@ -31,6 +31,7 @@ type Config struct {
MinioSecretKey string MinioSecretKey string
MinioBucket string MinioBucket string
TempDirPrefix string TempDirPrefix string
PrometheusHost string
} }
const ( const (
......
package controller
import (
"github.com/gin-gonic/gin"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/router/middleware/header"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/service"
)
func PrometheusLabel(c *gin.Context) {
var req request.PrometheusLabel
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.Label(req)
if err != nil {
SendJsonResponse(c, resp.FAIL.WithError(err), nil)
return
}
SendJsonResponse(c, resp.OK, data)
}
...@@ -54,17 +54,18 @@ func initConfig() { ...@@ -54,17 +54,18 @@ func initConfig() {
RedisURL: util.SetEnvStr("REDIS_URL", "localhost:7001"), RedisURL: util.SetEnvStr("REDIS_URL", "localhost:7001"),
RedisDB: 0, RedisDB: 0,
RedisTag: "bg", RedisTag: "bg",
LogDirPrefix: util.SetEnvStr("LOG_DIR_PREFIX", "/app/log"), // 日志目录 LogDirPrefix: util.SetEnvStr("LOG_DIR_PREFIX", "/app/log"), // 日志目录
LogDirName: util.SetEnvStr("LOG_NAME", "syslog"), // 日志名称 LogDirName: util.SetEnvStr("LOG_NAME", "syslog"), // 日志名称
LogSaveDays: util.SetEnvInt("LOG_SAVE_DAYS", 7), // 日志最大存储天数 LogSaveDays: util.SetEnvInt("LOG_SAVE_DAYS", 7), // 日志最大存储天数
LogMode: util.SetEnvInt("LOG_MODE", 1), // 1.标准打印 2.输出文件 LogMode: util.SetEnvInt("LOG_MODE", 1), // 1.标准打印 2.输出文件
ArgBool: util.SetEnvBool("ARG_BOOL", false), // 示例参数 ArgBool: util.SetEnvBool("ARG_BOOL", false), // 示例参数
ArgInt: util.SetEnvInt("ARG_INT", 10), // 示例参数 ArgInt: util.SetEnvInt("ARG_INT", 10), // 示例参数
MinioServer: util.SetEnvStr("MINIO_SERVER", "cache.wodcloud.com"), // Minio 服务地址 MinioServer: util.SetEnvStr("MINIO_SERVER", "cache.wodcloud.com"), // Minio 服务地址
MinioAccessKey: util.SetEnvStr("MINIO_ACCESS_KEY", "beagleadmin"), // Minio Access Key MinioAccessKey: util.SetEnvStr("MINIO_ACCESS_KEY", "beagleadmin"), // Minio Access Key
MinioSecretKey: util.SetEnvStr("MINIO_SECRET_KEY", "H76cPmwvH7vJ"), // Minio Secret MinioSecretKey: util.SetEnvStr("MINIO_SECRET_KEY", "H76cPmwvH7vJ"), // Minio Secret
MinioBucket: util.SetEnvStr("MINIO_BUCKET", "so-operation"), // Minio Bucket MinioBucket: util.SetEnvStr("MINIO_BUCKET", "so-operation"), // Minio Bucket
TempDirPrefix: util.SetEnvStr("TEMP_DIR_PREFIX", "/app/xlsx/"), //模板目录前缀 TempDirPrefix: util.SetEnvStr("TEMP_DIR_PREFIX", "/app/xlsx/"), //模板目录前缀
PrometheusHost: util.SetEnvStr("PROMETHEUS_HOST", "https://prometheus.wodcloud.com"), // Prometheus Host
} }
} }
......
package router
import (
"fmt"
"github.com/gin-gonic/gin"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/conf"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/controller"
)
// InitPrometheusRouter 初始化prometheus路由
func InitPrometheusRouter(e *gin.Engine) {
group := e.Group(fmt.Sprintf("%s/prometheus", conf.Options.Prefix))
{
group.GET("", controller.PrometheusLabel)
}
}
...@@ -38,4 +38,6 @@ func Load(r *gin.Engine, middleware ...gin.HandlerFunc) { ...@@ -38,4 +38,6 @@ func Load(r *gin.Engine, middleware ...gin.HandlerFunc) {
InitOrganizationRouter(r) InitOrganizationRouter(r)
// 初始化指标配置路由 // 初始化指标配置路由
InitMetricConfigRouter(r) InitMetricConfigRouter(r)
// 初始化prometheus路由
InitPrometheusRouter(r)
} }
package service
import (
"fmt"
json "github.com/json-iterator/go"
"github.com/thoas/go-funk"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/conf"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/util"
"net/http"
"sort"
)
type PrometheusSvc struct {
User entity.SystemUserInfo
}
func (m *PrometheusSvc) Label(req request.PrometheusLabel) (resp response.PrometheusList, err error) {
var (
prometheusLabel response.PrometheusLabel
prometheusSeries response.PrometheusSeries
)
if req.LabelName != "" {
url := fmt.Sprintf("%s%s", conf.Options.PrometheusHost, "/api/v1/series")
bytes, _ := util.Request(url, http.MethodPost,
[]byte(fmt.Sprintf("match[]=%s", req.LabelName)),
map[string]string{"Content-Type": util.MediaTypeForm})
_ = json.Unmarshal(bytes, &prometheusSeries)
for _, v := range prometheusSeries.Data {
for k, _ := range v {
resp.List = append(resp.List, k)
}
}
resp.List = funk.UniqString(resp.List)
sort.Strings(resp.List)
resp.TotalCount = int64(len(resp.List))
} else {
url := fmt.Sprintf("%s%s", conf.Options.PrometheusHost, "/api/v1/label/__name__/values")
bytes, _ := util.Request(url, http.MethodGet, nil, nil)
_ = json.Unmarshal(bytes, &prometheusLabel)
resp.TotalCount = int64(len(prometheusLabel.Data))
resp.List = prometheusLabel.Data
}
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