mirror of
				https://github.com/grafana/grafana.git
				synced 2025-02-25 18:55:37 -06:00 
			
		
		
		
	Merge pull request #9111 from eloo/feature/enhance_hipchat_card
Feature/enhance hipchat card
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -84,15 +84,17 @@ func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	message := evalContext.GetNotificationTitle() + " in state " + evalContext.GetStateModel().Text + "<br><a href=" + ruleUrl + ">Check Dashboard</a>"
 | 
			
		||||
	fields := make([]map[string]interface{}, 0)
 | 
			
		||||
	message += "<br>"
 | 
			
		||||
	attributes := make([]map[string]interface{}, 0)
 | 
			
		||||
	for index, evt := range evalContext.EvalMatches {
 | 
			
		||||
		message += evt.Metric + " :: " + strconv.FormatFloat(evt.Value.Float64, 'f', -1, 64) + "<br>"
 | 
			
		||||
		fields = append(fields, map[string]interface{}{
 | 
			
		||||
			"title": evt.Metric,
 | 
			
		||||
			"value": evt.Value,
 | 
			
		||||
			"short": true,
 | 
			
		||||
		metricName := evt.Metric
 | 
			
		||||
		if len(metricName) > 50 {
 | 
			
		||||
			metricName = metricName[:50]
 | 
			
		||||
		}
 | 
			
		||||
		attributes = append(attributes, map[string]interface{}{
 | 
			
		||||
			"label": metricName,
 | 
			
		||||
			"value": map[string]interface{}{
 | 
			
		||||
				"label": strconv.FormatFloat(evt.Value.Float64, 'f', -1, 64),
 | 
			
		||||
			},
 | 
			
		||||
		})
 | 
			
		||||
		if index > maxFieldCount {
 | 
			
		||||
			break
 | 
			
		||||
@@ -100,16 +102,23 @@ 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
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if message == "" {
 | 
			
		||||
		message = evalContext.GetNotificationTitle() + " in state " + evalContext.GetStateModel().Text
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	//HipChat has a set list of colors
 | 
			
		||||
	var color string
 | 
			
		||||
	switch evalContext.Rule.State {
 | 
			
		||||
@@ -123,15 +132,24 @@ 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,
 | 
			
		||||
	}
 | 
			
		||||
	if evalContext.ImagePublicUrl != "" {
 | 
			
		||||
		card["thumbnail"] = map[string]interface{}{
 | 
			
		||||
			"url":    evalContext.ImagePublicUrl,
 | 
			
		||||
			"url@2x": evalContext.ImagePublicUrl,
 | 
			
		||||
			"width":  1193,
 | 
			
		||||
			"height": 564,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	body := map[string]interface{}{
 | 
			
		||||
@@ -144,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.Info("Request payload", "json", string(data))
 | 
			
		||||
	cmd := &models.SendWebhookSync{Url: hipUrl, Body: string(data)}
 | 
			
		||||
 | 
			
		||||
	if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user