-
Notifications
You must be signed in to change notification settings - Fork 38
/
rule.go
151 lines (138 loc) · 4.9 KB
/
rule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Copyright (c) 2023 OceanBase
ob-operator is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
package rule
import (
"time"
alarmconstant "github.com/oceanbase/ob-operator/internal/dashboard/business/alarm/constant"
bizcommon "github.com/oceanbase/ob-operator/internal/dashboard/business/common"
"github.com/oceanbase/ob-operator/internal/dashboard/model/alarm"
"github.com/oceanbase/ob-operator/internal/dashboard/model/common"
"github.com/oceanbase/ob-operator/internal/dashboard/model/oceanbase"
prommodel "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/rulefmt"
promv1 "github.com/prometheus/prometheus/web/api/v1"
)
type PromRuleResponse struct {
Status string `json:"status" binding:"required"`
Data *promv1.RuleDiscovery `json:"data" binding:"required"`
}
type Rule struct {
Name string `json:"name" binding:"required"`
InstanceType oceanbase.OBInstanceType `json:"instanceType" binding:"required"`
Type RuleType `json:"type" default:"customized"`
Query string `json:"query" binding:"required"`
Duration int `json:"duration" binding:"required"`
Labels []common.KVPair `json:"labels" binding:"required"`
Severity alarm.Severity `json:"severity" binding:"required"`
Summary string `json:"summary" binding:"required"`
Description string `json:"description" binding:"required"`
}
type RuleResponse struct {
State RuleState `json:"state" binding:"required"`
KeepFiringFor int `json:"keepFiringFor" binding:"required"`
Health RuleHealth `json:"health" binding:"required"`
LastEvaluation int64 `json:"lastEvaluation" binding:"required"`
EvaluationTime float64 `json:"evaluationTime" binding:"required"`
LastError string `json:"lastError,omitempty"`
Rule
}
type RuleIdentity struct {
Name string `json:"name" binding:"required"`
}
type ConfigRuleGroups struct {
Groups []ConfigRuleGroup `json:"groups"`
}
type ConfigRuleGroup struct {
Name string `json:"name"`
Rules []rulefmt.Rule `json:"rules"`
}
func (r *Rule) ToPromRule() *rulefmt.Rule {
annotations := make(map[string]string)
annotations[alarmconstant.AnnoSummary] = r.Summary
annotations[alarmconstant.AnnoDescription] = r.Description
labels := r.Labels
labels = append(labels, common.KVPair{
Key: alarmconstant.LabelRuleType,
Value: string(r.Type),
})
labels = append(labels, common.KVPair{
Key: alarmconstant.LabelSeverity,
Value: string(r.Severity),
})
labels = append(labels, common.KVPair{
Key: alarmconstant.LabelRuleName,
Value: r.Name,
})
labels = append(labels, common.KVPair{
Key: alarmconstant.LabelInstanceType,
Value: string(r.InstanceType),
})
promRule := &rulefmt.Rule{
Alert: r.Name,
Expr: r.Query,
For: prommodel.Duration(r.Duration * int(time.Second)),
Labels: bizcommon.KVsToMap(labels),
Annotations: annotations,
}
return promRule
}
func NewRuleResponse(promRule *promv1.AlertingRule) *RuleResponse {
var instanceType oceanbase.OBInstanceType
var ruleType RuleType
severity := alarm.SeverityInfo
summary := ""
description := ""
labels := make([]common.KVPair, 0, len(promRule.Labels))
for _, label := range promRule.Labels {
labels = append(labels, common.KVPair{
Key: label.Name,
Value: label.Value,
})
if label.Name == alarmconstant.LabelSeverity {
severity = alarm.Severity(label.Value)
}
if label.Name == alarmconstant.LabelInstanceType {
instanceType = oceanbase.OBInstanceType(label.Value)
}
if label.Name == alarmconstant.LabelRuleType {
ruleType = RuleType(label.Value)
}
}
for _, annotation := range promRule.Annotations {
if annotation.Name == alarmconstant.AnnoSummary {
summary = annotation.Value
}
if annotation.Name == alarmconstant.AnnoDescription {
description = annotation.Value
}
}
rule := &Rule{
Name: promRule.Name,
InstanceType: instanceType,
Type: ruleType,
Query: promRule.Query,
Duration: int(promRule.Duration),
Labels: labels,
Severity: severity,
Summary: summary,
Description: description,
}
return &RuleResponse{
State: RuleState(promRule.State),
KeepFiringFor: int(promRule.KeepFiringFor),
Health: RuleHealth(promRule.Health),
LastEvaluation: promRule.LastEvaluation.Unix(),
EvaluationTime: promRule.EvaluationTime,
LastError: promRule.LastError,
Rule: *rule,
}
}