kafka.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package notifiers
  2. import (
  3. "strconv"
  4. "fmt"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. "github.com/grafana/grafana/pkg/log"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/services/alerting"
  10. )
  11. func init() {
  12. alerting.RegisterNotifier(&alerting.NotifierPlugin{
  13. Type: "kafka",
  14. Name: "Kafka REST Proxy",
  15. Description: "Sends notifications to Kafka Rest Proxy",
  16. Factory: NewKafkaNotifier,
  17. OptionsTemplate: `
  18. <h3 class="page-heading">Kafka settings</h3>
  19. <div class="gf-form">
  20. <span class="gf-form-label width-14">Kafka REST Proxy</span>
  21. <input type="text" required class="gf-form-input max-width-22" ng-model="ctrl.model.settings.kafkaRestProxy" placeholder="http://localhost:8082"></input>
  22. </div>
  23. <div class="gf-form">
  24. <span class="gf-form-label width-14">Topic</span>
  25. <input type="text" required class="gf-form-input max-width-22" ng-model="ctrl.model.settings.kafkaTopic" placeholder="topic1"></input>
  26. </div>
  27. `,
  28. })
  29. }
  30. func NewKafkaNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  31. endpoint := model.Settings.Get("kafkaRestProxy").MustString()
  32. if endpoint == "" {
  33. return nil, alerting.ValidationError{Reason: "Could not find kafka rest proxy endpoint property in settings"}
  34. }
  35. topic := model.Settings.Get("kafkaTopic").MustString()
  36. if topic == "" {
  37. return nil, alerting.ValidationError{Reason: "Could not find kafka topic property in settings"}
  38. }
  39. return &KafkaNotifier{
  40. NotifierBase: NewNotifierBase(model),
  41. Endpoint: endpoint,
  42. Topic: topic,
  43. log: log.New("alerting.notifier.kafka"),
  44. }, nil
  45. }
  46. type KafkaNotifier struct {
  47. NotifierBase
  48. Endpoint string
  49. Topic string
  50. log log.Logger
  51. }
  52. func (this *KafkaNotifier) Notify(evalContext *alerting.EvalContext) error {
  53. state := evalContext.Rule.State
  54. customData := triggMetrString
  55. for _, evt := range evalContext.EvalMatches {
  56. customData = customData + fmt.Sprintf("%s: %v\n", evt.Metric, evt.Value)
  57. }
  58. this.log.Info("Notifying Kafka", "alert_state", state)
  59. recordJSON := simplejson.New()
  60. records := make([]interface{}, 1)
  61. bodyJSON := simplejson.New()
  62. bodyJSON.Set("description", evalContext.Rule.Name+" - "+evalContext.Rule.Message)
  63. bodyJSON.Set("client", "Grafana")
  64. bodyJSON.Set("details", customData)
  65. bodyJSON.Set("incident_key", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10))
  66. ruleUrl, err := evalContext.GetRuleUrl()
  67. if err != nil {
  68. this.log.Error("Failed get rule link", "error", err)
  69. return err
  70. }
  71. bodyJSON.Set("client_url", ruleUrl)
  72. if evalContext.ImagePublicUrl != "" {
  73. contexts := make([]interface{}, 1)
  74. imageJSON := simplejson.New()
  75. imageJSON.Set("type", "image")
  76. imageJSON.Set("src", evalContext.ImagePublicUrl)
  77. contexts[0] = imageJSON
  78. bodyJSON.Set("contexts", contexts)
  79. }
  80. valueJSON := simplejson.New()
  81. valueJSON.Set("value", bodyJSON)
  82. records[0] = valueJSON
  83. recordJSON.Set("records", records)
  84. body, _ := recordJSON.MarshalJSON()
  85. topicUrl := this.Endpoint + "/topics/" + this.Topic
  86. cmd := &m.SendWebhookSync{
  87. Url: topicUrl,
  88. Body: string(body),
  89. HttpMethod: "POST",
  90. HttpHeader: map[string]string{
  91. "Content-Type": "application/vnd.kafka.json.v2+json",
  92. "Accept": "application/vnd.kafka.v2+json",
  93. },
  94. }
  95. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  96. this.log.Error("Failed to send notification to Kafka", "error", err, "body", string(body))
  97. return err
  98. }
  99. return nil
  100. }