webhook.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package notifications
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io/ioutil"
  7. "net"
  8. "net/http"
  9. "time"
  10. "golang.org/x/net/context/ctxhttp"
  11. "github.com/grafana/grafana/pkg/log"
  12. "github.com/grafana/grafana/pkg/util"
  13. )
  14. type Webhook struct {
  15. Url string
  16. User string
  17. Password string
  18. Body string
  19. HttpMethod string
  20. HttpHeader map[string]string
  21. ContentType string
  22. }
  23. var netTransport = &http.Transport{
  24. Proxy: http.ProxyFromEnvironment,
  25. Dial: (&net.Dialer{
  26. Timeout: 30 * time.Second,
  27. DualStack: true,
  28. }).Dial,
  29. TLSHandshakeTimeout: 5 * time.Second,
  30. }
  31. var netClient = &http.Client{
  32. Timeout: time.Second * 30,
  33. Transport: netTransport,
  34. }
  35. var (
  36. webhookQueue chan *Webhook
  37. webhookLog log.Logger
  38. )
  39. func initWebhookQueue() {
  40. webhookLog = log.New("notifications.webhook")
  41. webhookQueue = make(chan *Webhook, 10)
  42. go processWebhookQueue()
  43. }
  44. func processWebhookQueue() {
  45. for {
  46. select {
  47. case webhook := <-webhookQueue:
  48. err := sendWebRequestSync(context.Background(), webhook)
  49. if err != nil {
  50. webhookLog.Error("Failed to send webrequest ", "error", err)
  51. }
  52. }
  53. }
  54. }
  55. func sendWebRequestSync(ctx context.Context, webhook *Webhook) error {
  56. webhookLog.Debug("Sending webhook", "url", webhook.Url, "http method", webhook.HttpMethod, "content type", webhook.ContentType)
  57. if webhook.HttpMethod == "" {
  58. webhook.HttpMethod = http.MethodPost
  59. }
  60. request, err := http.NewRequest(webhook.HttpMethod, webhook.Url, bytes.NewReader([]byte(webhook.Body)))
  61. if err != nil {
  62. return err
  63. }
  64. if webhook.ContentType == "" {
  65. webhook.ContentType = "application/json"
  66. }
  67. request.Header.Add("Content-Type", webhook.ContentType)
  68. request.Header.Add("User-Agent", "Grafana")
  69. if webhook.User != "" && webhook.Password != "" {
  70. request.Header.Add("Authorization", util.GetBasicAuthHeader(webhook.User, webhook.Password))
  71. }
  72. for k, v := range webhook.HttpHeader {
  73. request.Header.Set(k, v)
  74. }
  75. resp, err := ctxhttp.Do(ctx, netClient, request)
  76. if err != nil {
  77. return err
  78. }
  79. if resp.StatusCode/100 == 2 {
  80. return nil
  81. }
  82. defer resp.Body.Close()
  83. body, err := ioutil.ReadAll(resp.Body)
  84. if err != nil {
  85. return err
  86. }
  87. webhookLog.Debug("Webhook failed", "statuscode", resp.Status, "body", string(body))
  88. return fmt.Errorf("Webhook response status %v", resp.Status)
  89. }
  90. var addToWebhookQueue = func(msg *Webhook) {
  91. webhookQueue <- msg
  92. }