From c6f7d34c5528d1638ba5628b7c94373b4386c283 Mon Sep 17 00:00:00 2001 From: Joseph Weigl Date: Thu, 24 Aug 2017 13:40:19 +0200 Subject: [PATCH 1/5] Reorder editorconfig --- .editorconfig | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.editorconfig b/.editorconfig index 386c27fceb8..146224e7330 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,13 +1,6 @@ # http://editorconfig.org root = true -[*.go] -indent_style = tab -indent_size = 4 -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - [*] indent_style = space indent_size = 2 @@ -15,5 +8,12 @@ charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true +[*.go] +indent_style = tab +indent_size = 4 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + [*.md] trim_trailing_whitespace = false From 9666f45e9c2447db0614f902eb08b3f82673c904 Mon Sep 17 00:00:00 2001 From: Joseph Weigl Date: Thu, 24 Aug 2017 13:40:33 +0200 Subject: [PATCH 2/5] Add values to the hipchat card --- pkg/services/alerting/notifiers/hipchat.go | 31 +++++++++++----------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/pkg/services/alerting/notifiers/hipchat.go b/pkg/services/alerting/notifiers/hipchat.go index 0eb21865b9f..03176c64729 100644 --- a/pkg/services/alerting/notifiers/hipchat.go +++ b/pkg/services/alerting/notifiers/hipchat.go @@ -84,15 +84,13 @@ func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error { return err } - message := evalContext.GetNotificationTitle() + " in state " + evalContext.GetStateModel().Text + "
Check Dasboard" - fields := make([]map[string]interface{}, 0) - message += "
" + attributes := make([]map[string]interface{}, 0) for index, evt := range evalContext.EvalMatches { - message += evt.Metric + " :: " + strconv.FormatFloat(evt.Value.Float64, 'f', -1, 64) + "
" - fields = append(fields, map[string]interface{}{ - "title": evt.Metric, - "value": evt.Value, - "short": true, + attributes = append(attributes, map[string]interface{}{ + "label": evt.Metric, + "value": map[string]interface{}{ + "label": strconv.FormatFloat(evt.Value.Float64, 'f', -1, 64), + }, }) if index > maxFieldCount { break @@ -100,13 +98,15 @@ func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error { } if evalContext.Error != nil { - fields = append(fields, map[string]interface{}{ - "title": "Error message", - "value": evalContext.Error.Error(), - "short": false, + attributes = append(attributes, map[string]interface{}{ + "label": "Error message", + "value": map[string]interface{}{ + "label": evalContext.Error.Error(), + }, }) } + message := "" if evalContext.Rule.State != models.AlertStateOK { //dont add message when going back to alert state ok. message += " " + evalContext.Rule.Message } @@ -123,15 +123,16 @@ func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error { // Add a card with link to the dashboard card := map[string]interface{}{ - "style": "link", + "style": "application", "url": ruleUrl, "id": "1", "title": evalContext.GetNotificationTitle(), - "description": evalContext.GetNotificationTitle() + " in state " + evalContext.GetStateModel().Text, + "description": message, "icon": map[string]interface{}{ "url": "https://grafana.com/assets/img/fav32.png", }, - "date": evalContext.EndTime.Unix(), + "date": evalContext.EndTime.Unix(), + "attributes": attributes, } body := map[string]interface{}{ From 81d3ab37c372642ebf151d05378663d9402974f3 Mon Sep 17 00:00:00 2001 From: Joseph Weigl Date: Thu, 24 Aug 2017 14:52:23 +0200 Subject: [PATCH 3/5] Add thumbnail to card --- pkg/services/alerting/notifiers/hipchat.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/services/alerting/notifiers/hipchat.go b/pkg/services/alerting/notifiers/hipchat.go index 03176c64729..757120db25b 100644 --- a/pkg/services/alerting/notifiers/hipchat.go +++ b/pkg/services/alerting/notifiers/hipchat.go @@ -134,6 +134,14 @@ func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error { "date": evalContext.EndTime.Unix(), "attributes": attributes, } + if len(evalContext.ImagePublicUrl) > 0 { + card["thumbnail"] = map[string]interface{}{ + "url": evalContext.ImagePublicUrl, + "url@2x": evalContext.ImagePublicUrl, + "width": 1193, + "height": 564, + } + } body := map[string]interface{}{ "message": message, From 7284db0e5494e7ba423ad188cec953951ded637e Mon Sep 17 00:00:00 2001 From: Joseph Weigl Date: Wed, 4 Oct 2017 16:07:13 +0200 Subject: [PATCH 4/5] Fix empty message and toolong attribute names Use default state message if no message is provided by the user Slice attribute name to maximum of 50 chars --- pkg/services/alerting/notifiers/hipchat.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/services/alerting/notifiers/hipchat.go b/pkg/services/alerting/notifiers/hipchat.go index 757120db25b..c131b80c578 100644 --- a/pkg/services/alerting/notifiers/hipchat.go +++ b/pkg/services/alerting/notifiers/hipchat.go @@ -86,8 +86,12 @@ func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error { attributes := make([]map[string]interface{}, 0) for index, evt := range evalContext.EvalMatches { + metricName := evt.Metric + if len(metricName) > 50 { + metricName = metricName[:50] + } attributes = append(attributes, map[string]interface{}{ - "label": evt.Metric, + "label": metricName, "value": map[string]interface{}{ "label": strconv.FormatFloat(evt.Value.Float64, 'f', -1, 64), }, @@ -110,6 +114,11 @@ func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error { if evalContext.Rule.State != models.AlertStateOK { //dont add message when going back to alert state ok. message += " " + evalContext.Rule.Message } + + if len(message) < 1 { + message = evalContext.GetNotificationTitle() + " in state " + evalContext.GetStateModel().Text + } + //HipChat has a set list of colors var color string switch evalContext.Rule.State { @@ -153,6 +162,7 @@ func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error { hipUrl := fmt.Sprintf("%s/v2/room/%s/notification?auth_token=%s", this.Url, this.RoomId, this.ApiKey) data, _ := json.Marshal(&body) + this.log.Debug(string(data)) cmd := &models.SendWebhookSync{Url: hipUrl, Body: string(data)} if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil { From 6a030b2e8c067cae5b4e77b22fec864e9286f56f Mon Sep 17 00:00:00 2001 From: Joseph Weigl Date: Thu, 5 Oct 2017 16:00:55 +0200 Subject: [PATCH 5/5] Change empty string checks and improve logging --- pkg/services/alerting/notifiers/hipchat.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/services/alerting/notifiers/hipchat.go b/pkg/services/alerting/notifiers/hipchat.go index c131b80c578..f1f63d42a04 100644 --- a/pkg/services/alerting/notifiers/hipchat.go +++ b/pkg/services/alerting/notifiers/hipchat.go @@ -115,7 +115,7 @@ func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error { message += " " + evalContext.Rule.Message } - if len(message) < 1 { + if message == "" { message = evalContext.GetNotificationTitle() + " in state " + evalContext.GetStateModel().Text } @@ -143,7 +143,7 @@ func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error { "date": evalContext.EndTime.Unix(), "attributes": attributes, } - if len(evalContext.ImagePublicUrl) > 0 { + if evalContext.ImagePublicUrl != "" { card["thumbnail"] = map[string]interface{}{ "url": evalContext.ImagePublicUrl, "url@2x": evalContext.ImagePublicUrl, @@ -162,7 +162,7 @@ func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error { hipUrl := fmt.Sprintf("%s/v2/room/%s/notification?auth_token=%s", this.Url, this.RoomId, this.ApiKey) data, _ := json.Marshal(&body) - this.log.Debug(string(data)) + this.log.Info("Request payload", "json", string(data)) cmd := &models.SendWebhookSync{Url: hipUrl, Body: string(data)} if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {