rewrote to better fit in exisitng code

This commit is contained in:
Tomas Karasek 2015-05-19 14:31:06 +03:00
parent 9632bf24f8
commit 5b1c59f0cc
3 changed files with 35 additions and 34 deletions

View File

@ -71,11 +71,11 @@ message SlackConfig {
}
message FlowdockConfig {
// Flowdock flow API token
// Flowdock flow API token.
optional string api_token = 1;
// Flowdock from_address
// Flowdock from_address.
optional string from_address = 2;
// Flowdock flow tags
// Flowdock flow tags.
repeated string tag = 3;
// Notify when resolved.
optional bool send_resolved = 4 [default = false];

View File

@ -243,11 +243,11 @@ func (m *SlackConfig) GetSendResolved() bool {
}
type FlowdockConfig struct {
// Flowdock flow API token
// Flowdock flow API token.
ApiToken *string `protobuf:"bytes,1,opt,name=api_token" json:"api_token,omitempty"`
// Flowdock from_address
// Flowdock from_address.
FromAddress *string `protobuf:"bytes,2,opt,name=from_address" json:"from_address,omitempty"`
// Flowdock flow tags
// Flowdock flow tags.
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"`

View File

@ -326,10 +326,6 @@ func (n *notifier) sendSlackNotification(op notificationOp, config *pb.SlackConf
return nil
}
// https://www.flowdock.com/api/team-inbox
// 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 {
Source string `json:"source"`
FromAddress string `json:"from_address"`
@ -340,14 +336,24 @@ type flowdockMessage struct {
Tags []string `json:"tags,omitempty"`
}
func marshallJSON(msg interface{}) []byte {
buf, err := json.Marshal(msg)
func (n *notifier) sendFlowdockNotification(op notificationOp, config *pb.FlowdockConfig, a *Alert) error {
flowdockMessage := newFlowdockMessage(op, config, a)
url := strings.TrimRight(*flowdockURL, "/") + "/" + config.GetApiToken()
jsonMessage, err := json.Marshal(flowdockMessage)
if err != nil {
glog.Errorln("Error marshalling JSON:", err)
return err
}
return buf
httpResponse, err := postJSON(jsonMessage, url)
if err != nil {
return err
}
if err := processResponse(httpResponse, "Flowdock", a); err != nil {
return err
}
return nil
}
func newFlowdockNotificationMessage(op notificationOp, config *pb.FlowdockConfig, a *Alert) *flowdockMessage {
func newFlowdockMessage(op notificationOp, config *pb.FlowdockConfig, a *Alert) *flowdockMessage {
status := ""
switch op {
case notificationOpTrigger:
@ -368,16 +374,12 @@ func newFlowdockNotificationMessage(op notificationOp, config *pb.FlowdockConfig
return msg
}
func postJSON(jsonMessage []byte, url string) *http.Response {
func postJSON(jsonMessage []byte, url string) (*http.Response, error) {
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 posting JSON:", err)
}
return response
return client.Post(url, contentTypeJSON, bytes.NewBuffer(jsonMessage))
}
func writeEmailBody(w io.Writer, from, to, status string, a *Alert) error {
@ -507,18 +509,18 @@ func (n *notifier) sendPushoverNotification(token string, op notificationOp, use
return err
}
func processResponse(r *http.Response, targetName string, a *Alert) {
func processResponse(r *http.Response, targetName string, a *Alert) error {
spec := fmt.Sprintf("%s notification for alert %v", targetName, a.Fingerprint())
if r == nil {
glog.Errorln("No HTTP response for", spec)
} else {
defer r.Body.Close()
respBuf, err := ioutil.ReadAll(r.Body)
if err != nil {
glog.Errorln("Error reading HTTP response for", spec, err)
}
glog.Infof("Sent %s. Response: HTTP %d: %s", spec, r.StatusCode, respBuf)
return fmt.Errorf("No HTTP response for %s", spec)
}
defer r.Body.Close()
respBuf, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
glog.Infof("Sent %s. Response: HTTP %d: %s", spec, r.StatusCode, respBuf)
return nil
}
func (n *notifier) handleNotification(a *Alert, op notificationOp, config *pb.NotificationConfig) {
@ -567,10 +569,9 @@ func (n *notifier) handleNotification(a *Alert, op notificationOp, config *pb.No
if op == notificationOpResolve && !fdConfig.GetSendResolved() {
continue
}
flowdockMessage := newFlowdockNotificationMessage(op, fdConfig, a)
url := *flowdockURL + "/" + fdConfig.GetApiToken()
httpResponse := postJSON(marshallJSON(flowdockMessage), url)
processResponse(httpResponse, "Flowdock", a)
if err := n.sendFlowdockNotification(op, fdConfig, a); err != nil {
glog.Errorln("Error sending Flowdock notification:", err)
}
}
}