File: blobcachemetrics.go

package info (click to toggle)
docker-registry 2.8.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,148 kB
  • sloc: sh: 331; makefile: 82
file content (66 lines) | stat: -rw-r--r-- 1,763 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package storage

import (
	"context"
	"expvar"
	"sync/atomic"

	dcontext "github.com/docker/distribution/context"
	"github.com/docker/distribution/registry/storage/cache"
)

type blobStatCollector struct {
	metrics cache.Metrics
}

func (bsc *blobStatCollector) Hit() {
	atomic.AddUint64(&bsc.metrics.Requests, 1)
	atomic.AddUint64(&bsc.metrics.Hits, 1)
}

func (bsc *blobStatCollector) Miss() {
	atomic.AddUint64(&bsc.metrics.Requests, 1)
	atomic.AddUint64(&bsc.metrics.Misses, 1)
}

func (bsc *blobStatCollector) Metrics() cache.Metrics {
	return bsc.metrics
}

func (bsc *blobStatCollector) Logger(ctx context.Context) cache.Logger {
	return dcontext.GetLogger(ctx)
}

// blobStatterCacheMetrics keeps track of cache metrics for blob descriptor
// cache requests. Note this is kept globally and made available via expvar.
// For more detailed metrics, its recommend to instrument a particular cache
// implementation.
var blobStatterCacheMetrics cache.MetricsTracker = &blobStatCollector{}

func init() {
	registry := expvar.Get("registry")
	if registry == nil {
		registry = expvar.NewMap("registry")
	}

	cache := registry.(*expvar.Map).Get("cache")
	if cache == nil {
		cache = &expvar.Map{}
		cache.(*expvar.Map).Init()
		registry.(*expvar.Map).Set("cache", cache)
	}

	storage := cache.(*expvar.Map).Get("storage")
	if storage == nil {
		storage = &expvar.Map{}
		storage.(*expvar.Map).Init()
		cache.(*expvar.Map).Set("storage", storage)
	}

	storage.(*expvar.Map).Set("blobdescriptor", expvar.Func(func() interface{} {
		// no need for synchronous access: the increments are atomic and
		// during reading, we don't care if the data is up to date. The
		// numbers will always *eventually* be reported correctly.
		return blobStatterCacheMetrics
	}))
}