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

This commit is contained in:
Torkel Ödegaard 2016-07-29 14:55:02 +02:00
parent f9a28d3306
commit 4fc50742a0
7 changed files with 106 additions and 22 deletions

View File

@ -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 =

View File

@ -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
pkg/models/annotation.go Normal file
View File

@ -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
}

View File

@ -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
}

View File

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

View File

@ -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)

View File

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