forked from warpnet/ses_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathses_exporter.go
122 lines (104 loc) · 3.42 KB
/
ses_exporter.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
package main
import (
"log/slog"
"net/http"
"os"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promslog"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ses"
)
var (
metricsPath = kingpin.Flag(
"web.telemetry-path",
"Path under which to expose metrics.",
).Default("/metrics").String()
toolkitFlags = webflag.AddFlags(kingpin.CommandLine, ":9101")
// Metrics
sesMax24HourSend = promauto.NewGauge(prometheus.GaugeOpts{
Name: "ses_quota_max",
Help: "The maximum number of emails the user is allowed to send in a 24-hour interval.",
})
sesMaxSendRate = promauto.NewGauge(prometheus.GaugeOpts{
Name: "ses_quota_rate",
Help: "The maximum number of emails that Amazon SES can accept from the user's account per second.",
})
sesSentLast24Hours = promauto.NewGauge(prometheus.GaugeOpts{
Name: "ses_quota_sent",
Help: "The number of emails sent during the previous 24 hours.",
})
client *ses.SES
logger *slog.Logger
)
// Periodically retrieve the SES metrics using the AWS client.
func recordMetrics() {
go func() {
for {
// Retrieve the current quota.
result, err := client.GetSendQuota(nil)
if err != nil {
logger.Error("Error retrieving the sending limits for the Amazon SES account", "err", err)
}
// Update the gauges.
sesMax24HourSend.Set(aws.Float64Value(result.Max24HourSend))
sesMaxSendRate.Set(aws.Float64Value(result.MaxSendRate))
sesSentLast24Hours.Set(aws.Float64Value(result.SentLast24Hours))
// Sleep for 5 seconds.
time.Sleep(5 * time.Second)
}
}()
}
func main() {
promslogConfig := &promslog.Config{}
kingpin.Version(version.Print("ses_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger = promslog.New(promslogConfig)
logger.Info("Starting ses_exporter", "version", version.Info())
logger.Info("operational information", "build_context", version.BuildContext())
// Initialize a session that the SDK uses to load
// credentials from the shared credentials file ~/.aws/credentials
// and configuration from the shared configuration file ~/.aws/config.
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// Create an SES client with a session.
client = ses.New(sess)
recordMetrics()
// Define the metrics page based on the metrics path.
http.Handle(*metricsPath, promhttp.Handler())
// Define the landing page if the metrics path is not set to the root.
if *metricsPath != "/" {
landingConfig := web.LandingConfig{
Name: "AWS SES Exporter",
Description: "Amazon Simple Email Service Exporter",
Version: version.Info(),
Links: []web.LandingLinks{
{
Address: *metricsPath,
Text: "Metrics",
},
},
}
landingPage, err := web.NewLandingPage(landingConfig)
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
http.Handle("/", landingPage)
}
// Start the HTTP server.
srv := &http.Server{}
if err := web.ListenAndServe(srv, toolkitFlags, logger); err != nil {
logger.Error("Error starting HTTP server", "err", err)
os.Exit(1)
}
}