support graph url (#1088)

Co-authored-by: ziv <xiaozheng@tuya.com>
This commit is contained in:
xiaoziv 2022-08-06 18:12:33 +08:00 committed by GitHub
parent 04a9161f75
commit 58e777eb00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 2 deletions

View File

@ -45,10 +45,11 @@ def convert_alert(rule, interval):
continue continue
if v == 'critical': if v == 'critical':
severity = 1 severity = 1
if v == 'warning':
severity = 2
elif v == 'info': elif v == 'info':
severity = 3 severity = 3
# elif v == 'warning':
# severity = 2
n9e_alert_rule = { n9e_alert_rule = {
"name": name, "name": name,
@ -102,6 +103,20 @@ def convert_record(rule, interval):
return n9e_record_rule return n9e_record_rule
'''
example of rule group file
---
groups:
- name: example
rules:
- alert: HighRequestLatency
expr: job:request_latency_seconds:mean5m{job="myjob"} > 0.5
for: 10m
labels:
severity: page
annotations:
summary: High request latency
'''
def deal_group(group): def deal_group(group):
""" """
parse single prometheus/vmalert rule group parse single prometheus/vmalert rule group
@ -123,6 +138,27 @@ def deal_group(group):
return alert_rules, record_rules return alert_rules, record_rules
'''
example of k8s rule configmap
---
apiVersion: v1
kind: ConfigMap
metadata:
name: rulefiles-0
data:
etcdrules.yaml: |
groups:
- name: etcd
rules:
- alert: etcdInsufficientMembers
annotations:
message: 'etcd cluster "{{ $labels.job }}": insufficient members ({{ $value}}).'
expr: sum(up{job=~".*etcd.*"} == bool 1) by (job) < ((count(up{job=~".*etcd.*"})
by (job) + 1) / 2)
for: 3m
labels:
severity: critical
'''
def deal_configmap(rule_configmap): def deal_configmap(rule_configmap):
""" """
parse rule configmap from k8s parse rule configmap from k8s

26
etc/template/README.md Normal file
View File

@ -0,0 +1,26 @@
# 告警消息模版文件
模版中可以使用的变量参考`AlertCurEvent`对象
模版语法如何使用可以参考[html/template](https://pkg.go.dev/html/template)
## 如何在告警模版中添加监控详情url
假设web的地址是http://127.0.0.1:18000/, 实际使用时用web地址替换该地址
在监控模版中添加以下行:
* dingtalk / wecom / feishu
```markdown
[监控详情](http://127.0.0.1:18000/metric/explorer?promql={{ .PromQl | escape }})
```
* mailbody
```html
<tr>
<th>监控详情:</th>
<td>
<a href="http://127.0.0.1:18000/metric/explorer?promql={{ .PromQl | escape }}" target="_blank">点击查看</a>
</td>
</tr>
```

View File

@ -2,11 +2,13 @@ package tplx
import ( import (
"html/template" "html/template"
"net/url"
"regexp" "regexp"
"strings" "strings"
) )
var TemplateFuncMap = template.FuncMap{ var TemplateFuncMap = template.FuncMap{
"escape": url.PathEscape,
"unescaped": Unescaped, "unescaped": Unescaped,
"urlconvert": Urlconvert, "urlconvert": Urlconvert,
"timeformat": Timeformat, "timeformat": Timeformat,