...

Source file src/github.com/docker/distribution/registry/storage/blobcachemetrics.go

Documentation: github.com/docker/distribution/registry/storage

     1  package storage
     2  
     3  import (
     4  	"context"
     5  	"expvar"
     6  	"sync/atomic"
     7  
     8  	dcontext "github.com/docker/distribution/context"
     9  	"github.com/docker/distribution/registry/storage/cache"
    10  )
    11  
    12  type blobStatCollector struct {
    13  	metrics cache.Metrics
    14  }
    15  
    16  func (bsc *blobStatCollector) Hit() {
    17  	atomic.AddUint64(&bsc.metrics.Requests, 1)
    18  	atomic.AddUint64(&bsc.metrics.Hits, 1)
    19  }
    20  
    21  func (bsc *blobStatCollector) Miss() {
    22  	atomic.AddUint64(&bsc.metrics.Requests, 1)
    23  	atomic.AddUint64(&bsc.metrics.Misses, 1)
    24  }
    25  
    26  func (bsc *blobStatCollector) Metrics() cache.Metrics {
    27  	return bsc.metrics
    28  }
    29  
    30  func (bsc *blobStatCollector) Logger(ctx context.Context) cache.Logger {
    31  	return dcontext.GetLogger(ctx)
    32  }
    33  
    34  // blobStatterCacheMetrics keeps track of cache metrics for blob descriptor
    35  // cache requests. Note this is kept globally and made available via expvar.
    36  // For more detailed metrics, its recommend to instrument a particular cache
    37  // implementation.
    38  var blobStatterCacheMetrics cache.MetricsTracker = &blobStatCollector{}
    39  
    40  func init() {
    41  	registry := expvar.Get("registry")
    42  	if registry == nil {
    43  		registry = expvar.NewMap("registry")
    44  	}
    45  
    46  	cache := registry.(*expvar.Map).Get("cache")
    47  	if cache == nil {
    48  		cache = &expvar.Map{}
    49  		cache.(*expvar.Map).Init()
    50  		registry.(*expvar.Map).Set("cache", cache)
    51  	}
    52  
    53  	storage := cache.(*expvar.Map).Get("storage")
    54  	if storage == nil {
    55  		storage = &expvar.Map{}
    56  		storage.(*expvar.Map).Init()
    57  		cache.(*expvar.Map).Set("storage", storage)
    58  	}
    59  
    60  	storage.(*expvar.Map).Set("blobdescriptor", expvar.Func(func() interface{} {
    61  		// no need for synchronous access: the increments are atomic and
    62  		// during reading, we don't care if the data is up to date. The
    63  		// numbers will always *eventually* be reported correctly.
    64  		return blobStatterCacheMetrics
    65  	}))
    66  }
    67  

View as plain text