forked from kuzaxak/promalert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_handlers.go
134 lines (121 loc) · 3.12 KB
/
http_handlers.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"net/http/httputil"
"net/url"
"github.com/bugsnag/bugsnag-go/v2"
"github.com/bugsnag/microkit/clog"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
func healthz(c *gin.Context) {
c.JSON(200, gin.H{
"message": "ok",
})
}
func webhook(c *gin.Context) {
ctx := c.Request.Context()
if viper.GetBool("debug") {
// Save a copy of this request for debugging.
requestDump, err := httputil.DumpRequest(c.Request, true)
if err != nil {
err = errors.Wrap(err, "Error dumping request")
_ = bugsnag.Notify(err, ctx)
clog.Error(err.Error())
}
clog.Infof("New request: %s", string(requestDump))
}
var m HookMessage
if c.ShouldBindJSON(&m) == nil {
clog.Infof("Alerts: GroupLabels=%v, CommonLabels=%v", m.GroupLabels, m.CommonLabels)
for _, alert := range m.Alerts {
alertName := alert.Labels["alertname"]
// shorten all alert annotation URLs
cli := NewLinksClient()
for k, txt := range alert.Annotations {
err, n := cli.ReplaceLinks(ctx, txt)
if err != nil {
err = errors.Wrap(err, "Error whilst shortening links")
_ = bugsnag.Notify(err, ctx,
bugsnag.MetaData{
"Alert": {
"Name": alertName,
},
"Kutt": {
"URL": n,
"URLLength": len(n),
},
})
clog.Error(err.Error())
}
alert.Annotations[k] = n
}
// get chart url
generatorUrl, err := url.Parse(alert.GeneratorURL)
if err != nil {
err = errors.Wrap(err, "Could not parse generator url")
_ = bugsnag.Notify(err, ctx,
bugsnag.MetaData{
"Alert": {
"Name": alertName,
"GeneratorURL": alert.GeneratorURL,
},
})
clog.Error(err.Error())
}
// from the chart url get the expressions to build the charts
generatorQuery, err := url.ParseQuery(generatorUrl.RawQuery)
if err != nil {
err = errors.Wrap(err, "Could not parse query from generator url")
_ = bugsnag.Notify(err, ctx,
bugsnag.MetaData{
"Alert": {
"Name": alertName,
"RawQuery": generatorUrl.RawQuery,
},
})
clog.Error(err.Error())
}
// shorten generator URL
err, n := cli.ReplaceLinks(ctx, alert.GeneratorURL)
if err != nil {
err = errors.Wrap(err, "Error shortening generator URL")
_ = bugsnag.Notify(err, ctx,
bugsnag.MetaData{
"Alert": {
"Name": alertName,
},
"Kutt": {
"URL": n,
},
})
clog.Error(err.Error())
}
alert.GeneratorURL = n
// override channel if specified in rule
if m.CommonLabels["channel"] != "" {
alert.Channel = m.CommonLabels["channel"]
}
// post new message
err = alert.PostMessage(generatorQuery)
if err != nil {
c.String(500, "%v", err)
err = errors.Wrap(err, "Error posting Slack message")
_ = bugsnag.Notify(err, ctx,
bugsnag.MetaData{
"Alert": {
"Name": alertName,
},
"Slack": {
"Query": generatorQuery,
},
})
clog.Error(err.Error())
return
}
}
c.JSON(200, map[string]string{"success": "true"})
return
}
c.JSON(400, map[string]string{"status": "Invalid body of request"})
}