aboutsummaryrefslogtreecommitdiff
path: root/collector/collector.go
diff options
context:
space:
mode:
authorCalle Pettersson <carlpett@users.noreply.github.com>2017-09-28 15:06:26 +0200
committerJohannes 'fish' Ziemke <github@freigeist.org>2017-09-28 15:06:26 +0200
commit859a825bb84bf4fb911fcba445d410965945f088 (patch)
tree0726b08cf03c790bff3a34a085cecc01e3b927e2 /collector/collector.go
parent3762191e66e309106e930ab11f9c080fb4428001 (diff)
downloadprometheus_node_collector-859a825bb84bf4fb911fcba445d410965945f088.tar.bz2
prometheus_node_collector-859a825bb84bf4fb911fcba445d410965945f088.tar.xz
prometheus_node_collector-859a825bb84bf4fb911fcba445d410965945f088.zip
Replace --collectors.enabled with per-collector flags (#640)
* Move NodeCollector into package collector * Refactor collector enabling * Update README with new collector enabled flags * Fix out-of-date inline flag reference syntax * Use new flags in end-to-end tests * Add flag to disable all default collectors * Track if a flag has been set explicitly * Add --collectors.disable-defaults to README * Revert disable-defaults flag * Shorten flags * Fixup timex collector registration * Fix end-to-end tests * Change procfs and sysfs path flags * Fix review comments
Diffstat (limited to 'collector/collector.go')
-rw-r--r--collector/collector.go107
1 files changed, 104 insertions, 3 deletions
diff --git a/collector/collector.go b/collector/collector.go
index 72b81e3..1176b6f 100644
--- a/collector/collector.go
+++ b/collector/collector.go
@@ -15,20 +15,121 @@
15package collector 15package collector
16 16
17import ( 17import (
18 "fmt"
19 "sync"
20 "time"
21
18 "github.com/prometheus/client_golang/prometheus" 22 "github.com/prometheus/client_golang/prometheus"
19 "github.com/prometheus/common/log" 23 "github.com/prometheus/common/log"
24 "gopkg.in/alecthomas/kingpin.v2"
20) 25)
21 26
22// Namespace defines the common namespace to be used by all metrics. 27// Namespace defines the common namespace to be used by all metrics.
23const Namespace = "node" 28const namespace = "node"
24 29
25// Factories contains the list of all available collectors. 30var (
26var Factories = make(map[string]func() (Collector, error)) 31 scrapeDurationDesc = prometheus.NewDesc(
32 prometheus.BuildFQName(namespace, "scrape", "collector_duration_seconds"),
33 "node_exporter: Duration of a collector scrape.",
34 []string{"collector"},
35 nil,
36 )
37 scrapeSuccessDesc = prometheus.NewDesc(
38 prometheus.BuildFQName(namespace, "scrape", "collector_success"),
39 "node_exporter: Whether a collector succeeded.",
40 []string{"collector"},
41 nil,
42 )
43)
27 44
28func warnDeprecated(collector string) { 45func warnDeprecated(collector string) {
29 log.Warnf("The %s collector is deprecated and will be removed in the future!", collector) 46 log.Warnf("The %s collector is deprecated and will be removed in the future!", collector)
30} 47}
31 48
49const (
50 defaultEnabled = true
51 defaultDisabled = false
52)
53
54var (
55 factories = make(map[string]func() (Collector, error))
56 collectorState = make(map[string]*bool)
57)
58
59func registerCollector(collector string, isDefaultEnabled bool, factory func() (Collector, error)) {
60 var helpDefaultState string
61 if isDefaultEnabled {
62 helpDefaultState = "enabled"
63 } else {
64 helpDefaultState = "disabled"
65 }
66
67 flagName := fmt.Sprintf("collector.%s", collector)
68 flagHelp := fmt.Sprintf("Enable the %s collector (default: %s).", collector, helpDefaultState)
69 defaultValue := fmt.Sprintf("%v", isDefaultEnabled)
70
71 flag := kingpin.Flag(flagName, flagHelp).Default(defaultValue).Bool()
72 collectorState[collector] = flag
73
74 factories[collector] = factory
75}
76
77// NodeCollector implements the prometheus.Collector interface.
78type nodeCollector struct {
79 Collectors map[string]Collector
80}
81
82// NewNodeCollector creates a new NodeCollector
83func NewNodeCollector() (*nodeCollector, error) {
84 collectors := make(map[string]Collector)
85 for key, enabled := range collectorState {
86 if *enabled {
87 collector, err := factories[key]()
88 if err != nil {
89 return nil, err
90 }
91 collectors[key] = collector
92 }
93 }
94 return &nodeCollector{Collectors: collectors}, nil
95}
96
97// Describe implements the prometheus.Collector interface.
98func (n nodeCollector) Describe(ch chan<- *prometheus.Desc) {
99 ch <- scrapeDurationDesc
100 ch <- scrapeSuccessDesc
101}
102
103// Collect implements the prometheus.Collector interface.
104func (n nodeCollector) Collect(ch chan<- prometheus.Metric) {
105 wg := sync.WaitGroup{}
106 wg.Add(len(n.Collectors))
107 for name, c := range n.Collectors {
108 go func(name string, c Collector) {
109 execute(name, c, ch)
110 wg.Done()
111 }(name, c)
112 }
113 wg.Wait()
114}
115
116func execute(name string, c Collector, ch chan<- prometheus.Metric) {
117 begin := time.Now()
118 err := c.Update(ch)
119 duration := time.Since(begin)
120 var success float64
121
122 if err != nil {
123 log.Errorf("ERROR: %s collector failed after %fs: %s", name, duration.Seconds(), err)
124 success = 0
125 } else {
126 log.Debugf("OK: %s collector succeeded after %fs.", name, duration.Seconds())
127 success = 1
128 }
129 ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, duration.Seconds(), name)
130 ch <- prometheus.MustNewConstMetric(scrapeSuccessDesc, prometheus.GaugeValue, success, name)
131}
132
32// Collector is the interface a collector has to implement. 133// Collector is the interface a collector has to implement.
33type Collector interface { 134type Collector interface {
34 // Get new metrics and expose them via prometheus registry. 135 // Get new metrics and expose them via prometheus registry.