forked from hypoport/mandrill-prometheus-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (108 loc) · 4.39 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
sent = prometheus.NewDesc("mandrill_sent_total", "Total number of sent mails.", []string{"tag"}, nil)
hardBounces = prometheus.NewDesc("mandrill_hard_bounces", "Number of mails bounced hard", []string{"tag"}, nil)
softBounces = prometheus.NewDesc("mandrill_soft_bounces", "Number of mails bounced soft", []string{"tag"}, nil)
rejects = prometheus.NewDesc("mandrill_rejects", "Number of mails rejected", []string{"tag"}, nil)
complaints = prometheus.NewDesc("mandrill_complaints", "Number of complaints", []string{"tag"}, nil)
unsubs = prometheus.NewDesc("mandrill_unsubs", "Number of unsubscribes", []string{"tag"}, nil)
opens = prometheus.NewDesc("mandrill_opens", "Number of mails opened", []string{"tag"}, nil)
clicks = prometheus.NewDesc("mandrill_clicks", "Number of clicks inside mails", []string{"tag"}, nil)
uniqueOpens = prometheus.NewDesc("mandrill_unique_opens", "Unique number of mails opened", []string{"tag"}, nil)
uniqueClicks = prometheus.NewDesc("mandrill_unique_clicks", "Unique number of clicks", []string{"tag"}, nil)
reputation = prometheus.NewDesc("mandrill_reputation", "Mandrill reputation", []string{"tag"}, nil)
)
type mandrillCollector struct {
apiKey string
}
func (m mandrillCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- sent
ch <- hardBounces
ch <- softBounces
ch <- rejects
ch <- complaints
ch <- unsubs
ch <- opens
ch <- clicks
ch <- uniqueOpens
ch <- uniqueClicks
ch <- reputation
}
type mandrillTagData struct {
Tag string
Sent int
HardBounces int `json:"hard_bounces"`
SoftBounces int `json:"soft_bounces"`
Rejects int
Complaints int
Unsubs int
Opens int
Clicks int
UniqueOpens int `json:"unique_opens"`
UniqueClicks int `json:"unique_clicks"`
Reputation int
}
func (m mandrillCollector) Collect(ch chan<- prometheus.Metric) {
//get Tags from Mandrill
tagData, err := m.getTags()
if err != nil {
log.Print(err)
}
//iterate over tags and get stats
for _, tag := range tagData {
ch <- prometheus.MustNewConstMetric(sent, prometheus.CounterValue, float64(tag.Sent), tag.Tag)
ch <- prometheus.MustNewConstMetric(hardBounces, prometheus.CounterValue, float64(tag.HardBounces), tag.Tag)
ch <- prometheus.MustNewConstMetric(softBounces, prometheus.CounterValue, float64(tag.SoftBounces), tag.Tag)
ch <- prometheus.MustNewConstMetric(rejects, prometheus.CounterValue, float64(tag.Rejects), tag.Tag)
ch <- prometheus.MustNewConstMetric(complaints, prometheus.CounterValue, float64(tag.Complaints), tag.Tag)
ch <- prometheus.MustNewConstMetric(unsubs, prometheus.CounterValue, float64(tag.Unsubs), tag.Tag)
ch <- prometheus.MustNewConstMetric(opens, prometheus.CounterValue, float64(tag.Opens), tag.Tag)
ch <- prometheus.MustNewConstMetric(clicks, prometheus.CounterValue, float64(tag.Clicks), tag.Tag)
ch <- prometheus.MustNewConstMetric(uniqueOpens, prometheus.CounterValue, float64(tag.UniqueOpens), tag.Tag)
ch <- prometheus.MustNewConstMetric(uniqueClicks, prometheus.CounterValue, float64(tag.UniqueClicks), tag.Tag)
ch <- prometheus.MustNewConstMetric(reputation, prometheus.CounterValue, float64(tag.Reputation), tag.Tag)
}
}
func (m mandrillCollector) getTags() ([]mandrillTagData, error) {
body := bytes.Buffer{}
body.WriteString("{\"key\": \"")
body.WriteString(m.apiKey)
body.WriteString("\"}")
client := &http.Client{}
req, err := http.NewRequest("POST", "https://mandrillapp.com/api/1.0/tags/list.json", &body)
req.Header.Add("Content-Type", "application/json; charset=utf-8")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
result := []mandrillTagData{}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return nil, err
}
return result, nil
}
func main() {
mc := mandrillCollector{
apiKey: os.Getenv("MANDRILL_API_KEY"),
}
reg := prometheus.NewPedanticRegistry()
reg.MustRegister(mc)
//health check
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
//port 9153 https://github.com/prometheus/prometheus/wiki/Default-port-allocations
http.ListenAndServe(":9153", nil)
}