-
Notifications
You must be signed in to change notification settings - Fork 15
/
metrics.go
93 lines (77 loc) · 2.35 KB
/
metrics.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
package metric
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/qiniu/x/log"
)
// use WEWORK_WEBHOOK to send alert message to wework group
// refer: https://developer.work.weixin.qq.com/document/path/91770
var WEWORK_WEBHOOK = os.Getenv("WEWORK_WEBHOOK")
var issueCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "reviewbot_issue_found_total",
Help: "issue found by linter",
}, []string{"repo", "linter", "pull_request", "commit"})
func IncIssueCounter(repo, linter, pull_request, commit string, count float64) {
issueCounter.WithLabelValues(repo, linter, pull_request, commit).Add(count)
}
type MessageBody struct {
MsgType string `json:"msgtype"`
Text MsgContent `json:"text,omitempty"`
Markdown MsgContent `json:"markdown,omitempty"`
}
type MsgContent struct {
Content string `json:"content"`
}
// notify sends message to wework group
// refer: https://developer.work.weixin.qq.com/document/path/91770
func notify(message MessageBody) error {
if WEWORK_WEBHOOK == "" || (message.Text.Content == "" && message.Markdown.Content == "") {
return nil
}
body, err := json.Marshal(message)
if err != nil {
return err
}
resp, err := http.DefaultClient.Post(WEWORK_WEBHOOK, "application/json", strings.NewReader(string(body)))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("send message failed: %v", resp)
}
errCode := resp.Header.Get("Error-Code")
if errCode != "0" {
return fmt.Errorf("send message failed, errCode: %v, errMsg: %v", errCode, resp.Header.Get("Error-Msg"))
}
return nil
}
// notifyAsync sends message to wework group asynchronously.
func notifyAsync(message MessageBody) {
go func() {
if err := notify(message); err != nil {
log.Infof("send message failed, err: %v, message: %v\n", err, message)
}
}()
}
// NotifyWebhookByText sends text message to wework group.
func NotifyWebhookByText(content string) {
notifyAsync(MessageBody{
MsgType: "text",
Text: MsgContent{Content: content},
})
}
// NotifyWebhookByMarkdown sends markdown message to wework group.
func NotifyWebhookByMarkdown(content string) {
notifyAsync(MessageBody{
MsgType: "markdown",
Markdown: MsgContent{
Content: content,
},
})
}