Skip to content

Commit ae08fd6

Browse files
committed
SD-11577: decouple kv namespace from scrape interval
1 parent ddc5fc5 commit ae08fd6

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

main.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"runtime"
88
"strings"
99
"sync"
10+
"sync/atomic"
1011
"time"
1112

1213
"github.com/nelkinda/health-go"
@@ -25,6 +26,10 @@ var (
2526
cftimeout time.Duration
2627
gql *GraphQL
2728
log = logrus.New()
29+
30+
// kvNamespaceCache stores accountID -> namespaceID -> name.
31+
// Atomically replaced by refreshKVNamespaceCache, read by getKVNamespaceMap.
32+
kvNamespaceCache atomic.Pointer[map[string]map[string]string]
2833
)
2934

3035
// var (
@@ -151,6 +156,30 @@ func fetchMetrics(deniedMetricsSet MetricsSet) {
151156
wg.Wait()
152157
}
153158

159+
func refreshKVNamespaceCache() {
160+
accounts := fetchAccounts()
161+
newCache := make(map[string]map[string]string, len(accounts))
162+
for _, a := range accounts {
163+
nsMap, err := fetchKVNamespaces(a.ID)
164+
if err != nil {
165+
log.Warnf("failed to refresh KV namespace cache for account %s: %v", a.ID, err)
166+
continue
167+
}
168+
newCache[a.ID] = nsMap
169+
}
170+
171+
kvNamespaceCache.Store(&newCache)
172+
log.Info("KV namespace cache refreshed")
173+
}
174+
175+
func getKVNamespaceMap(accountID string) map[string]string {
176+
cache := kvNamespaceCache.Load()
177+
if cache == nil {
178+
return nil
179+
}
180+
return (*cache)[accountID]
181+
}
182+
154183
func runExporter() {
155184
cfgMetricsPath := viper.GetString("metrics_path")
156185

@@ -174,6 +203,14 @@ func runExporter() {
174203
log.Debugf("Metrics set: %v", metricsSet)
175204
mustRegisterMetrics(metricsSet)
176205

206+
// Populate KV namespace cache at boot, then refresh every 5 minutes.
207+
refreshKVNamespaceCache()
208+
go func() {
209+
for range time.NewTicker(5 * time.Minute).C {
210+
refreshKVNamespaceCache()
211+
}
212+
}()
213+
177214
scrapeInterval := time.Duration(viper.GetInt("scrape_interval")) * time.Second
178215
log.Info("Scrape interval set to ", scrapeInterval)
179216

prometheus.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -631,11 +631,7 @@ func fetchKVAnalytics(account cfaccounts.Account, wg *sync.WaitGroup, deniedMetr
631631
wg.Add(1)
632632
defer wg.Done()
633633

634-
namespaceMap, err := fetchKVNamespaces(account.ID)
635-
if err != nil {
636-
log.Error("failed to fetch KV namespaces for account ", account.ID, ": ", err)
637-
return
638-
}
634+
namespaceMap := getKVNamespaceMap(account.ID)
639635

640636
r, err := fetchKVOperations(account.ID)
641637
if err != nil {

0 commit comments

Comments
 (0)