Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
so-operation-api
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
smart-operation
so-operation-api
Commits
26fa830f
Commit
26fa830f
authored
Jul 20, 2023
by
李科
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: 完善预警规则设置风险等级阈值上下限逻辑
parent
fecb1d1c
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
54 additions
and
21 deletions
+54
-21
src/bean/entity/alert_rules.go
src/bean/entity/alert_rules.go
+3
-3
src/controller/alert_rules.go
src/controller/alert_rules.go
+1
-17
src/service/alert_rules.go
src/service/alert_rules.go
+6
-0
src/service/prometheusrule.go
src/service/prometheusrule.go
+44
-1
No files found.
src/bean/entity/alert_rules.go
View file @
26fa830f
...
...
@@ -36,8 +36,8 @@ type RulesAlertRange struct {
}
type
AlertCondition
struct
{
ThresholdsMin
int
`json:"thresholds_min" form:"thresholds_min" binding:"required,ltfield=ThresholdsMax
"`
ThresholdsMax
int
`json:"thresholds_max" form:"thresholds_max" binding:"required
"`
ThresholdsMin
*
float64
`json:"thresholds_min" form:"thresholds_min" binding:"omitempty,required_without=Id
"`
ThresholdsMax
*
float64
`json:"thresholds_max" form:"thresholds_max" binding:"omitempty
"`
RiskLevel
int
`json:"risk_level" form:"risk_level" binding:"required,oneof=1 2 3 4"`
}
...
...
src/controller/alert_rules.go
View file @
26fa830f
...
...
@@ -16,24 +16,8 @@ func AddAlertRules(c *gin.Context) {
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
TranslateError
(
err
),
nil
)
return
}
/*
[
{
"thresholds_min": 12,
"thresholds_max": 66,
"risk_level": 1
},
{
"thresholds_min": 66,
"thresholds_max": 80,
"risk_level": 2
}
]
*/
// TODO 阈值范围判断,阈值上下限是否存在交叉
sort
.
SliceStable
(
req
.
AlertCondition
,
func
(
i
,
j
int
)
bool
{
return
req
.
AlertCondition
[
i
]
.
ThresholdsMin
<
req
.
AlertCondition
[
j
]
.
ThresholdsMin
return
*
req
.
AlertCondition
[
i
]
.
ThresholdsMin
<
*
req
.
AlertCondition
[
j
]
.
ThresholdsMin
})
svc
:=
service
.
AlertRulesSvc
{
User
:
header
.
GetUser
(
c
)}
...
...
src/service/alert_rules.go
View file @
26fa830f
...
...
@@ -200,9 +200,14 @@ func (a *AlertRulesSvc) UpdateIsEnabled(req request.UpdateIsEnabledAlertRules) e
UpdatedBy
:
a
.
User
.
SystemAccount
,
UpdatedAt
:
now
,
}
if
req
.
IsEnabled
==
2
{
// TODO 关闭状态需要删除prometheus规则
}
session
:=
db
.
NewSession
()
defer
session
.
Close
()
_
,
err
=
session
.
Table
(
data
.
TableName
())
.
Cols
(
"is_enabled,updated_by,updated_at"
)
.
Where
(
"id = ?"
,
req
.
Id
)
.
Update
(
&
data
)
return
err
}
...
...
@@ -274,6 +279,7 @@ func (a *AlertRulesSvc) Delete(ids []string) (err error) {
}
if
!
exist
{
_
,
err
=
db
.
NewSession
()
.
Where
(
"id = ?"
,
id
)
.
Delete
(
new
(
entity
.
AlertRules
))
// TODO 删除普罗米修斯规则
}
else
{
return
errors
.
New
(
"alert_rules_id already exists in opensearch"
)
}
...
...
src/service/prometheusrule.go
View file @
26fa830f
...
...
@@ -52,7 +52,30 @@ func (p *PrometheusRuleSvc) Create(data response.AlertRulesItem) (err error) {
"description"
:
""
,
},
}
expr
:=
fmt
.
Sprintf
(
"%d <= %s <=%d"
,
v
.
ThresholdsMin
,
data
.
Expr
,
v
.
ThresholdsMax
)
var
(
condition
int
expr
string
)
if
v
.
ThresholdsMin
!=
nil
{
condition
+=
1
}
if
v
.
ThresholdsMax
!=
nil
{
condition
+=
2
}
switch
condition
{
default
:
expr
=
data
.
Expr
case
1
:
expr
=
fmt
.
Sprintf
(
"%s <= %s"
,
cast
.
ToString
(
v
.
ThresholdsMin
),
data
.
Expr
)
case
2
:
expr
=
fmt
.
Sprintf
(
"%s <=%s"
,
data
.
Expr
,
cast
.
ToString
(
v
.
ThresholdsMax
))
case
3
:
expr
=
fmt
.
Sprintf
(
"%s <= %s <=%s"
,
cast
.
ToString
(
v
.
ThresholdsMin
),
data
.
Expr
,
cast
.
ToString
(
v
.
ThresholdsMax
))
}
rule
.
Expr
=
intstr
.
FromString
(
expr
)
group
.
Rules
=
append
(
group
.
Rules
,
rule
)
}
...
...
@@ -69,3 +92,23 @@ func (p *PrometheusRuleSvc) Create(data response.AlertRulesItem) (err error) {
}
return
}
func
(
p
*
PrometheusRuleSvc
)
Delete
(
data
response
.
AlertRulesItem
)
(
err
error
)
{
pr
:=
monitoringv1
.
PrometheusRule
{}
prometheusRuleName
:=
k8s
.
GetPrometheusRuleName
(
data
.
Id
)
pr
.
Name
=
prometheusRuleName
pr
.
Namespace
=
k8s
.
Namespace
pr
.
Labels
=
k8s
.
AlertDefLabels
header
:=
make
(
map
[
string
]
string
)
header
[
"Authorization"
]
=
"Bearer "
+
conf
.
Options
.
KubernetesToken
prSvc
:=
k8s
.
PrometheusRule
{
Header
:
header
}
conf
.
Logger
.
Info
(
"创建规则"
,
zap
.
Any
(
"pr"
,
pr
))
err
=
prSvc
.
Delete
(
pr
.
Namespace
,
pr
.
Name
)
if
err
!=
nil
{
fmt
.
Println
(
"删除失败"
+
err
.
Error
())
}
else
{
fmt
.
Println
(
"删除成功"
)
}
return
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment