github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/image/save/distributionpkg/proxy/proxymetrics.go (about)

     1  // Copyright © 2021 https://github.com/distribution/distribution
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package proxy
    16  
    17  import (
    18  	"expvar"
    19  	"sync/atomic"
    20  )
    21  
    22  // Metrics is used to hold metric counters
    23  // related to the proxy
    24  type Metrics struct {
    25  	Requests    uint64
    26  	Hits        uint64
    27  	Misses      uint64
    28  	BytesPulled uint64
    29  	BytesPushed uint64
    30  }
    31  
    32  type proxyMetricsCollector struct {
    33  	blobMetrics     Metrics
    34  	manifestMetrics Metrics
    35  }
    36  
    37  // BlobPull tracks metrics about blobs pulled into the cache
    38  func (pmc *proxyMetricsCollector) BlobPull(bytesPulled uint64) {
    39  	atomic.AddUint64(&pmc.blobMetrics.Misses, 1)
    40  	atomic.AddUint64(&pmc.blobMetrics.BytesPulled, bytesPulled)
    41  }
    42  
    43  // BlobPush tracks metrics about blobs pushed to clients
    44  func (pmc *proxyMetricsCollector) BlobPush(bytesPushed uint64) {
    45  	atomic.AddUint64(&pmc.blobMetrics.Requests, 1)
    46  	atomic.AddUint64(&pmc.blobMetrics.Hits, 1)
    47  	atomic.AddUint64(&pmc.blobMetrics.BytesPushed, bytesPushed)
    48  }
    49  
    50  // ManifestPull tracks metrics related to Manifests pulled into the cache
    51  func (pmc *proxyMetricsCollector) ManifestPull(bytesPulled uint64) {
    52  	atomic.AddUint64(&pmc.manifestMetrics.Misses, 1)
    53  	atomic.AddUint64(&pmc.manifestMetrics.BytesPulled, bytesPulled)
    54  }
    55  
    56  // ManifestPush tracks metrics about manifests pushed to clients
    57  func (pmc *proxyMetricsCollector) ManifestPush(bytesPushed uint64) {
    58  	atomic.AddUint64(&pmc.manifestMetrics.Requests, 1)
    59  	atomic.AddUint64(&pmc.manifestMetrics.Hits, 1)
    60  	atomic.AddUint64(&pmc.manifestMetrics.BytesPushed, bytesPushed)
    61  }
    62  
    63  // proxyMetrics tracks metrics about the proxy cache.  This is
    64  // kept globally and made available via expvar.
    65  var proxyMetrics = &proxyMetricsCollector{}
    66  
    67  func init() {
    68  	registry := expvar.Get("registry")
    69  	if registry == nil {
    70  		registry = expvar.NewMap("registry")
    71  	}
    72  
    73  	pm := registry.(*expvar.Map).Get("proxy")
    74  	if pm == nil {
    75  		pm = &expvar.Map{}
    76  		pm.(*expvar.Map).Init()
    77  		registry.(*expvar.Map).Set("proxy", pm)
    78  	}
    79  
    80  	pm.(*expvar.Map).Set("blobs", expvar.Func(func() interface{} {
    81  		return proxyMetrics.blobMetrics
    82  	}))
    83  
    84  	pm.(*expvar.Map).Set("manifests", expvar.Func(func() interface{} {
    85  		return proxyMetrics.manifestMetrics
    86  	}))
    87  }