This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
grafana.go
71 lines (58 loc) · 1.7 KB
/
grafana.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package services
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"path"
"strings"
"time"
httputil "github.com/argoproj-labs/argocd-notifications/pkg/util/http"
log "github.com/sirupsen/logrus"
)
type GrafanaOptions struct {
ApiUrl string `json:"apiUrl"`
ApiKey string `json:"apiKey"`
InsecureSkipVerify bool `json:"insecureSkipVerify"`
}
type grafanaService struct {
opts GrafanaOptions
}
func NewGrafanaService(opts GrafanaOptions) NotificationService {
return &grafanaService{opts: opts}
}
type GrafanaAnnotation struct {
Time int64 `json:"time"` // unix ts in ms
IsRegion bool `json:"isRegion"`
Tags []string `json:"tags"`
Text string `json:"text"`
}
func (s *grafanaService) Send(notification Notification, dest Destination) error {
ga := GrafanaAnnotation{
Time: time.Now().Unix() * 1000, // unix ts in ms
IsRegion: false,
Tags: strings.Split(dest.Recipient, "|"),
Text: notification.Message,
}
client := &http.Client{
Transport: httputil.NewLoggingRoundTripper(
httputil.NewTransport(s.opts.ApiUrl, s.opts.InsecureSkipVerify), log.WithField("service", "grafana")),
}
jsonValue, _ := json.Marshal(ga)
apiUrl, err := url.Parse(s.opts.ApiUrl)
if err != nil {
return err
}
annotationApi := *apiUrl
annotationApi.Path = path.Join(apiUrl.Path, "annotations")
req, err := http.NewRequest("POST", annotationApi.String(), bytes.NewBuffer(jsonValue))
if err != nil {
log.Errorf("Failed to create grafana annotation request: %s", err)
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", s.opts.ApiKey))
_, err = client.Do(req)
return err
}