-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcollector.go
120 lines (105 loc) · 3.86 KB
/
collector.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
package main
import (
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
namespace = "awsses_exporter"
)
// Exporter collects metrics from aws ses.
type Exporter struct {
accessKey string
secretAccessKey string
awsSesRegions []string
max24hoursend *prometheus.Desc
maxsendrate *prometheus.Desc
sentlast24hours *prometheus.Desc
}
// NewExporter returns an initialized exporter.
func NewExporter(awsSesRegions *[]string) *Exporter {
return &Exporter{
max24hoursend: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "max24hoursend"),
"The maximum number of emails allowed to be sent in a rolling 24 hours.",
[]string{"aws_region"},
nil,
),
maxsendrate: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "maxsendrate"),
"The maximum rate of emails allowed to be sent per second.",
[]string{"aws_region"},
nil,
),
sentlast24hours: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "sentlast24hours"),
"The number of emails sent in the last 24 hours.",
[]string{"aws_region"},
nil,
),
awsSesRegions: *awsSesRegions,
}
}
// Describe describes all the metrics exported by the memcached exporter. It
// implements prometheus.Collector.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.max24hoursend
ch <- e.maxsendrate
ch <- e.sentlast24hours
}
// Collect fetches the statistics from the configured memcached server, and
// delivers them as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
for _, regionName := range e.awsSesRegions {
svc := ses.New(session.New(), aws.NewConfig().WithRegion(regionName))
input := &ses.GetSendQuotaInput{}
result, err := svc.GetSendQuota(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
log.Errorf("Failed to get sending quota from region %s: %s", regionName, aerr)
}
} else {
log.Errorf("Failed to get sending quota from region %s: %s", regionName, err)
}
return
}
ch <- prometheus.MustNewConstMetric(e.max24hoursend, prometheus.GaugeValue, *result.Max24HourSend, regionName)
ch <- prometheus.MustNewConstMetric(e.maxsendrate, prometheus.GaugeValue, *result.MaxSendRate, regionName)
ch <- prometheus.MustNewConstMetric(e.sentlast24hours, prometheus.GaugeValue, *result.SentLast24Hours, regionName)
}
}
func main() {
var (
listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9199").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
awsSesRegions = kingpin.Flag("aws.regions", "AWS Regions to be monitored.").Default("us-east-1", "us-west-2", "eu-west-1").Strings()
)
log.AddFlags(kingpin.CommandLine)
kingpin.Version(version.Print("awsses_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
log.Infoln("Starting awsses_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
prometheus.MustRegister(NewExporter(awsSesRegions))
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>AWS SES Exporter</title></head>
<body>
<h1>AWS SES Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Infoln("Starting HTTP server on", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}