AuthZ-Service: Add traces to cache (#105718)

This commit is contained in:
Gabriel MABILLE
2025-05-21 14:35:43 +02:00
committed by GitHub
parent 8caa62ede0
commit cb3cd021b7
3 changed files with 25 additions and 15 deletions
+11 -2
View File
@@ -7,8 +7,10 @@ import (
"time"
"github.com/grafana/authlib/cache"
"go.opentelemetry.io/otel/attribute"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
)
func userIdentifierCacheKey(namespace, userUID string) string {
@@ -50,20 +52,24 @@ type cacheWrap[T any] interface {
type cacheWrapImpl[T any] struct {
cache cache.Cache
logger log.Logger
tracer tracing.Tracer
ttl time.Duration
}
// cacheWrap is a wrapper around the authlib Cache that provides typed Get and Set methods
// it handles encoding/decoding for a specific type.
func newCacheWrap[T any](cache cache.Cache, logger log.Logger, ttl time.Duration) cacheWrap[T] {
func newCacheWrap[T any](cache cache.Cache, logger log.Logger, tracer tracing.Tracer, ttl time.Duration) cacheWrap[T] {
if ttl == 0 {
logger.Info("cache ttl is 0, using noop cache")
return &noopCache[T]{}
}
return &cacheWrapImpl[T]{cache: cache, logger: logger, ttl: ttl}
return &cacheWrapImpl[T]{cache: cache, logger: logger, tracer: tracer, ttl: ttl}
}
func (c *cacheWrapImpl[T]) Get(ctx context.Context, key string) (T, bool) {
ctx, span := c.tracer.Start(ctx, "cacheWrap.Get")
defer span.End()
span.SetAttributes(attribute.Bool("hit", false))
logger := c.logger.FromContext(ctx)
var value T
@@ -81,10 +87,13 @@ func (c *cacheWrapImpl[T]) Get(ctx context.Context, key string) (T, bool) {
return value, false
}
span.SetAttributes(attribute.Bool("hit", true))
return value, true
}
func (c *cacheWrapImpl[T]) Set(ctx context.Context, key string, value T) {
ctx, span := c.tracer.Start(ctx, "cacheWrap.Set")
defer span.End()
logger := c.logger.FromContext(ctx)
data, err := json.Marshal(value)