Skip to content

Commit

Permalink
Fix caching logic and TTL for cache
Browse files Browse the repository at this point in the history
Signed-off-by: Ananya Kumar Mallik <[email protected]>
  • Loading branch information
ananya-mallik-ps committed Dec 6, 2024
1 parent d9499df commit 172634e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
36 changes: 34 additions & 2 deletions collectors/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,33 @@ type collectorCacheEntry struct {

// NewCollectorCache returns a new CollectorCache with the given TTL
func NewCollectorCache(ttl time.Duration) *CollectorCache {
return &CollectorCache{
c := &CollectorCache{
cache: make(map[string]*collectorCacheEntry),
ttl: ttl,
}

go c.cleanup()
return c
}

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

entry, ok := c.cache[key]
if !ok || time.Now().After(entry.expiry) {

if !ok {
return nil, false
}

if time.Now().After(entry.expiry) {
delete(c.cache, key)
return nil, false
}

entry.expiry = time.Now().Add(c.ttl)
return entry.collector, true
}

Expand All @@ -116,3 +128,23 @@ func (c *CollectorCache) Store(key string, collector *MonitoringCollector) {
defer c.lock.Unlock()
c.cache[key] = entry
}

func (c *CollectorCache) cleanup() {
ticker := time.NewTicker(5 * time.Minute)
defer ticker.Stop()
for range ticker.C {
c.removeExpired()
}
}

func (c *CollectorCache) removeExpired() {
c.lock.Lock()
defer c.lock.Unlock()

now := time.Now()
for key, entry := range c.cache {
if now.After(entry.expiry) {
delete(c.cache, key)
}
}
}
13 changes: 6 additions & 7 deletions stackdriver_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,15 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

func newHandler(projectIDs []string, metricPrefixes []string, metricExtraFilters []collectors.MetricFilter, m *monitoring.Service, logger *slog.Logger, additionalGatherer prometheus.Gatherer) *handler {
ttl := 2 * time.Hour
var ttl time.Duration
// Add collector caching TTL as max of deltas aggregation or descriptor caching
if *monitoringMetricsAggregateDeltas || *monitoringDescriptorCacheTTL > 0 {
featureTTL := *monitoringMetricsDeltasTTL
if *monitoringDescriptorCacheTTL > featureTTL {
featureTTL = *monitoringDescriptorCacheTTL
}
if featureTTL > ttl {
ttl = featureTTL
ttl = *monitoringMetricsDeltasTTL
if *monitoringDescriptorCacheTTL > ttl {
ttl = *monitoringDescriptorCacheTTL
}
} else {
ttl = 2 * time.Hour
}

logger.Info("Creating collector cache", "ttl", ttl)
Expand Down

0 comments on commit 172634e

Please sign in to comment.