Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/registry-facade/pkg/registry/metrics.go
2499 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package registry
6
7
import (
8
"net/http"
9
"strings"
10
"time"
11
12
"github.com/prometheus/client_golang/prometheus"
13
)
14
15
// NewMeasuringRegistryRoundTripper produces a round tripper that exposes registry access metrics
16
func NewMeasuringRegistryRoundTripper(delegate http.RoundTripper, reg prometheus.Registerer) (http.RoundTripper, error) {
17
metrics, err := newMetrics(reg, false)
18
if err != nil {
19
return nil, err
20
}
21
return &measuringRegistryRoundTripper{
22
delegate: delegate,
23
metrics: metrics,
24
}, nil
25
}
26
27
type measuringRegistryRoundTripper struct {
28
delegate http.RoundTripper
29
metrics *metrics
30
}
31
32
func (m *measuringRegistryRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
33
t0 := time.Now()
34
resp, err := m.delegate.RoundTrip(req)
35
dt := time.Since(t0)
36
37
if strings.Contains(req.URL.Path, "/manifests/") {
38
m.metrics.ManifestHist.Observe(dt.Seconds())
39
if err != nil {
40
m.metrics.ReqFailedCounter.WithLabelValues("manifest").Inc()
41
}
42
} else if strings.Contains(req.URL.Path, "/blobs/") {
43
m.metrics.BlobCounter.Inc()
44
if err != nil {
45
m.metrics.ReqFailedCounter.WithLabelValues("blob").Inc()
46
}
47
}
48
49
return resp, err
50
}
51
52
// Metrics combine custom metrics exported by registry facade
53
type metrics struct {
54
ManifestHist prometheus.Histogram
55
ReqFailedCounter *prometheus.CounterVec
56
BlobCounter prometheus.Counter
57
BlobDownloadSizeCounter *prometheus.CounterVec
58
BlobDownloadCounter *prometheus.CounterVec
59
BlobDownloadSpeedHist *prometheus.HistogramVec
60
}
61
62
func newMetrics(reg prometheus.Registerer, upstream bool) (*metrics, error) {
63
manifestHist := prometheus.NewHistogram(prometheus.HistogramOpts{
64
Name: "manifest_req_seconds",
65
Help: "time of manifest requests made to the downstream registry",
66
Buckets: []float64{0.1, 0.5, 1, 2, 5, 10, 60, 300, 600, 1800},
67
})
68
err := reg.Register(manifestHist)
69
if err != nil {
70
return nil, err
71
}
72
73
reqFailedCounter := prometheus.NewCounterVec(
74
prometheus.CounterOpts{
75
Name: "req_failed_total",
76
Help: "number of requests that failed",
77
}, []string{"type"},
78
)
79
err = reg.Register(reqFailedCounter)
80
if err != nil {
81
return nil, err
82
}
83
84
blobCounter := prometheus.NewCounter(prometheus.CounterOpts{
85
Name: "blob_req_total",
86
Help: "number of blob requests made to the downstream registry",
87
})
88
err = reg.Register(blobCounter)
89
if err != nil {
90
return nil, err
91
}
92
93
blobDownloadCounter := prometheus.NewCounterVec(prometheus.CounterOpts{
94
Name: "blob_req_dl_total",
95
Help: "number of blob download requests",
96
}, []string{"blobSource", "ok"})
97
err = reg.Register(blobDownloadCounter)
98
if err != nil {
99
return nil, err
100
}
101
102
blobDownloadSizeCounter := prometheus.NewCounterVec(prometheus.CounterOpts{
103
Name: "blob_req_bytes_total",
104
Help: "amount of blob bytes downloaded",
105
}, []string{"blobSource"})
106
err = reg.Register(blobDownloadSizeCounter)
107
if err != nil {
108
return nil, err
109
}
110
111
blobDownloadSpeedHist := prometheus.NewHistogramVec(prometheus.HistogramOpts{
112
Name: "blob_req_bytes_second",
113
Help: "blob download speed in bytes per second",
114
Buckets: prometheus.ExponentialBuckets(1024*1024, 2, 15),
115
}, []string{"blobSource"})
116
if upstream {
117
err = reg.Register(blobDownloadSpeedHist)
118
if err != nil {
119
return nil, err
120
}
121
}
122
123
return &metrics{
124
ManifestHist: manifestHist,
125
ReqFailedCounter: reqFailedCounter,
126
BlobCounter: blobCounter,
127
BlobDownloadSpeedHist: blobDownloadSpeedHist,
128
BlobDownloadSizeCounter: blobDownloadSizeCounter,
129
BlobDownloadCounter: blobDownloadCounter,
130
}, nil
131
}
132
133