Merge pull request #2897 from SuperQ/cache_metrics

Cleanup storage cache metrics
This commit is contained in:
Milos Gajdos 2023-08-15 07:44:14 +01:00 committed by GitHub
commit ff46bf1c41
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,8 +14,14 @@ type cachedBlobStatter struct {
backend distribution.BlobDescriptorService backend distribution.BlobDescriptorService
} }
// cacheCount is the number of total cache request received/hits/misses var (
var cacheCount = prometheus.StorageNamespace.NewLabeledCounter("cache", "The number of cache request received", "type") // cacheRequestCount is the number of total cache requests received.
cacheRequestCount = prometheus.StorageNamespace.NewCounter("cache_requests", "The number of cache request received")
// cacheRequestCount is the number of total cache requests received.
cacheHitCount = prometheus.StorageNamespace.NewCounter("cache_hits", "The number of cache request received")
// cacheErrorCount is the number of cache request errors.
cacheErrorCount = prometheus.StorageNamespace.NewCounter("cache_errors", "The number of cache request errors")
)
// NewCachedBlobStatter creates a new statter which prefers a cache and // NewCachedBlobStatter creates a new statter which prefers a cache and
// falls back to a backend. // falls back to a backend.
@ -27,12 +33,12 @@ func NewCachedBlobStatter(cache distribution.BlobDescriptorService, backend dist
} }
func (cbds *cachedBlobStatter) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) { func (cbds *cachedBlobStatter) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
cacheCount.WithValues("Request").Inc(1) cacheRequestCount.Inc(1)
// try getting from cache // try getting from cache
desc, cacheErr := cbds.cache.Stat(ctx, dgst) desc, cacheErr := cbds.cache.Stat(ctx, dgst)
if cacheErr == nil { if cacheErr == nil {
cacheCount.WithValues("Hit").Inc(1) cacheHitCount.Inc(1)
return desc, nil return desc, nil
} }
@ -43,8 +49,6 @@ func (cbds *cachedBlobStatter) Stat(ctx context.Context, dgst digest.Digest) (di
} }
if cacheErr == distribution.ErrBlobUnknown { if cacheErr == distribution.ErrBlobUnknown {
// cache doesn't have info. update it with info got from backend
cacheCount.WithValues("Miss").Inc(1)
if err := cbds.cache.SetDescriptor(ctx, dgst, desc); err != nil { if err := cbds.cache.SetDescriptor(ctx, dgst, desc); err != nil {
dcontext.GetLoggerWithField(ctx, "blob", dgst).WithError(err).Error("error from cache setting desc") dcontext.GetLoggerWithField(ctx, "blob", dgst).WithError(err).Error("error from cache setting desc")
} }
@ -52,7 +56,7 @@ func (cbds *cachedBlobStatter) Stat(ctx context.Context, dgst digest.Digest) (di
} else { } else {
// unknown error from cache. just log and error. do not store cache as it may be trigger many set calls // unknown error from cache. just log and error. do not store cache as it may be trigger many set calls
dcontext.GetLoggerWithField(ctx, "blob", dgst).WithError(cacheErr).Error("error from cache stat(ing) blob") dcontext.GetLoggerWithField(ctx, "blob", dgst).WithError(cacheErr).Error("error from cache stat(ing) blob")
cacheCount.WithValues("Error").Inc(1) cacheErrorCount.Inc(1)
} }
return desc, nil return desc, nil