Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bd1ad44

Browse filesBrowse files
committed
refactor: Wrapper metrics
1 parent 305292f commit bd1ad44
Copy full SHA for bd1ad44

17 files changed

+147
-41
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+13-1Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,16 @@
88

99
### 已支持的通知方式
1010

11-
- [x] feishu 飞书群组机器人
11+
- [x] feishu 飞书群机器人
12+
- [ ] feishu_app 飞书应用机器人
13+
- [ ] dingtalk 钉钉
14+
- [ ] wechat 企业微信
15+
- [ ] email 邮箱
16+
- [ ] telegram 电报
17+
18+
19+
### 快速启动
20+
21+
```bash
22+
docker run -d -p 8080:8080 lingcoder/prometheus-notifier:latest
23+
```

‎api/v1/base.go

Copy file name to clipboardExpand all lines: api/v1/base.go
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ type BaseReq struct {
99
URL string `json:"url"`
1010
}
1111

12-
// Message defines the JSON object send to webhook endpoints.
13-
type Message struct {
12+
// WebhookBody defines the JSON object send to webhook endpoints.
13+
type WebhookBody struct {
1414
*Data
1515

1616
// The protocol version.

‎api/v1/custom.go

Copy file name to clipboardExpand all lines: api/v1/custom.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package v1
22

33
type CustomReq struct {
44
BaseReq
5-
Body Message
5+
Body WebhookBody
66
}

‎api/v1/feishu.go

Copy file name to clipboardExpand all lines: api/v1/feishu.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package v1
22

33
type FeishuReq struct {
44
BaseReq
5-
Body Message
5+
WebhookBody
66
}

‎config/config.yaml

Copy file name to clipboardExpand all lines: config/config.yaml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
server:
2-
address: ":8082"
2+
address: ":8080"
33
# openapiPath: "/api.json"
44
# swaggerPath: "/swagger"
55

‎docs/feishu.md

Copy file name to clipboard
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
```yml
2+
route:
3+
group_by: [ ... ]
4+
group_wait: 60s
5+
group_interval: 5m
6+
repeat_interval: 1h
7+
receiver: 'webhook'
8+
9+
receivers:
10+
- name: 'webhook'
11+
webhook_configs:
12+
- url: 'http://IP:8080/notifier/feishu?lang=zh-CN&url=https://open.feishu.cn/open-apis/bot/v2/hook/{your-token}'
13+
send_resolved: true
14+
15+
```

‎i18n/en/alert_template.json

Copy file name to clipboardExpand all lines: i18n/en/alert_template.json
-4Lines changed: 0 additions & 4 deletions
This file was deleted.

‎i18n/en/alert_template.toml

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
alert-notification = "Alert Notification"
2+
alert-level-status = "Level Status"
3+
alert-rule-name = "Rule Name"
4+
alert-rule-notes = "Rule Notes"
5+
alert-monitoring-metrics = "Monitoring Metrics"
6+
alert-trigger-time = "Trigger Time"
7+
alert-trigger-value = "Trigger Value"
8+
alert-sending-time = "Sending Time"
9+
alert-recovery-time = "Recovery Time"
10+
alert-status-triggered = "Triggered"
11+
alert-status-recovered = "Recovered"

‎i18n/zh-CN/alert_template.json

Copy file name to clipboardExpand all lines: i18n/zh-CN/alert_template.json
-4Lines changed: 0 additions & 4 deletions
This file was deleted.

‎i18n/zh-CN/alert_template.toml

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
alert-notification = "告警通知"
2+
alert-level-status = "级别状态"
3+
alert-rule-name = "规则名称"
4+
alert-rule-notes = "规则备注"
5+
alert-monitoring-metrics = "监控指标"
6+
alert-trigger-time = "触发时间"
7+
alert-trigger-value = "触发时值"
8+
alert-sending-time = "发送时间"
9+
alert-recovery-time = "恢复时间"
10+
alert-status-triggered = "已触发"
11+
alert-status-recovered = "已恢复"

‎i18n/zh-TW/alert_template.json

Copy file name to clipboardExpand all lines: i18n/zh-TW/alert_template.json
-4Lines changed: 0 additions & 4 deletions
This file was deleted.

‎i18n/zh-TW/alert_template.toml

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
alert-notification = "警報通知"
2+
alert-level-status = "等級狀態"
3+
alert-rule-name = "規則名稱"
4+
alert-rule-notes = "規則備註"
5+
alert-monitoring-metrics = "監控指標"
6+
alert-trigger-time = "觸發時間"
7+
alert-trigger-value = "觸發時值"
8+
alert-sending-time = "發送時間"
9+
alert-recovery-time = "恢復時間"
10+
alert-status-triggered = "已觸發"
11+
alert-status-recovered = "已恢復"

‎internal/consts/alert_template.go

Copy file name to clipboard
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package consts
2+
3+
const DefaultFeishuTemplate = `
4+
**{#alert-level-status}**: {{ .Severity }} {{ if .IsRecovered }}{#alert-status-recovered}{{else }}{#alert-status-triggered}{{ end }}
5+
**{#alert-rule-name}**: {{ .RuleName }}
6+
{{ if .RuleNotes }}**{#alert-rule-notes}**: {{ .RuleNotes }}{{end }}
7+
**{#alert-monitoring-metrics}**: {{.TagsJSON }}
8+
{{ if .IsRecovered }}**{#alert-recovery-time}**: {{ .LastEvalTime }}{{ else }}**{#alert-trigger-time}**: {{ .TriggerTime }}
9+
**{#alert-trigger-value}**: {{ .TriggerValue }}{{ end }}
10+
**{#alert-sending-time}**: {{ .SendingTime }}`

‎internal/logic/feishu/feishu.go

Copy file name to clipboardExpand all lines: internal/logic/feishu/feishu.go
+56-18Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ package feishu
22

33
import (
44
"context"
5+
"fmt"
6+
"github.com/gogf/gf/v2/encoding/gjson"
57
"github.com/gogf/gf/v2/frame/g"
68
"github.com/gogf/gf/v2/util/gconv"
79
larkcard "github.com/larksuite/oapi-sdk-go/v3/card"
810
v1 "github.com/lingcoder/prometheus-notifier/api/v1"
11+
"github.com/lingcoder/prometheus-notifier/internal/consts"
12+
"github.com/lingcoder/prometheus-notifier/internal/model"
913
"github.com/lingcoder/prometheus-notifier/internal/service"
14+
"time"
1015
)
1116

1217
type sFeishu struct {
@@ -26,25 +31,58 @@ func (u *sFeishu) init(ctx context.Context) {
2631

2732
func (u *sFeishu) Send(ctx context.Context, req *v1.FeishuReq) error {
2833

29-
// 根据语言选择合适的模板
30-
template := g.I18n().T(ctx, "feishu-template")
34+
// 重新解析模板
35+
alerts := req.WebhookBody.Alerts
36+
webhookAlert := model.WebhookAlert{
37+
IsRecovered: req.WebhookBody.Status == "resolved",
38+
SendingTime: time.Now().Format(time.RFC3339),
39+
}
3140

32-
// 解析模板
33-
content, err := g.View().ParseContent(ctx, template, gconv.Map(req.Body))
34-
// 创建卡片配置,设置宽度和固定显示
35-
36-
card := larkcard.NewMessageCard().
37-
Config(larkcard.NewMessageCardConfig().
38-
WideScreenMode(true).EnableForward(true)).
39-
Header(larkcard.NewMessageCardHeader().
40-
Title(larkcard.NewMessageCardPlainText().Content(g.I18n().T(ctx, "feishu-title")))).
41-
Elements([]larkcard.MessageCardElement{
42-
larkcard.NewMessageCardDiv().Text(larkcard.NewMessageCardPlainText().Content(content)),
41+
encodeString := gjson.MustEncodeString(req)
42+
fmt.Println(encodeString)
43+
44+
for i := 0; i < len(alerts); i++ {
45+
alert := alerts[i]
46+
47+
webhookAlert.Severity = alert.Labels["severity"]
48+
webhookAlert.RuleName = alert.Labels["alertname"]
49+
webhookAlert.RuleNotes = alert.Annotations["summary"]
50+
webhookAlert.TriggerTime = alert.StartsAt.Format(time.RFC3339)
51+
webhookAlert.LastEvalTime = alert.EndsAt.Format(time.RFC3339)
52+
webhookAlert.TriggerValue = alert.Annotations["value"]
53+
webhookAlert.TagsJSON = alert.Labels["tags"]
54+
55+
content, err := g.View().ParseContent(ctx, consts.DefaultFeishuTemplate, gconv.Map(webhookAlert))
56+
57+
if err != nil {
58+
59+
}
60+
// 创建卡片配置,设置宽度和固定显示
61+
62+
card := larkcard.NewMessageCard().
63+
Config(larkcard.NewMessageCardConfig().
64+
WideScreenMode(true).EnableForward(true)).
65+
Header(larkcard.NewMessageCardHeader().
66+
Template(func() string {
67+
if webhookAlert.IsRecovered {
68+
return larkcard.TemplateGreen
69+
} else {
70+
return larkcard.TemplateRed
71+
}
72+
}()).
73+
Title(larkcard.NewMessageCardPlainText().Content(g.I18n().T(ctx, "alert-notification")))).
74+
Elements([]larkcard.MessageCardElement{
75+
larkcard.NewMessageCardDiv().Text(larkcard.NewMessageCardLarkMd().Content(content)),
76+
})
77+
78+
_, _ = g.Client().Post(ctx, req.URL, g.Map{
79+
"msg_type": "interactive",
80+
"card": card,
4381
})
4482

45-
_, err = g.Client().Post(ctx, req.URL, g.Map{
46-
"msg_type": "interactive",
47-
"card": card,
48-
})
49-
return err
83+
}
84+
85+
// 解析模板
86+
87+
return nil
5088
}

‎internal/model/.gitkeep

Copy file name to clipboardExpand all lines: internal/model/.gitkeep
Whitespace-only changes.

‎internal/model/template.go

Copy file name to clipboard
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package model
2+
3+
type WebhookAlert struct {
4+
Severity string
5+
IsRecovered bool
6+
RuleName string
7+
RuleNotes string
8+
TagsJSON string
9+
LastEvalTime string
10+
TriggerTime string
11+
TriggerValue string
12+
SendingTime string
13+
}

‎main.go

Copy file name to clipboardExpand all lines: main.go
+2-5Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package main
22

33
import (
4-
_ "github.com/lingcoder/prometheus-notifier/internal/packed"
5-
6-
_ "github.com/lingcoder/prometheus-notifier/internal/logic"
7-
84
"github.com/gogf/gf/v2/os/gctx"
9-
105
"github.com/lingcoder/prometheus-notifier/internal/cmd"
6+
_ "github.com/lingcoder/prometheus-notifier/internal/logic"
7+
_ "github.com/lingcoder/prometheus-notifier/internal/packed"
118
)
129

1310
func main() {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.