kafka.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/infra/log"
  8. "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. // NewKafkaNotifier is the constructor function for the Kafka notifier.
  31. func NewKafkaNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
  32. endpoint := model.Settings.Get("kafkaRestProxy").MustString()
  33. if endpoint == "" {
  34. return nil, alerting.ValidationError{Reason: "Could not find kafka rest proxy endpoint property in settings"}
  35. }
  36. topic := model.Settings.Get("kafkaTopic").MustString()
  37. if topic == "" {
  38. return nil, alerting.ValidationError{Reason: "Could not find kafka topic property in settings"}
  39. }
  40. return &KafkaNotifier{
  41. NotifierBase: NewNotifierBase(model),
  42. Endpoint: endpoint,
  43. Topic: topic,
  44. log: log.New("alerting.notifier.kafka"),
  45. }, nil
  46. }
  47. // KafkaNotifier is responsible for sending
  48. // alert notifications to Kafka.
  49. type KafkaNotifier struct {
  50. NotifierBase
  51. Endpoint string
  52. Topic string
  53. log log.Logger
  54. }
  55. // Notify sends the alert notification.
  56. func (kn *KafkaNotifier) Notify(evalContext *alerting.EvalContext) error {
  57. state := evalContext.Rule.State
  58. customData := triggMetrString
  59. for _, evt := range evalContext.EvalMatches {
  60. customData = customData + fmt.Sprintf("%s: %v\n", evt.Metric, evt.Value)
  61. }
  62. kn.log.Info("Notifying Kafka", "alert_state", state)
  63. recordJSON := simplejson.New()
  64. records := make([]interface{}, 1)
  65. bodyJSON := simplejson.New()
  66. bodyJSON.Set("description", evalContext.Rule.Name+" - "+evalContext.Rule.Message)
  67. bodyJSON.Set("client", "Grafana")
  68. bodyJSON.Set("details", customData)
  69. bodyJSON.Set("incident_key", "alertId-"+strconv.FormatInt(evalContext.Rule.ID, 10))
  70. ruleURL, err := evalContext.GetRuleURL()
  71. if err != nil {
  72. kn.log.Error("Failed get rule link", "error", err)
  73. return err
  74. }
  75. bodyJSON.Set("client_url", ruleURL)
  76. if evalContext.ImagePublicURL != "" {
  77. contexts := make([]interface{}, 1)
  78. imageJSON := simplejson.New()
  79. imageJSON.Set("type", "image")
  80. imageJSON.Set("src", evalContext.ImagePublicURL)
  81. contexts[0] = imageJSON
  82. bodyJSON.Set("contexts", contexts)
  83. }
  84. valueJSON := simplejson.New()
  85. valueJSON.Set("value", bodyJSON)
  86. records[0] = valueJSON
  87. recordJSON.Set("records", records)
  88. body, _ := recordJSON.MarshalJSON()
  89. topicURL := kn.Endpoint + "/topics/" + kn.Topic
  90. cmd := &models.SendWebhookSync{
  91. Url: topicURL,
  92. Body: string(body),
  93. HttpMethod: "POST",
  94. HttpHeader: map[string]string{
  95. "Content-Type": "application/vnd.kafka.json.v2+json",
  96. "Accept": "application/vnd.kafka.v2+json",
  97. },
  98. }
  99. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  100. kn.log.Error("Failed to send notification to Kafka", "error", err, "body", string(body))
  101. return err
  102. }
  103. return nil
  104. }