66 "sync"
77 "time"
88
9+ "github.com/prometheus/client_golang/prometheus"
910 "k8s.io/klog/v2"
10-
11- "github.com/hetznercloud/hcloud-cloud-controller-manager/internal/metrics"
1211)
1312
1413type Mode string
@@ -63,9 +62,9 @@ type Cache[T any] struct {
6362 fetchAll func (ctx context.Context ) ([]* T , error )
6463 getID func (value * T ) int64
6564 getName func (value * T ) string
66-
67- defaultMaxAge time.Duration
68- defaultMode Mode
65+ metric * prometheus. CounterVec
66+ defaultMaxAge time.Duration
67+ defaultMode Mode
6968
7069 byID map [int64 ]* entry [T ]
7170 byName map [string ]* entry [T ]
@@ -79,6 +78,7 @@ func newCache[T any](
7978 fetchAll func (ctx context.Context ) ([]* T , error ),
8079 getID func (value * T ) int64 ,
8180 getName func (value * T ) string ,
81+ metric * prometheus.CounterVec ,
8282 defaultMode Mode ,
8383 defaultMaxAge time.Duration ,
8484) * Cache [T ] {
@@ -88,6 +88,7 @@ func newCache[T any](
8888 fetchAll : fetchAll ,
8989 getID : getID ,
9090 getName : getName ,
91+ metric : metric ,
9192
9293 defaultMode : defaultMode ,
9394 defaultMaxAge : defaultMaxAge ,
@@ -140,12 +141,12 @@ func (c *Cache[T]) All(ctx context.Context, opts ...RefreshOption) ([]*T, error)
140141 }
141142
142143 if now .Sub (refreshedAllAt ) > refreshOpts .maxAge {
143- metrics . CacheRequests .WithLabelValues (subsystem , string (refreshOpts .mode ), "miss" ).Inc ()
144+ c . metric .WithLabelValues (subsystem , string (refreshOpts .mode ), "miss" ).Inc ()
144145 if err := c .refreshAll (ctx ); err != nil {
145146 return nil , err
146147 }
147148 } else {
148- metrics . CacheRequests .WithLabelValues (subsystem , string (refreshOpts .mode ), "hit" ).Inc ()
149+ c . metric .WithLabelValues (subsystem , string (refreshOpts .mode ), "hit" ).Inc ()
149150 }
150151
151152 values := make ([]* T , 0 , len (c .byID ))
@@ -166,7 +167,7 @@ func (c *Cache[T]) getFromCache(
166167 refreshOpts := newCacheRefreshOpts (c , opts ... )
167168
168169 if refreshOpts .mode == ModeOff {
169- metrics . CacheRequests .WithLabelValues (subsystem , string (refreshOpts .mode ), "miss" ).Inc ()
170+ c . metric .WithLabelValues (subsystem , string (refreshOpts .mode ), "miss" ).Inc ()
170171 klog .V (4 ).InfoS ("cache mode is off: fetching entry from api" , "subsystem" , subsystem )
171172 return fetch ()
172173 }
@@ -177,7 +178,7 @@ func (c *Cache[T]) getFromCache(
177178 now := time .Now ()
178179
179180 if e := lookup (); e != nil && now .Sub (e .refreshedAt ) <= refreshOpts .maxAge {
180- metrics . CacheRequests .WithLabelValues (subsystem , string (refreshOpts .mode ), "hit" ).Inc ()
181+ c . metric .WithLabelValues (subsystem , string (refreshOpts .mode ), "hit" ).Inc ()
181182 klog .V (4 ).InfoS (
182183 "cache hit" ,
183184 "subsystem" , subsystem ,
@@ -201,7 +202,7 @@ func (c *Cache[T]) getFromCache(
201202 // Handled above through early return
202203 }
203204
204- metrics . CacheRequests .WithLabelValues (subsystem , string (refreshOpts .mode ), "miss" ).Inc ()
205+ c . metric .WithLabelValues (subsystem , string (refreshOpts .mode ), "miss" ).Inc ()
205206
206207 // When the value is not found in the API, the entry is not removed from the cache.
207208 // Expired entries are only evicted after an hour and when a value is found.
0 commit comments