From 039554da6f982e5eda1a37d0410d24e9c93634d1 Mon Sep 17 00:00:00 2001 From: hectorj-thetreep Date: Fri, 25 Jul 2025 17:02:49 +0200 Subject: [PATCH] feature(ctxcache): disable verbose logs by default --- ctxcache/cache.go | 24 ++++++++++++++++++------ ctxcache/verbose_logs.go | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 ctxcache/verbose_logs.go diff --git a/ctxcache/cache.go b/ctxcache/cache.go index 664b9c1..7ed7867 100644 --- a/ctxcache/cache.go +++ b/ctxcache/cache.go @@ -27,10 +27,14 @@ func (c *contextCache[T]) get(ctx context.Context, cacheKey string, builder func c.lock.Lock() defer c.lock.Unlock() if cached, exists := c.data[cacheKey]; exists { - logger.Debug(ctx, "context cache hit") + if verboseLogsEnabled(ctx) { + logger.Debug(ctx, "context cache hit") + } return cached } - logger.Debug(ctx, "context cache miss") + if verboseLogsEnabled(ctx) { + logger.Debug(ctx, "context cache miss") + } promise := sync.OnceValues( func() (T, error) { return errtrace.Wrap2(builder()) @@ -54,7 +58,9 @@ func (c *contextCache[T]) ctxWithAttributes(ctx context.Context, cacheKey string func ContextWithCache[T any](ctx context.Context) context.Context { cache, hasCache := ctx.Value(contextCacheCtxKey[T]{}).(*contextCache[T]) if hasCache { - logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache already exists") + if verboseLogsEnabled(ctx) { + logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache already exists") + } return ctx } cache = &contextCache[T]{ @@ -67,7 +73,9 @@ func ContextWithCache[T any](ctx context.Context) context.Context { func GetFromContextCache[T any](ctx context.Context, cacheKey string, builder func() (T, error)) (T, error) { cache, hasCache := ctx.Value(contextCacheCtxKey[T]{}).(*contextCache[T]) if !hasCache { - logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache does not exists") + if verboseLogsEnabled(ctx) { + logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache does not exists") + } return errtrace.Wrap2(builder()) } @@ -80,13 +88,17 @@ func GetFromContextCache[T any](ctx context.Context, cacheKey string, builder fu func PutInContextCache[T any](ctx context.Context, cacheKey string, value T) { cache, hasCache := ctx.Value(contextCacheCtxKey[T]{}).(*contextCache[T]) if !hasCache { - logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache does not exists") + if verboseLogsEnabled(ctx) { + logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache does not exists") + } return } ctx = cache.ctxWithAttributes(ctx, cacheKey) cache.lock.Lock() defer cache.lock.Unlock() - logger.Debug(ctx, "context cache put") + if verboseLogsEnabled(ctx) { + logger.Debug(ctx, "context cache put") + } cache.data[cacheKey] = func() (T, error) { //nolint:unparam // matches the cache signature return value, nil } diff --git a/ctxcache/verbose_logs.go b/ctxcache/verbose_logs.go new file mode 100644 index 0000000..8f1a008 --- /dev/null +++ b/ctxcache/verbose_logs.go @@ -0,0 +1,18 @@ +package ctxcache + +import "context" + +type verboseLogsCtxKey struct{} + +func CtxWithVerboseLogs(ctx context.Context) context.Context { + return context.WithValue(ctx, verboseLogsCtxKey{}, true) +} + +func CtxWithoutVerboseLogs(ctx context.Context) context.Context { + return context.WithValue(ctx, verboseLogsCtxKey{}, false) +} + +func verboseLogsEnabled(ctx context.Context) bool { + enabled, _ := ctx.Value(verboseLogsCtxKey{}).(bool) + return enabled +}