فهرست منبع

feat(alerting): working on image rendering with alert notifications

Torkel Ödegaard 9 سال پیش
والد
کامیت
4fc50742a0

+ 6 - 0
conf/defaults.ini

@@ -382,3 +382,9 @@ interval_seconds  = 60
 
 [grafana_net]
 url = https://grafana.net
+
+#################################### S3 Temp Store ##########################
+[s3-temp-image-store]
+bucket_url =
+access_key =
+secret_key =

+ 67 - 0
pkg/components/imguploader/imguploader.go

@@ -0,0 +1,67 @@
+package imguploader
+
+import (
+	"io/ioutil"
+	"net/http"
+	"time"
+
+	"github.com/grafana/grafana/pkg/log"
+	"github.com/grafana/grafana/pkg/util"
+	"github.com/kr/s3/s3util"
+)
+
+type Uploader interface {
+	Upload(imgUrl string) (string, error)
+}
+
+type S3Uploader struct {
+	bucket    string
+	secretKey string
+	accessKey string
+}
+
+func NewS3Uploader(bucket, accessKey, secretKey string) *S3Uploader {
+	return &S3Uploader{
+		bucket:    bucket,
+		accessKey: accessKey,
+		secretKey: secretKey,
+	}
+}
+
+func (u *S3Uploader) Upload(imgUrl string) (string, error) {
+	client := http.Client{Timeout: time.Duration(60 * time.Second)}
+
+	res, err := client.Get(imgUrl)
+	if err != nil {
+		return "", err
+	}
+
+	s3util.DefaultConfig.AccessKey = u.accessKey
+	s3util.DefaultConfig.SecretKey = u.secretKey
+	log.Info("AccessKey: %s", u.accessKey)
+	log.Info("SecretKey: %s", u.secretKey)
+
+	header := make(http.Header)
+	header.Add("x-amz-acl", "public-read")
+	header.Add("Content-Type", "image/png")
+
+	fullUrl := u.bucket + util.GetRandomString(20) + ".png"
+	writer, err := s3util.Create(fullUrl, header, nil)
+	if err != nil {
+		return "", err
+	}
+
+	defer writer.Close()
+
+	imgData, err := ioutil.ReadAll(res.Body)
+	if err != nil {
+		return "", err
+	}
+
+	_, err = writer.Write(imgData)
+	if err != nil {
+		return "", err
+	}
+
+	return fullUrl, nil
+}

+ 24 - 0
pkg/models/annotation.go

@@ -0,0 +1,24 @@
+package models
+
+import (
+	"time"
+
+	"github.com/grafana/grafana/pkg/components/simplejson"
+)
+
+type AnnotationType string
+
+type Annotation struct {
+	Id            int64
+	OrgId         int64
+	Type          AnnotationType
+	Title         string
+	Text          string
+	AlertId       int64
+	UserId        int64
+	PreviousState string
+	NewState      string
+	Timestamp     time.Time
+
+	Data *simplejson.Json
+}

+ 0 - 22
pkg/models/annotations.go

@@ -1,22 +0,0 @@
-package models
-
-import (
-	"time"
-
-	"github.com/grafana/grafana/pkg/components/simplejson"
-)
-
-type AnnotationType string
-
-type AnnotationEvent struct {
-	Id        int64
-	OrgId     int64
-	Type      AnnotationType
-	Title     string
-	Text      string
-	AlertId   int64
-	UserId    int64
-	Timestamp time.Time
-
-	Data *simplejson.Json
-}

+ 1 - 0
pkg/services/alerting/interfaces.go

@@ -14,6 +14,7 @@ type Scheduler interface {
 type Notifier interface {
 	Notify(alertResult *EvalContext)
 	GetType() string
+	NeedsImage() bool
 }
 
 type Condition interface {

+ 4 - 0
pkg/services/alerting/notifier.go

@@ -22,6 +22,10 @@ func (n *RootNotifier) GetType() string {
 	return "root"
 }
 
+func (n *RootNotifier) NeedsImage() bool {
+	return false
+}
+
 func (n *RootNotifier) Notify(context *EvalContext) {
 	n.log.Info("Sending notifications for", "ruleId", context.Rule.Id)
 

+ 4 - 0
pkg/services/alerting/notifiers/base.go

@@ -8,3 +8,7 @@ type NotifierBase struct {
 func (n *NotifierBase) GetType() string {
 	return n.Type
 }
+
+func (n *NotifierBase) NeedsImage() bool {
+	return true
+}