-
Notifications
You must be signed in to change notification settings - Fork 88
/
metrics_blob_access.go
181 lines (160 loc) · 7.39 KB
/
metrics_blob_access.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package blobstore
import (
"context"
"sync"
"time"
remoteexecution "github.com/bazelbuild/remote-apis/build/bazel/remote/execution/v2"
"github.com/buildbarn/bb-storage/pkg/blobstore/buffer"
"github.com/buildbarn/bb-storage/pkg/blobstore/slicing"
"github.com/buildbarn/bb-storage/pkg/clock"
"github.com/buildbarn/bb-storage/pkg/digest"
"github.com/buildbarn/bb-storage/pkg/util"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
blobAccessOperationsPrometheusMetrics sync.Once
blobAccessOperationsBlobSizeBytes = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "buildbarn",
Subsystem: "blobstore",
Name: "blob_access_operations_blob_size_bytes",
Help: "Size of blobs being inserted/retrieved, in bytes.",
Buckets: prometheus.ExponentialBuckets(1.0, 2.0, 33),
},
[]string{"storage_type", "backend_type", "operation"})
blobAccessOperationsFindMissingBatchSize = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "buildbarn",
Subsystem: "blobstore",
Name: "blob_access_operations_find_missing_batch_size",
Help: "Number of digests provided to FindMissing().",
Buckets: prometheus.ExponentialBuckets(1.0, 2.0, 17),
},
[]string{"storage_type", "backend_type"})
blobAccessOperationsDurationSeconds = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "buildbarn",
Subsystem: "blobstore",
Name: "blob_access_operations_duration_seconds",
Help: "Amount of time spent per operation on blob access objects, in seconds.",
Buckets: util.DecimalExponentialBuckets(-3, 6, 2),
},
[]string{"storage_type", "backend_type", "operation", "grpc_code"})
)
type metricsBlobAccess struct {
blobAccess BlobAccess
clock clock.Clock
getBlobSizeBytes prometheus.Observer
getDurationSeconds prometheus.ObserverVec
getFromCompositeBlobSizeBytes prometheus.Observer
getFromCompositeDurationSeconds prometheus.ObserverVec
putBlobSizeBytes prometheus.Observer
putDurationSeconds prometheus.ObserverVec
findMissingBatchSize prometheus.Observer
findMissingDurationSeconds prometheus.ObserverVec
getCapabilitiesSeconds prometheus.ObserverVec
}
// NewMetricsBlobAccess creates an adapter for BlobAccess that adds
// basic instrumentation in the form of Prometheus metrics.
func NewMetricsBlobAccess(blobAccess BlobAccess, clock clock.Clock, storageType, backendType string) BlobAccess {
blobAccessOperationsPrometheusMetrics.Do(func() {
prometheus.MustRegister(blobAccessOperationsBlobSizeBytes)
prometheus.MustRegister(blobAccessOperationsFindMissingBatchSize)
prometheus.MustRegister(blobAccessOperationsDurationSeconds)
})
return &metricsBlobAccess{
blobAccess: blobAccess,
clock: clock,
getBlobSizeBytes: blobAccessOperationsBlobSizeBytes.WithLabelValues(storageType, backendType, "Get"),
getDurationSeconds: blobAccessOperationsDurationSeconds.MustCurryWith(map[string]string{"storage_type": storageType, "backend_type": backendType, "operation": "Get"}),
getFromCompositeBlobSizeBytes: blobAccessOperationsBlobSizeBytes.WithLabelValues(storageType, backendType, "GetFromComposite"),
getFromCompositeDurationSeconds: blobAccessOperationsDurationSeconds.MustCurryWith(map[string]string{"storage_type": storageType, "backend_type": backendType, "operation": "GetFromComposite"}),
putBlobSizeBytes: blobAccessOperationsBlobSizeBytes.WithLabelValues(storageType, backendType, "Put"),
putDurationSeconds: blobAccessOperationsDurationSeconds.MustCurryWith(map[string]string{"storage_type": storageType, "backend_type": backendType, "operation": "Put"}),
findMissingBatchSize: blobAccessOperationsFindMissingBatchSize.WithLabelValues(storageType, backendType),
findMissingDurationSeconds: blobAccessOperationsDurationSeconds.MustCurryWith(map[string]string{"storage_type": storageType, "backend_type": backendType, "operation": "FindMissing"}),
getCapabilitiesSeconds: blobAccessOperationsDurationSeconds.MustCurryWith(map[string]string{"storage_type": storageType, "backend_type": backendType, "operation": "GetCapabilities"}),
}
}
func (ba *metricsBlobAccess) updateDurationSeconds(vec prometheus.ObserverVec, code codes.Code, timeStart time.Time) {
vec.WithLabelValues(code.String()).Observe(ba.clock.Now().Sub(timeStart).Seconds())
}
func (ba *metricsBlobAccess) Get(ctx context.Context, digest digest.Digest) buffer.Buffer {
timeStart := ba.clock.Now()
b := buffer.WithErrorHandler(
ba.blobAccess.Get(ctx, digest),
&metricsErrorHandler{
blobAccess: ba,
timeStart: timeStart,
errorCode: codes.OK,
durationSeconds: ba.getDurationSeconds,
})
if sizeBytes, err := b.GetSizeBytes(); err == nil {
ba.getBlobSizeBytes.Observe(float64(sizeBytes))
}
return b
}
func (ba *metricsBlobAccess) GetFromComposite(ctx context.Context, parentDigest, childDigest digest.Digest, slicer slicing.BlobSlicer) buffer.Buffer {
timeStart := ba.clock.Now()
b := buffer.WithErrorHandler(
ba.blobAccess.GetFromComposite(ctx, parentDigest, childDigest, slicer),
&metricsErrorHandler{
blobAccess: ba,
timeStart: timeStart,
errorCode: codes.OK,
durationSeconds: ba.getFromCompositeDurationSeconds,
})
if sizeBytes, err := b.GetSizeBytes(); err == nil {
ba.getFromCompositeBlobSizeBytes.Observe(float64(sizeBytes))
}
return b
}
func (ba *metricsBlobAccess) Put(ctx context.Context, digest digest.Digest, b buffer.Buffer) error {
// If the Buffer is in a known error state, return the error
// here instead of propagating the error to the underlying
// BlobAccess. Such a Put() call wouldn't have any effect.
sizeBytes, err := b.GetSizeBytes()
if err != nil {
b.Discard()
return err
}
ba.putBlobSizeBytes.Observe(float64(sizeBytes))
timeStart := ba.clock.Now()
err = ba.blobAccess.Put(ctx, digest, b)
ba.updateDurationSeconds(ba.putDurationSeconds, status.Code(err), timeStart)
return err
}
func (ba *metricsBlobAccess) FindMissing(ctx context.Context, digests digest.Set) (digest.Set, error) {
// Discard zero-sized FindMissing() requests. These may, for
// example, be generated by SizeDistinguishingBlobAccess. Such
// calls would skew the batch size and duration metrics.
if digests.Empty() {
return digest.EmptySet, nil
}
ba.findMissingBatchSize.Observe(float64(digests.Length()))
timeStart := ba.clock.Now()
digests, err := ba.blobAccess.FindMissing(ctx, digests)
ba.updateDurationSeconds(ba.findMissingDurationSeconds, status.Code(err), timeStart)
return digests, err
}
func (ba *metricsBlobAccess) GetCapabilities(ctx context.Context, instanceName digest.InstanceName) (*remoteexecution.ServerCapabilities, error) {
timeStart := ba.clock.Now()
capabilities, err := ba.blobAccess.GetCapabilities(ctx, instanceName)
ba.updateDurationSeconds(ba.getCapabilitiesSeconds, status.Code(err), timeStart)
return capabilities, err
}
type metricsErrorHandler struct {
blobAccess *metricsBlobAccess
timeStart time.Time
errorCode codes.Code
durationSeconds prometheus.ObserverVec
}
func (eh *metricsErrorHandler) OnError(err error) (buffer.Buffer, error) {
eh.errorCode = status.Code(err)
return nil, err
}
func (eh *metricsErrorHandler) Done() {
eh.blobAccess.updateDurationSeconds(eh.durationSeconds, eh.errorCode, eh.timeStart)
}