feat: support ident disk usage metric (#1100)
* feat: support ident disk usage metric * code refactor Co-authored-by: ziv <xiaozheng@tuya.com>
This commit is contained in:
parent
f34c3c6a2c
commit
b1b2c7d6b0
|
@ -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)'''
|
||||
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)'''
|
|
@ -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]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue