github.com/mika/distribution@v2.2.2-0.20160108133430-a75790e3d8e0+incompatible/registry/storage/blobcachemetrics.go (about)

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