Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
jobily
Nightingale
提交
6440645c
N
Nightingale
项目概览
jobily
/
Nightingale
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
Nightingale
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
6440645c
编写于
7月 06, 2021
作者:
Q
qinyening
提交者:
GitHub
7月 06, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: alert rule api fill users and groups (#723)
上级
45855199
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
172 addition
and
27 deletion
+172
-27
cache/user_group.go
cache/user_group.go
+41
-0
http/router_alert_rule.go
http/router_alert_rule.go
+25
-1
http/router_alert_rule_group.go
http/router_alert_rule_group.go
+4
-0
main.go
main.go
+1
-0
models/alert_rule.go
models/alert_rule.go
+28
-26
models/user_group.go
models/user_group.go
+16
-0
timer/user_group.go
timer/user_group.go
+57
-0
未找到文件。
cache/user_group.go
0 → 100644
浏览文件 @
6440645c
package
cache
import
(
"sync"
"github.com/didi/nightingale/v5/models"
)
type
UserGroupMap
struct
{
sync
.
RWMutex
Data
map
[
int64
]
*
models
.
UserGroup
}
var
UserGroupCache
=
&
UserGroupMap
{
Data
:
make
(
map
[
int64
]
*
models
.
UserGroup
)}
func
(
s
*
UserGroupMap
)
GetBy
(
id
int64
)
*
models
.
UserGroup
{
s
.
RLock
()
defer
s
.
RUnlock
()
return
s
.
Data
[
id
]
}
func
(
s
*
UserGroupMap
)
GetByIds
(
ids
[]
int64
)
[]
*
models
.
UserGroup
{
s
.
RLock
()
defer
s
.
RUnlock
()
var
userGroups
[]
*
models
.
UserGroup
for
_
,
id
:=
range
ids
{
if
s
.
Data
[
id
]
==
nil
{
continue
}
userGroups
=
append
(
userGroups
,
s
.
Data
[
id
])
}
return
userGroups
}
func
(
s
*
UserGroupMap
)
SetAll
(
userGroups
map
[
int64
]
*
models
.
UserGroup
)
{
s
.
Lock
()
defer
s
.
Unlock
()
s
.
Data
=
userGroups
}
http/router_alert_rule.go
浏览文件 @
6440645c
...
...
@@ -3,16 +3,21 @@ package http
import
(
"encoding/json"
"net/http"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/didi/nightingale/v5/cache"
"github.com/didi/nightingale/v5/config"
"github.com/didi/nightingale/v5/models"
)
func
alertRuleGet
(
c
*
gin
.
Context
)
{
renderData
(
c
,
AlertRule
(
urlParamInt64
(
c
,
"id"
)),
nil
)
alertRule
:=
AlertRule
(
urlParamInt64
(
c
,
"id"
))
alertRuleFillUserAndGroups
(
alertRule
)
renderData
(
c
,
alertRule
,
nil
)
}
type
alertRuleForm
struct
{
...
...
@@ -163,3 +168,22 @@ func alertRuleDel(c *gin.Context) {
func
notifyChannelsGet
(
c
*
gin
.
Context
)
{
renderData
(
c
,
config
.
Config
.
NotifyChannels
,
nil
)
}
func
alertRuleFillUserAndGroups
(
alertRule
*
models
.
AlertRule
)
{
uidStrs
:=
strings
.
Fields
(
alertRule
.
NotifyUsers
)
var
uids
[]
int64
for
_
,
uidStr
:=
range
uidStrs
{
uid
,
_
:=
strconv
.
ParseInt
(
uidStr
,
10
,
64
)
uids
=
append
(
uids
,
uid
)
}
alertRule
.
NotifyUsersDetail
=
cache
.
UserCache
.
GetByIds
(
uids
)
gidStrs
:=
strings
.
Fields
(
alertRule
.
NotifyGroups
)
var
gids
[]
int64
for
_
,
gidStr
:=
range
gidStrs
{
gid
,
_
:=
strconv
.
ParseInt
(
gidStr
,
10
,
64
)
gids
=
append
(
gids
,
gid
)
}
alertRule
.
NotifyGroupsDetail
=
cache
.
UserGroupCache
.
GetByIds
(
gids
)
}
http/router_alert_rule_group.go
浏览文件 @
6440645c
...
...
@@ -68,6 +68,10 @@ func alertRuleGroupGet(c *gin.Context) {
func
alertRuleOfGroupGet
(
c
*
gin
.
Context
)
{
ars
,
err
:=
models
.
AlertRulesOfGroup
(
urlParamInt64
(
c
,
"id"
))
for
i
:=
range
ars
{
alertRuleFillUserAndGroups
(
&
ars
[
i
])
}
renderData
(
c
,
ars
,
err
)
}
...
...
main.go
浏览文件 @
6440645c
...
...
@@ -71,6 +71,7 @@ func main() {
timer
.
SyncResourceTags
()
timer
.
SyncUsers
()
timer
.
SyncUserGroups
()
timer
.
SyncUserGroupMember
()
timer
.
SyncClasspathReses
()
timer
.
SyncCollectRules
()
...
...
models/alert_rule.go
浏览文件 @
6440645c
...
...
@@ -19,32 +19,34 @@ const ALERT_RULE_ACTIVE = 0
const
ALERT_RULE_DISABLED
=
1
type
AlertRule
struct
{
Id
int64
`json:"id"`
GroupId
int64
`json:"group_id"`
Name
string
`json:"name"`
Type
int
`json:"type"`
// 0: nightingale, 1: prometheus
Expression
json
.
RawMessage
`json:"expression"`
Status
int
`json:"status"`
// 0: active, 1: disabled
AppendTags
string
`json:"append_tags"`
EnableStime
string
`json:"enable_stime"`
EnableEtime
string
`json:"enable_etime"`
EnableDaysOfWeek
string
`json:"enable_days_of_week"`
RecoveryNotify
int
`json:"recovery_notify"`
Priority
int
`json:"priority"`
NotifyChannels
string
`json:"notify_channels"`
NotifyGroups
string
`json:"notify_groups"`
NotifyUsers
string
`json:"notify_users"`
Callbacks
string
`json:"callbacks"`
RunbookUrl
string
`json:"runbook_url"`
Note
string
`json:"note"`
CreateAt
int64
`json:"create_at"`
CreateBy
string
`json:"create_by"`
UpdateAt
int64
`json:"update_at"`
UpdateBy
string
`json:"update_by"`
AlertDuration
int
`json:"alert_duration"`
// 告警统计周期,PULL模型会当做P8S的for时间
PushExpr
PushExpression
`xorm:"-" json:"-"`
PullExpr
PullExpression
`xorm:"-" json:"-"`
FirstMetric
string
`xorm:"-" json:"-"`
// Exps里可能有多个metric,只取第一个,给后续制作map使用
Id
int64
`json:"id"`
GroupId
int64
`json:"group_id"`
Name
string
`json:"name"`
Type
int
`json:"type"`
// 0: nightingale, 1: prometheus
Expression
json
.
RawMessage
`json:"expression"`
Status
int
`json:"status"`
// 0: active, 1: disabled
AppendTags
string
`json:"append_tags"`
EnableStime
string
`json:"enable_stime"`
EnableEtime
string
`json:"enable_etime"`
EnableDaysOfWeek
string
`json:"enable_days_of_week"`
RecoveryNotify
int
`json:"recovery_notify"`
Priority
int
`json:"priority"`
NotifyChannels
string
`json:"notify_channels"`
NotifyGroups
string
`json:"notify_groups"`
NotifyUsers
string
`json:"notify_users"`
Callbacks
string
`json:"callbacks"`
RunbookUrl
string
`json:"runbook_url"`
Note
string
`json:"note"`
CreateAt
int64
`json:"create_at"`
CreateBy
string
`json:"create_by"`
UpdateAt
int64
`json:"update_at"`
UpdateBy
string
`json:"update_by"`
AlertDuration
int
`json:"alert_duration"`
// 告警统计周期,PULL模型会当做P8S的for时间
PushExpr
PushExpression
`xorm:"-" json:"-"`
PullExpr
PullExpression
`xorm:"-" json:"-"`
FirstMetric
string
`xorm:"-" json:"-"`
// Exps里可能有多个metric,只取第一个,给后续制作map使用
NotifyUsersDetail
[]
*
User
`xorm:"-" json:"notify_users_detail"`
NotifyGroupsDetail
[]
*
UserGroup
`xorm:"-" json:"notify_groups_detail"`
}
type
PushExpression
struct
{
...
...
models/user_group.go
浏览文件 @
6440645c
...
...
@@ -204,3 +204,19 @@ func (ug *UserGroup) Del() error {
return
session
.
Commit
()
}
func
UserGroupGetAll
()
([]
UserGroup
,
error
)
{
var
userGroups
[]
UserGroup
err
:=
DB
.
Find
(
&
userGroups
)
if
err
!=
nil
{
logger
.
Errorf
(
"mysql.error: select user_group fail: %v"
,
err
)
return
userGroups
,
internalServerError
}
if
len
(
userGroups
)
==
0
{
return
[]
UserGroup
{},
nil
}
return
userGroups
,
nil
}
timer/user_group.go
0 → 100644
浏览文件 @
6440645c
package
timer
import
(
"fmt"
"math/rand"
"time"
"github.com/didi/nightingale/v5/cache"
"github.com/didi/nightingale/v5/models"
"github.com/toolkits/pkg/logger"
)
// user_group_id->user_group 将数据库中的用户信息缓存在内存里,
// 在生成告警事件的时候,根据用户ID快速找到用户的详情
func
SyncUserGroups
()
{
err
:=
syncUserGroups
()
if
err
!=
nil
{
fmt
.
Println
(
"timer: sync users fail:"
,
err
)
exit
(
1
)
}
go
loopSyncUserGroups
()
}
func
loopSyncUserGroups
()
{
randtime
:=
rand
.
Intn
(
9000
)
fmt
.
Printf
(
"timer: sync users: random sleep %dms
\n
"
,
randtime
)
time
.
Sleep
(
time
.
Duration
(
randtime
)
*
time
.
Millisecond
)
for
{
time
.
Sleep
(
time
.
Second
*
time
.
Duration
(
9
))
err
:=
syncUserGroups
()
if
err
!=
nil
{
logger
.
Warning
(
"timer: sync users fail:"
,
err
)
}
}
}
func
syncUserGroups
()
error
{
start
:=
time
.
Now
()
userGroups
,
err
:=
models
.
UserGroupGetAll
()
if
err
!=
nil
{
return
err
}
userGroupsMap
:=
make
(
map
[
int64
]
*
models
.
UserGroup
)
for
i
:=
range
userGroups
{
userGroupsMap
[
userGroups
[
i
]
.
Id
]
=
&
userGroups
[
i
]
}
cache
.
UserGroupCache
.
SetAll
(
userGroupsMap
)
logger
.
Debugf
(
"timer: sync userGroups done, cost: %dms"
,
time
.
Since
(
start
)
.
Milliseconds
())
return
nil
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录