未验证 提交 b1b2c7d6 编写于 作者: X xiaoziv 提交者: GitHub

feat: support ident disk usage metric (#1100)

* feat: support ident disk usage metric

* code refactor
Co-authored-by: Nziv <xiaozheng@tuya.com>
上级 f34c3c6a
......@@ -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
......@@ -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 {
......
......@@ -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]
}
}
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册