feat: trace span for binary marshal and unmarshal operations (#9740)

This commit is contained in:
Karan Balani
2025-12-02 16:13:11 +05:30
committed by GitHub
parent 3db0e1f66a
commit bc1295b93a

View File

@@ -93,7 +93,7 @@ func New(ctx context.Context, settings factory.ProviderSettings, config cache.Co
}
func (provider *provider) Set(ctx context.Context, orgID valuer.UUID, cacheKey string, data cachetypes.Cacheable, ttl time.Duration) error {
_, span := provider.settings.Tracer().Start(ctx, "memory.set", trace.WithAttributes(
ctx, span := provider.settings.Tracer().Start(ctx, "memory.set", trace.WithAttributes(
attribute.String(semconv.AttributeDBSystem, "memory"),
attribute.String(semconv.AttributeDBStatement, "set "+strings.Join([]string{orgID.StringValue(), cacheKey}, "::")),
attribute.String(semconv.AttributeDBOperation, "SET"),
@@ -113,12 +113,12 @@ func (provider *provider) Set(ctx context.Context, orgID valuer.UUID, cacheKey s
if ok := provider.cc.SetWithTTL(strings.Join([]string{orgID.StringValue(), cacheKey}, "::"), toCache, 1, ttl); !ok {
return errors.New(errors.TypeInternal, errors.CodeInternal, "error writing to cache")
}
provider.cc.Wait()
return nil
}
toCache, err := data.MarshalBinary()
toCache, err := provider.marshalBinary(ctx, data)
cost := int64(len(toCache))
if err != nil {
return err
@@ -136,7 +136,7 @@ func (provider *provider) Set(ctx context.Context, orgID valuer.UUID, cacheKey s
}
func (provider *provider) Get(ctx context.Context, orgID valuer.UUID, cacheKey string, dest cachetypes.Cacheable) error {
_, span := provider.settings.Tracer().Start(ctx, "memory.get", trace.WithAttributes(
ctx, span := provider.settings.Tracer().Start(ctx, "memory.get", trace.WithAttributes(
attribute.String(semconv.AttributeDBSystem, "memory"),
attribute.String(semconv.AttributeDBStatement, "get "+strings.Join([]string{orgID.StringValue(), cacheKey}, "::")),
attribute.String(semconv.AttributeDBOperation, "GET"),
@@ -176,7 +176,7 @@ func (provider *provider) Get(ctx context.Context, orgID valuer.UUID, cacheKey s
if fromCache, ok := cachedData.([]byte); ok {
span.SetAttributes(attribute.Bool("memory.cloneable", false))
if err = dest.UnmarshalBinary(fromCache); err != nil {
if err = provider.unmarshalBinary(ctx, dest, fromCache); err != nil {
return err
}
@@ -202,3 +202,19 @@ func (provider *provider) DeleteMany(_ context.Context, orgID valuer.UUID, cache
provider.cc.Del(strings.Join([]string{orgID.StringValue(), cacheKey}, "::"))
}
}
func (provider *provider) marshalBinary(ctx context.Context, toMarshal cachetypes.Cacheable) ([]byte, error) {
_, span := provider.settings.Tracer().Start(ctx, "binary.Marshal", trace.WithAttributes(
attribute.String(semconv.AttributeDBSystem, "memory"),
))
defer span.End()
return toMarshal.MarshalBinary()
}
func (provider *provider) unmarshalBinary(ctx context.Context, dest cachetypes.Cacheable, fromCache []byte) error {
_, span := provider.settings.Tracer().Start(ctx, "binary.Unmarshal", trace.WithAttributes(
attribute.String(semconv.AttributeDBSystem, "memory"),
))
defer span.End()
return dest.UnmarshalBinary(fromCache)
}