Skip to content

Commit 4a0e2d0

Browse files
committed
extend cache limit.
1 parent b02b353 commit 4a0e2d0

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

collector/overview_metric.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,25 @@ func (me *OverviewMetric) collect(ch chan<- prometheus.Metric) (errRet error) {
117117

118118
var cacheMetrics = make([]*prometheus.Metric, 0, len(me.MetricConfig.Statistics))
119119

120+
nowts := time.Now().Unix()
121+
120122
for _, instanceId := range instanceIds {
121123
for _, statistic := range me.MetricConfig.Statistics {
122124
statistic = strings.ToLower(statistic)
123125
cacheMetric := me.caches[instanceId+"#"+statistic]
124126
if cacheMetric != nil &&
125127
cacheMetric.metric != nil &&
126-
cacheMetric.insertTime+metricCacheTime > time.Now().Unix() {
128+
cacheMetric.insertTime+metricCacheTime > nowts {
127129
cacheMetrics = append(cacheMetrics, cacheMetric.metric)
128130
}
129131
}
130132
}
131133

132-
if len(cacheMetrics) == len(me.MetricConfig.Statistics)*len(instanceIds) {
134+
if len(cacheMetrics) > 0 {
133135
for _, cacheMetric := range cacheMetrics {
134136
ch <- *cacheMetric
135137
log.Debugf("metric read from cache,%s", (*cacheMetric).Desc().String())
136138
}
137-
138139
return
139140
}
140141

@@ -151,6 +152,7 @@ func (me *OverviewMetric) collect(ch chan<- prometheus.Metric) (errRet error) {
151152
/*
152153
Have to deal with allDataRet whether it's wrong or not.
153154
*/
155+
nowts = time.Now().Unix()
154156
for instanceId, datas := range allDataRet {
155157
if instances[instanceId] == nil {
156158
log.Errorf("It was a big api bug, because monitor api return a not exist instance id [%s] ", instanceId)
@@ -178,7 +180,7 @@ func (me *OverviewMetric) collect(ch chan<- prometheus.Metric) (errRet error) {
178180
proMetric := prometheus.MustNewConstMetric(promDesc, prometheus.GaugeValue, float64(statisticRet), labels...)
179181
me.caches[instanceId+"#"+statistic] = &MetricCache{
180182
metric: &proMetric,
181-
insertTime: time.Now().Unix(),
183+
insertTime: nowts,
182184
}
183185
ch <- proMetric
184186
_ = lastTime

collector/special_metric.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,21 @@ func (me *SpecailMetric) collect(ch chan<- prometheus.Metric) (errRet error) {
8282
errRet = fmt.Errorf("error,this product [%s] get self control monitor datas func not support yet", me.ProductName)
8383
return
8484
}
85+
86+
nowts := time.Now().Unix()
8587
var cacheMetrics = make([]*prometheus.Metric, 0, len(me.MetricConfig.Statistics))
8688
for _, statistic := range me.MetricConfig.Statistics {
8789
statistic = strings.ToLower(statistic)
8890
cacheMetric := me.caches[statistic]
8991

9092
if cacheMetric != nil &&
9193
cacheMetric.metric != nil &&
92-
cacheMetric.insertTime+metricCacheTime > time.Now().Unix() {
94+
cacheMetric.insertTime+metricCacheTime > nowts {
9395
cacheMetrics = append(cacheMetrics, cacheMetric.metric)
9496
}
9597
}
9698

97-
if len(cacheMetrics) == len(me.MetricConfig.Statistics) {
99+
if len(cacheMetrics) > 0 {
98100
for _, cacheMetric := range cacheMetrics {
99101
ch <- *cacheMetric
100102
log.Debugf("metric read from cache,%s", (*cacheMetric).Desc().String())
@@ -112,6 +114,8 @@ func (me *SpecailMetric) collect(ch chan<- prometheus.Metric) (errRet error) {
112114
return
113115
}
114116

117+
nowts = time.Now().Unix()
118+
115119
for _, statistic := range me.MetricConfig.Statistics {
116120

117121
statistic = strings.ToLower(statistic)
@@ -130,7 +134,7 @@ func (me *SpecailMetric) collect(ch chan<- prometheus.Metric) (errRet error) {
130134
proMetric := prometheus.MustNewConstMetric(promDesc, prometheus.GaugeValue, float64(statisticRet), labels...)
131135
me.caches[statistic] = &MetricCache{
132136
metric: &proMetric,
133-
insertTime: time.Now().Unix(),
137+
insertTime: nowts,
134138
}
135139
ch <- proMetric
136140
_ = lastTime

0 commit comments

Comments
 (0)