2525 cftimeout time.Duration
2626 gql * GraphQL
2727 log = logrus .New ()
28+
29+ // kvNamespaceCache maps accountID -> namespaceID -> namespace name.
30+ // Written by refreshKVNamespaceCache, read by fetchKVAnalytics.
31+ kvNamespaceCache = make (map [string ]map [string ]string )
32+ kvNamespaceCacheMu sync.RWMutex
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+ kvNamespaceCacheMu .Lock ()
172+ kvNamespaceCache = newCache
173+ kvNamespaceCacheMu .Unlock ()
174+ log .Info ("KV namespace cache refreshed" )
175+ }
176+
177+ func getKVNamespaceMap (accountID string ) map [string ]string {
178+ kvNamespaceCacheMu .RLock ()
179+ defer kvNamespaceCacheMu .RUnlock ()
180+ return kvNamespaceCache [accountID ]
181+ }
182+
154183func 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
0 commit comments