Skip to content

Commit cf964ce

Browse files
committed
Forget data after twice the cache duration.
1 parent 83fea59 commit cf964ce

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

collector.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ const (
1616
)
1717

1818
type flowercareCollector struct {
19-
Sensor sensor
20-
CacheDuration time.Duration
19+
Sensor sensor
20+
RefreshDuration time.Duration
21+
ForgetDuration time.Duration
2122

2223
dataReader func() (sensorData, error)
2324
cache sensorData
@@ -32,15 +33,16 @@ type flowercareCollector struct {
3233
temperatureDesc *prometheus.Desc
3334
}
3435

35-
func newCollector(dataReader func() (sensorData, error), cacheDuration time.Duration, sensorInfo sensor) *flowercareCollector {
36+
func newCollector(dataReader func() (sensorData, error), refreshDuration time.Duration, sensorInfo sensor) *flowercareCollector {
3637
constLabels := prometheus.Labels{
3738
"macaddress": strings.ToLower(sensorInfo.MacAddress),
3839
"name": sensorInfo.Name,
3940
}
4041

4142
return &flowercareCollector{
42-
Sensor: sensorInfo,
43-
CacheDuration: cacheDuration,
43+
Sensor: sensorInfo,
44+
RefreshDuration: refreshDuration,
45+
ForgetDuration: refreshDuration * 2,
4446

4547
dataReader: dataReader,
4648
upMetric: prometheus.NewGauge(prometheus.GaugeOpts{
@@ -98,7 +100,7 @@ func (c *flowercareCollector) Describe(ch chan<- *prometheus.Desc) {
98100
}
99101

100102
func (c *flowercareCollector) Collect(ch chan<- prometheus.Metric) {
101-
if time.Since(c.cache.Time) > c.CacheDuration {
103+
if time.Since(c.cache.Time) > c.RefreshDuration {
102104
data, err := c.dataReader()
103105
if err != nil {
104106
log.Printf("Error during scrape: %s", err)
@@ -114,7 +116,7 @@ func (c *flowercareCollector) Collect(ch chan<- prometheus.Metric) {
114116
c.upMetric.Collect(ch)
115117
c.scrapeErrorsMetric.Collect(ch)
116118

117-
if time.Since(c.cache.Time) < c.CacheDuration {
119+
if time.Since(c.cache.Time) < c.ForgetDuration {
118120
if err := c.collectData(ch, c.cache); err != nil {
119121
log.Printf("Error collecting metrics: %s", err)
120122
}

0 commit comments

Comments
 (0)