-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector_base.go
96 lines (74 loc) · 1.96 KB
/
collector_base.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
package main
import (
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
var collectorGlobal CollectorGlobal
type CollectorBase struct {
Name string
scrapeTime *time.Duration
logger *log.Entry
LastScrapeDuration *time.Duration
collectionStartTime time.Time
isHidden bool
}
type CollectorGlobal struct {
prometheus struct {
stats *prometheus.GaugeVec
statsMutex sync.Mutex
api *prometheus.CounterVec
apiMutex sync.Mutex
}
}
func (c *CollectorBase) Init() {
c.isHidden = false
c.logger = log.WithField("collector", c.Name)
}
func (c *CollectorBase) SetScrapeTime(scrapeTime time.Duration) {
c.scrapeTime = &scrapeTime
}
func (c *CollectorBase) GetScrapeTime() *time.Duration {
return c.scrapeTime
}
func (c *CollectorBase) SetIsHidden(v bool) {
c.isHidden = v
}
func (c *CollectorBase) PrometheusStatsGauge() *prometheus.GaugeVec {
if collectorGlobal.prometheus.stats == nil {
collectorGlobal.prometheus.statsMutex.Lock()
collectorGlobal.prometheus.stats = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "apprelease_stats",
Help: "AppRelease exporter statistics",
},
[]string{
"name",
"type",
},
)
prometheus.MustRegister(collectorGlobal.prometheus.stats)
collectorGlobal.prometheus.statsMutex.Unlock()
}
return collectorGlobal.prometheus.stats
}
func (c *CollectorBase) collectionStart() {
c.collectionStartTime = time.Now()
if !c.isHidden {
c.logger.Info("starting metrics collection")
}
}
func (c *CollectorBase) collectionFinish() {
duration := time.Since(c.collectionStartTime)
c.LastScrapeDuration = &duration
if !c.isHidden {
c.logger.WithField("duration", c.LastScrapeDuration.Seconds()).Infof("finished metrics collection (duration: %v)", c.LastScrapeDuration)
}
}
func (c *CollectorBase) sleepUntilNextCollection() {
if !c.isHidden {
c.logger.Debugf("sleeping %v", c.GetScrapeTime().String())
}
time.Sleep(*c.GetScrapeTime())
}