Skip to content

Commit

Permalink
fix(monitor): support configure vm alert rule (#1914)
Browse files Browse the repository at this point in the history
1. fix alert rule expression for vm
2. add vm name in alert message
3. round values in prometheus alert annotation to two decimals

Signed-off-by: willzgli <[email protected]>

Co-authored-by: willzgli <[email protected]>
  • Loading branch information
willzgli and willzgli authored May 8, 2022
1 parent cf9bda9 commit cfdac24
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/monitor/services/rest/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const (
alarmPolicyTypeCluster = "cluster"
alarmPolicyTypeNode = "node"
alarmPolicyTypePod = "pod"
alarmPolicyTypeVM = "virtualMachine"
alarmPolicyTypeKey = "alarmPolicyType"
alarmObjectsTypeKey = "alarmObjectsType"
filterNamespaceKey = "namespace"
Expand Down Expand Up @@ -350,6 +351,21 @@ func (r *AlarmMetric) GetExpr(alarmPolicy *AlarmPolicy) string {
} else {
metric = r.MetricName
}
case alarmPolicyTypeVM:
filter := MetricFilter{}
if alarmPolicy.AlarmPolicySettings.AlarmObjects != "" {
alarmObjects := strings.Split(alarmPolicy.AlarmPolicySettings.AlarmObjects, ",")
filter.WorkloadName = strings.Join(alarmObjects, "|")
}
if alarmPolicy.Namespace != "" {
filter.Namespace = alarmPolicy.Namespace
}
if filter.WorkloadName != "" || filter.Namespace != "" {
metric = fmt.Sprintf("%s{%s}", r.MetricName, filter.ToStr())
metric = strings.ReplaceAll(metric, "workload_name", "name") //In vm metrics label, use name to replace workload_name
} else {
metric = r.MetricName
}
}
var value string
b, err := parseBool(r.Evaluator.Value)
Expand Down Expand Up @@ -407,6 +423,10 @@ func (r *AlarmMetric) GetAnnotations(alarmPolicy *AlarmPolicy) map[string]string
annotations[alarmObjectsTypeKey] = alarmPolicySettings.AlarmObjectsType
annotations[measurementKey] = r.Measurement
annotations[valueKey] = valueStr

if r.Unit == "%" { // if value is a percentage type, format the value for readable
annotations[valueKey] = "{{ $value | printf \"%.2f\" }}"
}
annotations[metricDisplayNameKey] = r.MetricDisplayName

return annotations
Expand Down
7 changes: 7 additions & 0 deletions pkg/notify/apiserver/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
workloadKindKey = "workloadKind"
namespaceKey = "namespace"
workloadNameKey = "workloadName"
virtualMachineKey = "virtualMachine"
podNameKey = "podName"
nodeNameKey = "nodeName"
nodeRoleKey = "nodeRole"
Expand Down Expand Up @@ -259,6 +260,11 @@ func getVariables(alert Alert) map[string]string {
summary = fmt.Sprintf("%s\n工作负载名称:%s", summary, workloadNameValue)
}

virtualMachineName, ok := labels["name"]
if ok {
summary = fmt.Sprintf("%s\n虚拟机名称:%s", summary, virtualMachineName)
}

namespaceValue, ok := labels["namespace"]
if ok {
summary = fmt.Sprintf("%s\n命名空间:%s", summary, namespaceValue)
Expand Down Expand Up @@ -297,6 +303,7 @@ func getVariables(alert Alert) map[string]string {
variables[evaluateValueKey] = evaluateValue
variables[metricDisplayNameKey] = metricDisplayNameValue
variables[summaryKey] = summary
variables[virtualMachineKey] = virtualMachineName

return variables
}
Expand Down

0 comments on commit cfdac24

Please sign in to comment.