AuthZ-Service: Add traces to cache (#105718)
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user