Skip to content

Commit 92348cc

Browse files
committed
feat(cache): add logging and chainloop prefix to NATS KV buckets
Wire a logger into cache constructors so cache hits, misses, and initialization are visible. Rename NATS KV buckets to use chainloop- prefix for consistency with existing streams. Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
1 parent 9421241 commit 92348cc

2 files changed

Lines changed: 65 additions & 14 deletions

File tree

app/controlplane/cmd/wire.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,42 @@ var cacheProviderSet = wire.NewSet(
140140
newClaimsCache,
141141
)
142142

143-
func newClaimsCache(conn *nats.Conn) (cache.Cache[*jwt.MapClaims], error) {
144-
opts := []cache.Option{cache.WithTTL(10 * time.Second)}
143+
func newClaimsCache(conn *nats.Conn, logger log.Logger) (cache.Cache[*jwt.MapClaims], error) {
144+
l := log.NewHelper(logger)
145+
backend := "memory"
146+
opts := []cache.Option{cache.WithTTL(10 * time.Second), cache.WithLogger(&kratosLogAdapter{h: l})}
145147
if conn != nil {
146-
opts = append(opts, cache.WithNATS(conn, "jwt-claims"))
148+
backend = "nats"
149+
opts = append(opts, cache.WithNATS(conn, "chainloop-jwt-claims"))
147150
}
151+
l.Infow("msg", "cache initialized", "bucket", "chainloop-jwt-claims", "backend", backend, "ttl", "10s")
148152
return cache.New[*jwt.MapClaims](opts...)
149153
}
150154

151-
func newMembershipsCache(conn *nats.Conn) (cache.Cache[*entities.Membership], error) {
152-
opts := []cache.Option{cache.WithTTL(time.Second)}
155+
func newMembershipsCache(conn *nats.Conn, logger log.Logger) (cache.Cache[*entities.Membership], error) {
156+
l := log.NewHelper(logger)
157+
backend := "memory"
158+
opts := []cache.Option{cache.WithTTL(time.Second), cache.WithLogger(&kratosLogAdapter{h: l})}
153159
if conn != nil {
154-
opts = append(opts, cache.WithNATS(conn, "memberships"))
160+
backend = "nats"
161+
opts = append(opts, cache.WithNATS(conn, "chainloop-memberships"))
155162
}
163+
l.Infow("msg", "cache initialized", "bucket", "chainloop-memberships", "backend", backend, "ttl", "1s")
156164
return cache.New[*entities.Membership](opts...)
157165
}
166+
167+
// kratosLogAdapter adapts kratos log.Helper (Debugw(...interface{})) to cache.Logger (Debugw(string, ...any)).
168+
type kratosLogAdapter struct{ h *log.Helper }
169+
170+
func (a *kratosLogAdapter) Debugw(msg string, keyvals ...any) {
171+
a.h.Debugw(append([]any{"msg", msg}, keyvals...)...)
172+
}
173+
func (a *kratosLogAdapter) Infow(msg string, keyvals ...any) {
174+
a.h.Infow(append([]any{"msg", msg}, keyvals...)...)
175+
}
176+
func (a *kratosLogAdapter) Warnw(msg string, keyvals ...any) {
177+
a.h.Warnw(append([]any{"msg", msg}, keyvals...)...)
178+
}
179+
func (a *kratosLogAdapter) Errorw(msg string, keyvals ...any) {
180+
a.h.Errorw(append([]any{"msg", msg}, keyvals...)...)
181+
}

app/controlplane/cmd/wire_gen.go

Lines changed: 35 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)