From b1b2c7d6b0037114c87a9932b36ac3914118ff72 Mon Sep 17 00:00:00 2001 From: xiaoziv Date: Wed, 10 Aug 2022 17:00:49 +0800 Subject: [PATCH] feat: support ident disk usage metric (#1100) * feat: support ident disk usage metric * code refactor Co-authored-by: ziv --- etc/webapi.conf | 3 +- src/models/target.go | 7 ++-- src/webapi/router/router_target.go | 58 +++++++++++++----------------- 3 files changed, 30 insertions(+), 38 deletions(-) diff --git a/etc/webapi.conf b/etc/webapi.conf index 13d6c8fa..291f65d7 100644 --- a/etc/webapi.conf +++ b/etc/webapi.conf @@ -206,4 +206,5 @@ Timeout = 3000 [TargetMetrics] TargetUp = '''max(max_over_time(target_up{ident=~"(%s)"}[%dm])) by (ident)''' LoadPerCore = '''max(max_over_time(system_load_norm_1{ident=~"(%s)"}[%dm])) by (ident)''' -MemUtil = '''100-max(max_over_time(mem_available_percent{ident=~"(%s)"}[%dm])) by (ident)''' \ No newline at end of file +MemUtil = '''100-max(max_over_time(mem_available_percent{ident=~"(%s)"}[%dm])) by (ident)''' +DiskUsedPercent = '''max(max_over_time(disk_used_percent{ident=~"(%s)", path="/"}[%dm])) by (ident)''' \ No newline at end of file diff --git a/src/models/target.go b/src/models/target.go index 45e35625..bfd1a16b 100644 --- a/src/models/target.go +++ b/src/models/target.go @@ -21,9 +21,10 @@ type Target struct { TagsMap map[string]string `json:"-" gorm:"-"` // internal use, append tags to series UpdateAt int64 `json:"update_at"` - LoadPerCore float64 `json:"load_per_core" gorm:"-"` - MemUtil float64 `json:"mem_util" gorm:"-"` - TargetUp float64 `json:"target_up" gorm:"-"` + LoadPerCore float64 `json:"load_per_core" gorm:"-"` + MemUtil float64 `json:"mem_util" gorm:"-"` + TargetUp float64 `json:"target_up" gorm:"-"` + DiskUsedPercent float64 `json:"disk_used_percent" gorm:"-"` } func (t *Target) TableName() string { diff --git a/src/webapi/router/router_target.go b/src/webapi/router/router_target.go index dac8daf1..46318de4 100644 --- a/src/webapi/router/router_target.go +++ b/src/webapi/router/router_target.go @@ -40,7 +40,7 @@ func targetGets(c *gin.Context) { now := time.Now() - // query LoadPerCore / MemUtil / TargetUp from prometheus + // query LoadPerCore / MemUtil / TargetUp / DiskUsedPercent from prometheus // map key: cluster, map value: ident list targets := make(map[string][]string) for i := 0; i < len(list); i++ { @@ -58,42 +58,32 @@ func targetGets(c *gin.Context) { continue } - // load per core - promql := fmt.Sprintf(config.C.TargetMetrics["LoadPerCore"], strings.Join(targetArr, "|"), mins) - values, err := instantQuery(c, cc, promql, now) - ginx.Dangerous(err) + targetRe := strings.Join(targetArr, "|") + valuesMap := make(map[string]map[string]float64) - for ident := range values { - mapkey := cluster + ident - t, has := targetsMap[mapkey] - if has { - t.LoadPerCore = values[ident] - } + for metric, ql := range config.C.TargetMetrics { + promql := fmt.Sprintf(ql, targetRe, mins) + values, err := instantQuery(context.Background(), cc, promql, now) + ginx.Dangerous(err) + valuesMap[metric] = values } - // mem util - promql = fmt.Sprintf(config.C.TargetMetrics["MemUtil"], strings.Join(targetArr, "|"), mins) - values, err = instantQuery(c, cc, promql, now) - ginx.Dangerous(err) - - for ident := range values { - mapkey := cluster + ident - t, has := targetsMap[mapkey] - if has { - t.MemUtil = values[ident] - } - } - - // target up - promql = fmt.Sprintf(config.C.TargetMetrics["TargetUp"], strings.Join(targetArr, "|"), mins) - values, err = instantQuery(c, cc, promql, now) - ginx.Dangerous(err) - - for ident := range values { - mapkey := cluster + ident - t, has := targetsMap[mapkey] - if has { - t.TargetUp = values[ident] + // handle values + for metric, values := range valuesMap { + for ident := range values { + mapkey := cluster + ident + if t, has := targetsMap[mapkey]; has { + switch metric { + case "LoadPerCore": + t.LoadPerCore = values[ident] + case "MemUtil": + t.MemUtil = values[ident] + case "TargetUp": + t.TargetUp = values[ident] + case "DiskUsedPercent": + t.DiskUsedPercent = values[ident] + } + } } } }