Skip to content

Commit 172634e

Browse files
Fix caching logic and TTL for cache
Signed-off-by: Ananya Kumar Mallik <[email protected]>
1 parent d9499df commit 172634e

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

collectors/cache.go

+34-2
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,33 @@ type collectorCacheEntry struct {
8888

8989
// NewCollectorCache returns a new CollectorCache with the given TTL
9090
func NewCollectorCache(ttl time.Duration) *CollectorCache {
91-
return &CollectorCache{
91+
c := &CollectorCache{
9292
cache: make(map[string]*collectorCacheEntry),
9393
ttl: ttl,
9494
}
95+
96+
go c.cleanup()
97+
return c
9598
}
9699

97100
// Get returns a MonitoringCollector if the key is found and not expired
101+
// If key is found it resets the TTL for the collector
98102
func (c *CollectorCache) Get(key string) (*MonitoringCollector, bool) {
99103
c.lock.RLock()
100104
defer c.lock.RUnlock()
101105

102106
entry, ok := c.cache[key]
103-
if !ok || time.Now().After(entry.expiry) {
107+
108+
if !ok {
109+
return nil, false
110+
}
111+
112+
if time.Now().After(entry.expiry) {
113+
delete(c.cache, key)
104114
return nil, false
105115
}
116+
117+
entry.expiry = time.Now().Add(c.ttl)
106118
return entry.collector, true
107119
}
108120

@@ -116,3 +128,23 @@ func (c *CollectorCache) Store(key string, collector *MonitoringCollector) {
116128
defer c.lock.Unlock()
117129
c.cache[key] = entry
118130
}
131+
132+
func (c *CollectorCache) cleanup() {
133+
ticker := time.NewTicker(5 * time.Minute)
134+
defer ticker.Stop()
135+
for range ticker.C {
136+
c.removeExpired()
137+
}
138+
}
139+
140+
func (c *CollectorCache) removeExpired() {
141+
c.lock.Lock()
142+
defer c.lock.Unlock()
143+
144+
now := time.Now()
145+
for key, entry := range c.cache {
146+
if now.After(entry.expiry) {
147+
delete(c.cache, key)
148+
}
149+
}
150+
}

stackdriver_exporter.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,15 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
205205
}
206206

207207
func newHandler(projectIDs []string, metricPrefixes []string, metricExtraFilters []collectors.MetricFilter, m *monitoring.Service, logger *slog.Logger, additionalGatherer prometheus.Gatherer) *handler {
208-
ttl := 2 * time.Hour
208+
var ttl time.Duration
209209
// Add collector caching TTL as max of deltas aggregation or descriptor caching
210210
if *monitoringMetricsAggregateDeltas || *monitoringDescriptorCacheTTL > 0 {
211-
featureTTL := *monitoringMetricsDeltasTTL
212-
if *monitoringDescriptorCacheTTL > featureTTL {
213-
featureTTL = *monitoringDescriptorCacheTTL
214-
}
215-
if featureTTL > ttl {
216-
ttl = featureTTL
211+
ttl = *monitoringMetricsDeltasTTL
212+
if *monitoringDescriptorCacheTTL > ttl {
213+
ttl = *monitoringDescriptorCacheTTL
217214
}
215+
} else {
216+
ttl = 2 * time.Hour
218217
}
219218

220219
logger.Info("Creating collector cache", "ttl", ttl)

0 commit comments

Comments
 (0)