improve Flowdock PR based on julius' comments
This commit is contained in:
parent
ffd54a3ee6
commit
48cdc777ce
|
@ -18,8 +18,9 @@ following aspects:
|
|||
* sending alert notifications via external services (currently email,
|
||||
[PagerDuty](http://www.pagerduty.com/),
|
||||
[HipChat](http://www.hipchat.com/),
|
||||
[Slack](http://www.slack.com/), or
|
||||
[Pushover](https://www.pushover.net/))
|
||||
[Slack](http://www.slack.com/),
|
||||
[Pushover](https://www.pushover.net/)), or
|
||||
[Flowdock](https://www.flowdock.com/))
|
||||
|
||||
See [config/fixtures/sample.conf.input](config/fixtures/sample.conf.input) for
|
||||
an example config. The full configuration schema including a documentation for
|
||||
|
|
|
@ -72,13 +72,13 @@ message SlackConfig {
|
|||
|
||||
message FlowdockConfig {
|
||||
// Flowdock flow API token
|
||||
optional string api_token = 2;
|
||||
optional string api_token = 1;
|
||||
// Flowdock from_address
|
||||
optional string from_address = 3;
|
||||
optional string from_address = 2;
|
||||
// Flowdock flow tags
|
||||
repeated string tags = 4;
|
||||
// Color of message when triggered.
|
||||
optional bool send_resolved = 7 [default = false];
|
||||
repeated string tag = 3;
|
||||
// Notify when resolved.
|
||||
optional bool send_resolved = 4 [default = false];
|
||||
}
|
||||
|
||||
// Notification configuration definition.
|
||||
|
|
|
@ -22,6 +22,7 @@ notification_config {
|
|||
flowdock_config {
|
||||
api_token: "4c7234902348234902384234234cdb59"
|
||||
from_address: "aliaswithgravatar@somehwere.com"
|
||||
tag: "monitoring"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -244,13 +244,13 @@ func (m *SlackConfig) GetSendResolved() bool {
|
|||
|
||||
type FlowdockConfig struct {
|
||||
// Flowdock flow API token
|
||||
ApiToken *string `protobuf:"bytes,2,opt,name=api_token" json:"api_token,omitempty"`
|
||||
ApiToken *string `protobuf:"bytes,1,opt,name=api_token" json:"api_token,omitempty"`
|
||||
// Flowdock from_address
|
||||
FromAddress *string `protobuf:"bytes,3,opt,name=from_address" json:"from_address,omitempty"`
|
||||
FromAddress *string `protobuf:"bytes,2,opt,name=from_address" json:"from_address,omitempty"`
|
||||
// Flowdock flow tags
|
||||
Tags []string `protobuf:"bytes,4,rep,name=tags" json:"tags,omitempty"`
|
||||
// Color of message when triggered.
|
||||
SendResolved *bool `protobuf:"varint,7,opt,name=send_resolved,def=0" json:"send_resolved,omitempty"`
|
||||
Tag []string `protobuf:"bytes,3,rep,name=tag" json:"tag,omitempty"`
|
||||
// Notify when resolved.
|
||||
SendResolved *bool `protobuf:"varint,4,opt,name=send_resolved,def=0" json:"send_resolved,omitempty"`
|
||||
XXX_unrecognized []byte `json:"-"`
|
||||
}
|
||||
|
||||
|
@ -274,9 +274,9 @@ func (m *FlowdockConfig) GetFromAddress() string {
|
|||
return ""
|
||||
}
|
||||
|
||||
func (m *FlowdockConfig) GetTags() []string {
|
||||
func (m *FlowdockConfig) GetTag() []string {
|
||||
if m != nil {
|
||||
return m.Tags
|
||||
return m.Tag
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -330,7 +330,7 @@ func (n *notifier) sendSlackNotification(op notificationOp, config *pb.SlackConf
|
|||
// compulsory fields in Flowdock JSON: source, from_address, subject, content
|
||||
// Content-type: application/json
|
||||
// POST to https://api.flowdock.com/v1/messages/team_inbox/:flow_api_token
|
||||
type FlowdockMessage struct {
|
||||
type flowdockMessage struct {
|
||||
Source string `json:"source"`
|
||||
FromAddress string `json:"from_address"`
|
||||
Subject string `json:"subject"`
|
||||
|
@ -340,14 +340,14 @@ type FlowdockMessage struct {
|
|||
Tags []string `json:"tags,omitempty"`
|
||||
}
|
||||
|
||||
func jsonize(msg interface{}) []byte {
|
||||
func marshallJSON(msg interface{}) []byte {
|
||||
buf, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
glog.Errorln("Error compiling JSON:", err)
|
||||
glog.Errorln("Error marshalling JSON:", err)
|
||||
}
|
||||
return buf
|
||||
}
|
||||
func getFlowdockNotificationMessage(op notificationOp, config *pb.FlowdockConfig, a *Alert) *FlowdockMessage {
|
||||
func newFlowdockNotificationMessage(op notificationOp, config *pb.FlowdockConfig, a *Alert) *flowdockMessage {
|
||||
status := ""
|
||||
switch op {
|
||||
case notificationOpTrigger:
|
||||
|
@ -355,26 +355,27 @@ func getFlowdockNotificationMessage(op notificationOp, config *pb.FlowdockConfig
|
|||
case notificationOpResolve:
|
||||
status = "resolved"
|
||||
}
|
||||
msg := &FlowdockMessage{
|
||||
|
||||
msg := &flowdockMessage{
|
||||
Source: "Prometheus",
|
||||
FromAddress: config.GetFromAddress(),
|
||||
Subject: html.EscapeString(a.Summary),
|
||||
Format: "html",
|
||||
Content: fmt.Sprintf("*%s %s*: %s (<%s|view>)", html.EscapeString(a.Labels["alertname"]), status, html.EscapeString(a.Summary), a.Payload["GeneratorURL"]),
|
||||
Link: a.Payload["GeneratorURL"],
|
||||
Tags: config.GetTags(),
|
||||
Tags: append(config.GetTag(), status),
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
func postJSONtoURL(jsonMessage []byte, url string) *http.Response {
|
||||
func postJSON(jsonMessage []byte, url string) *http.Response {
|
||||
timeout := time.Duration(5 * time.Second)
|
||||
client := http.Client{
|
||||
Timeout: timeout,
|
||||
}
|
||||
response, err := client.Post(url, contentTypeJSON, bytes.NewBuffer(jsonMessage))
|
||||
if err != nil {
|
||||
glog.Errorln("Error while sending Flowdock notification:", err)
|
||||
glog.Errorln("Error while posting JSON:", err)
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
@ -562,9 +563,9 @@ func (n *notifier) handleNotification(a *Alert, op notificationOp, config *pb.No
|
|||
if op == notificationOpResolve && !fdConfig.GetSendResolved() {
|
||||
continue
|
||||
}
|
||||
flowdockMessage := getFlowdockNotificationMessage(op, fdConfig, a)
|
||||
flowdockMessage := newFlowdockNotificationMessage(op, fdConfig, a)
|
||||
url := *flowdockURL + "/" + fdConfig.GetApiToken()
|
||||
httpResponse := postJSONtoURL(jsonize(flowdockMessage), url)
|
||||
httpResponse := postJSON(marshallJSON(flowdockMessage), url)
|
||||
processResponse(httpResponse, "Flowdock", a)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue