This repository has been archived by the owner on Nov 25, 2022. It is now read-only.
forked from hellofresh/rds_exporter
/
scraper.go
172 lines (147 loc) · 5.44 KB
/
scraper.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package enhanced
import (
"context"
"time"
"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/cloudwatchlogs"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/theurichde/cloudwatch_rds_exporter/internal/sessions"
)
// scraper retrieves metrics from several RDS instances sharing a single session.
type scraper struct {
instances []sessions.Instance
logStreamNames []string
svc *cloudwatchlogs.CloudWatchLogs
nextStartTime time.Time
logger log.Logger
}
func newScraper(session *session.Session, instances []sessions.Instance) *scraper {
logStreamNames := make([]string, 0, len(instances))
for _, instance := range instances {
logStreamNames = append(logStreamNames, instance.ResourceID)
}
return &scraper{
instances: instances,
logStreamNames: logStreamNames,
svc: cloudwatchlogs.New(session),
nextStartTime: time.Now().Add(-3 * time.Minute).Round(0), // strip monotonic clock reading
logger: log.With("component", "enhanced"),
}
}
// start scrapes metrics in loop and sends them to the channel until context is canceled.
func (s *scraper) start(ctx context.Context, interval time.Duration, ch chan<- map[string][]prometheus.Metric) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
// nothing
case <-ctx.Done():
return
}
scrapeCtx, cancel := context.WithTimeout(ctx, interval)
ch <- s.scrape(scrapeCtx)
cancel()
}
}
// scrape performs a single scrape.
func (s *scraper) scrape(ctx context.Context) map[string][]prometheus.Metric {
allMetrics := make(map[string]map[time.Time][]prometheus.Metric) // ResourceID -> event timestamp -> metrics
// LogStreamNames parameter supports up to 100 items.
// https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_FilterLogEvents.html
streamCount := len(s.logStreamNames)
for i := 0; i < streamCount; i += 100 {
sliceStart := i
sliceEnd := i + 100
if sliceEnd > streamCount {
sliceEnd = streamCount
}
input := &cloudwatchlogs.FilterLogEventsInput{
LogGroupName: aws.String("RDSOSMetrics"),
LogStreamNames: aws.StringSlice(s.logStreamNames[sliceStart:sliceEnd]),
StartTime: aws.Int64(aws.TimeUnixMilli(s.nextStartTime)),
}
s.logger.With("next_start", s.nextStartTime.UTC()).With("since_last", time.Since(s.nextStartTime)).Debugf("Requesting metrics")
collectAllMetrics := func(output *cloudwatchlogs.FilterLogEventsOutput, lastPage bool) bool {
for _, event := range output.Events {
l := s.logger.With("EventId", *event.EventId).With("LogStreamName", *event.LogStreamName)
l = l.With("Timestamp", aws.MillisecondsTimeValue(event.Timestamp).UTC())
l = l.With("IngestionTime", aws.MillisecondsTimeValue(event.IngestionTime).UTC())
var instance *sessions.Instance
for _, i := range s.instances {
if i.ResourceID == *event.LogStreamName {
instance = &i
break
}
}
if instance == nil {
l.Errorf("Failed to find instance")
continue
}
l = l.With("region", instance.Region).With("instance", instance.Instance)
// l.Debugf("Message:\n%s", *event.Message)
osMetrics, err := parseOSMetrics([]byte(*event.Message))
if err != nil {
l.Errorf("Failed to parse metrics: %s.", err)
continue
}
// l.Debugf("OS Metrics:\n%#v", osMetrics)
if allMetrics[instance.ResourceID] == nil {
allMetrics[instance.ResourceID] = make(map[time.Time][]prometheus.Metric)
}
timestamp := aws.MillisecondsTimeValue(event.Timestamp)
metrics := osMetrics.makePrometheusMetrics(instance.Region)
allMetrics[instance.ResourceID][timestamp] = metrics
s.logger.With("timestamp", osMetrics.Timestamp.UTC()).Debugf("Timestamp from Message")
}
return true // continue pagination
}
if err := s.svc.FilterLogEventsPagesWithContext(ctx, input, collectAllMetrics); err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == cloudwatchlogs.ErrCodeResourceNotFoundException {
s.logger.Warnf("Enhanced monitoring problem: %s", awsErr.Message())
}
} else {
s.logger.Errorf("Failed to filter log events: %s.", err)
}
}
}
// get better times
allTimes := make(map[string][]time.Time)
for resourceID, events := range allMetrics {
allTimes[resourceID] = make([]time.Time, 0, len(events))
for timestamp := range events {
allTimes[resourceID] = append(allTimes[resourceID], timestamp)
}
}
var times map[string]time.Time
times, s.nextStartTime = betterTimes(allTimes)
// return only latest metrics
res := make(map[string][]prometheus.Metric) // ResourceID -> metrics
for resourceID, timestamp := range times {
res[resourceID] = allMetrics[resourceID][timestamp]
}
return res
}
// betterTimes returns timestamps of the latest metrics, and also StarTime that should be used in the next request
func betterTimes(allTimes map[string][]time.Time) (times map[string]time.Time, nextStartTime time.Time) {
// keep only the most recent metrics for each instance
nextStartTime = time.Now()
times = make(map[string]time.Time) // ResourceID -> timestamp
for resourceID, events := range allTimes {
var newest time.Time
for _, timestamp := range events {
if newest.Before(timestamp) {
newest = timestamp
times[resourceID] = timestamp
}
}
if nextStartTime.After(newest) {
nextStartTime = newest
}
}
return
}