...

Source file src/github.com/docker/distribution/registry/proxy/proxymetrics.go

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

     1  package proxy
     2  
     3  import (
     4  	"expvar"
     5  	"sync/atomic"
     6  )
     7  
     8  // Metrics is used to hold metric counters
     9  // related to the proxy
    10  type Metrics struct {
    11  	Requests    uint64
    12  	Hits        uint64
    13  	Misses      uint64
    14  	BytesPulled uint64
    15  	BytesPushed uint64
    16  }
    17  
    18  type proxyMetricsCollector struct {
    19  	blobMetrics     Metrics
    20  	manifestMetrics Metrics
    21  }
    22  
    23  // BlobPull tracks metrics about blobs pulled into the cache
    24  func (pmc *proxyMetricsCollector) BlobPull(bytesPulled uint64) {
    25  	atomic.AddUint64(&pmc.blobMetrics.Misses, 1)
    26  	atomic.AddUint64(&pmc.blobMetrics.BytesPulled, bytesPulled)
    27  }
    28  
    29  // BlobPush tracks metrics about blobs pushed to clients
    30  func (pmc *proxyMetricsCollector) BlobPush(bytesPushed uint64) {
    31  	atomic.AddUint64(&pmc.blobMetrics.Requests, 1)
    32  	atomic.AddUint64(&pmc.blobMetrics.Hits, 1)
    33  	atomic.AddUint64(&pmc.blobMetrics.BytesPushed, bytesPushed)
    34  }
    35  
    36  // ManifestPull tracks metrics related to Manifests pulled into the cache
    37  func (pmc *proxyMetricsCollector) ManifestPull(bytesPulled uint64) {
    38  	atomic.AddUint64(&pmc.manifestMetrics.Misses, 1)
    39  	atomic.AddUint64(&pmc.manifestMetrics.BytesPulled, bytesPulled)
    40  }
    41  
    42  // ManifestPush tracks metrics about manifests pushed to clients
    43  func (pmc *proxyMetricsCollector) ManifestPush(bytesPushed uint64) {
    44  	atomic.AddUint64(&pmc.manifestMetrics.Requests, 1)
    45  	atomic.AddUint64(&pmc.manifestMetrics.Hits, 1)
    46  	atomic.AddUint64(&pmc.manifestMetrics.BytesPushed, bytesPushed)
    47  }
    48  
    49  // proxyMetrics tracks metrics about the proxy cache.  This is
    50  // kept globally and made available via expvar.
    51  var proxyMetrics = &proxyMetricsCollector{}
    52  
    53  func init() {
    54  	registry := expvar.Get("registry")
    55  	if registry == nil {
    56  		registry = expvar.NewMap("registry")
    57  	}
    58  
    59  	pm := registry.(*expvar.Map).Get("proxy")
    60  	if pm == nil {
    61  		pm = &expvar.Map{}
    62  		pm.(*expvar.Map).Init()
    63  		registry.(*expvar.Map).Set("proxy", pm)
    64  	}
    65  
    66  	pm.(*expvar.Map).Set("blobs", expvar.Func(func() interface{} {
    67  		return proxyMetrics.blobMetrics
    68  	}))
    69  
    70  	pm.(*expvar.Map).Set("manifests", expvar.Func(func() interface{} {
    71  		return proxyMetrics.manifestMetrics
    72  	}))
    73  
    74  }
    75  

View as plain text