forked from Technofy/cloudwatch_exporter
/
collector.go
87 lines (72 loc) · 1.74 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
package basic
import (
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/percona/rds_exporter/config"
"github.com/percona/rds_exporter/sessions"
)
//go:generate go run generate/main.go generate/utils.go
var (
scrapeTimeDesc = prometheus.NewDesc(
"rds_exporter_scrape_duration_seconds",
"Time this RDS scrape took, in seconds.",
[]string{},
nil,
)
)
type Metric struct {
cwName string
prometheusName string
prometheusHelp string
}
type Collector struct {
config *config.Config
sessions *sessions.Sessions
metrics []Metric
l log.Logger
}
// New creates a new instance of a Collector.
func New(config *config.Config, sessions *sessions.Sessions) *Collector {
return &Collector{
config: config,
sessions: sessions,
metrics: Metrics,
l: log.With("component", "basic"),
}
}
func (e *Collector) Describe(ch chan<- *prometheus.Desc) {
// unchecked collector
}
func (e *Collector) Collect(ch chan<- prometheus.Metric) {
now := time.Now()
e.collect(ch)
// Collect scrape time
ch <- prometheus.MustNewConstMetric(scrapeTimeDesc, prometheus.GaugeValue, time.Since(now).Seconds())
}
func (e *Collector) collect(ch chan<- prometheus.Metric) {
var wg sync.WaitGroup
defer wg.Wait()
for _, instance := range e.config.Instances {
if instance.DisableBasicMetrics {
e.l.Debugf("Instance %s has disabled basic metrics, skipping.", instance)
continue
}
instance := instance
wg.Add(1)
go func() {
defer wg.Done()
s := NewScraper(&instance, e, ch)
if s == nil {
e.l.Errorf("No scraper for %s, skipping.", instance)
return
}
s.Scrape()
}()
}
}
// check interfaces
var (
_ prometheus.Collector = (*Collector)(nil)
)