This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
collector.go
143 lines (122 loc) · 4.34 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright 2022 Koor Technologies, Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"sync"
"time"
"github.com/koor-tech/extended-ceph-exporter/collector"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)
var (
scrapeDurationDesc = prometheus.NewDesc(
prometheus.BuildFQName(collector.Namespace, "scrape", "collector_duration_seconds"),
"Duration of a collector scrape.",
[]string{"collector"},
nil,
)
scrapeSuccessDesc = prometheus.NewDesc(
prometheus.BuildFQName(collector.Namespace, "scrape", "collector_success"),
"Whether a collector succeeded.",
[]string{"collector"},
nil,
)
)
// ExtendedCephMetricsCollector contains the collectors to be used
type ExtendedCephMetricsCollector struct {
log *logrus.Logger
lastCollectTime time.Time
collectors map[string]collector.Collector
// Cache related
cachingEnabled bool
cacheDuration time.Duration
cache []prometheus.Metric
cacheMutex sync.Mutex
}
func NewExtendedCephMetricsCollector(log *logrus.Logger, collectors map[string]collector.Collector, cachingEnabled bool, cacheDuration time.Duration) *ExtendedCephMetricsCollector {
return &ExtendedCephMetricsCollector{
log: log,
cache: make([]prometheus.Metric, 0),
lastCollectTime: time.Unix(0, 0),
collectors: collectors,
cachingEnabled: cachingEnabled,
cacheDuration: cacheDuration,
}
}
// Describe implements the prometheus.Collector interface.
func (n *ExtendedCephMetricsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- scrapeDurationDesc
ch <- scrapeSuccessDesc
}
// Collect implements the prometheus.Collector interface.
func (n *ExtendedCephMetricsCollector) Collect(outgoingCh chan<- prometheus.Metric) {
if n.cachingEnabled {
n.cacheMutex.Lock()
defer n.cacheMutex.Unlock()
expiry := n.lastCollectTime.Add(n.cacheDuration)
if time.Now().Before(expiry) {
n.log.Debugf("Using cache. Now: %s, Expiry: %s, LastCollect: %s", time.Now().String(), expiry.String(), n.lastCollectTime.String())
for _, cachedMetric := range n.cache {
n.log.Debugf("Pushing cached metric %s to outgoingCh", cachedMetric.Desc().String())
outgoingCh <- cachedMetric
}
return
}
// Clear cache, but keep slice
n.cache = n.cache[:0]
}
metricsCh := make(chan prometheus.Metric)
// Wait to ensure outgoingCh is not closed before the goroutine is finished
wgOutgoing := sync.WaitGroup{}
wgOutgoing.Add(1)
go func() {
for metric := range metricsCh {
outgoingCh <- metric
if n.cachingEnabled {
n.log.Debugf("Appending metric %s to cache", metric.Desc().String())
n.cache = append(n.cache, metric)
}
}
n.log.Debug("Finished pushing metrics from metricsCh to outgoingCh")
wgOutgoing.Done()
}()
wgCollection := sync.WaitGroup{}
wgCollection.Add(len(n.collectors))
for name, coll := range n.collectors {
go func(name string, coll collector.Collector) {
begin := time.Now()
err := coll.Update(metricsCh)
duration := time.Since(begin)
var success float64
if err != nil {
n.log.Errorf("%s collector failed after %fs: %s", name, duration.Seconds(), err)
success = 0
} else {
n.log.Debugf("%s collector succeeded after %fs.", name, duration.Seconds())
success = 1
}
metricsCh <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, duration.Seconds(), name)
metricsCh <- prometheus.MustNewConstMetric(scrapeSuccessDesc, prometheus.GaugeValue, success, name)
wgCollection.Done()
}(name, coll)
}
n.log.Debug("Waiting for collectors")
wgCollection.Wait()
n.log.Debug("Finished waiting for collectors")
n.lastCollectTime = time.Now()
n.log.Debugf("Updated lastCollectTime to %s", n.lastCollectTime.String())
close(metricsCh)
n.log.Debug("Waiting for outgoing Adapter")
wgOutgoing.Wait()
n.log.Debug("Finished waiting for outgoing Adapter")
}