generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 95
/
metrics.go
309 lines (263 loc) · 10.7 KB
/
metrics.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package gateway
import (
"context"
"io"
"time"
"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/path"
"github.com/ipfs/go-cid"
prometheus "github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
// Duration histograms measure things like API call execution, how long returning specific
// CID/path, how long CAR fetch form backend took, etc.
// We use fixed definition here, as we don't want to break existing buckets if we need to add more.
var defaultDurationHistogramBuckets = []float64{0.05, 0.1, 0.25, 0.5, 1, 2, 5, 10, 30, 60, 120, 240, 480, 960, 1920}
type ipfsBackendWithMetrics struct {
backend IPFSBackend
backendCallMetric *prometheus.HistogramVec
}
func newIPFSBackendWithMetrics(backend IPFSBackend) *ipfsBackendWithMetrics {
// We can add buckets as a parameter in the future, but for now using static defaults
// suggested in https://github.com/ipfs/kubo/issues/8441
backendCallMetric := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "ipfs",
Subsystem: "gw_backend",
Name: "api_call_duration_seconds",
Help: "The time spent in IPFSBackend API calls that returned success.",
Buckets: defaultDurationHistogramBuckets,
},
[]string{"name", "result"},
)
if err := prometheus.Register(backendCallMetric); err != nil {
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
backendCallMetric = are.ExistingCollector.(*prometheus.HistogramVec)
} else {
log.Errorf("failed to register ipfs_gw_backend_api_call_duration_seconds: %v", err)
}
}
return &ipfsBackendWithMetrics{backend, backendCallMetric}
}
func (b *ipfsBackendWithMetrics) updateBackendCallMetric(name string, err error, begin time.Time) {
end := time.Since(begin).Seconds()
if err == nil {
b.backendCallMetric.WithLabelValues(name, "success").Observe(end)
} else {
b.backendCallMetric.WithLabelValues(name, "failure").Observe(end)
}
}
func (b *ipfsBackendWithMetrics) Get(ctx context.Context, path path.ImmutablePath, ranges ...ByteRange) (ContentPathMetadata, *GetResponse, error) {
begin := time.Now()
name := "IPFSBackend.Get"
ctx, span := spanTrace(ctx, name, trace.WithAttributes(attribute.String("path", path.String()), attribute.Int("ranges", len(ranges))))
defer span.End()
md, f, err := b.backend.Get(ctx, path, ranges...)
b.updateBackendCallMetric(name, err, begin)
return md, f, err
}
func (b *ipfsBackendWithMetrics) GetAll(ctx context.Context, path path.ImmutablePath) (ContentPathMetadata, files.Node, error) {
begin := time.Now()
name := "IPFSBackend.GetAll"
ctx, span := spanTrace(ctx, name, trace.WithAttributes(attribute.String("path", path.String())))
defer span.End()
md, n, err := b.backend.GetAll(ctx, path)
b.updateBackendCallMetric(name, err, begin)
return md, n, err
}
func (b *ipfsBackendWithMetrics) GetBlock(ctx context.Context, path path.ImmutablePath) (ContentPathMetadata, files.File, error) {
begin := time.Now()
name := "IPFSBackend.GetBlock"
ctx, span := spanTrace(ctx, name, trace.WithAttributes(attribute.String("path", path.String())))
defer span.End()
md, n, err := b.backend.GetBlock(ctx, path)
b.updateBackendCallMetric(name, err, begin)
return md, n, err
}
func (b *ipfsBackendWithMetrics) Head(ctx context.Context, path path.ImmutablePath) (ContentPathMetadata, *HeadResponse, error) {
begin := time.Now()
name := "IPFSBackend.Head"
ctx, span := spanTrace(ctx, name, trace.WithAttributes(attribute.String("path", path.String())))
defer span.End()
md, n, err := b.backend.Head(ctx, path)
b.updateBackendCallMetric(name, err, begin)
return md, n, err
}
func (b *ipfsBackendWithMetrics) ResolvePath(ctx context.Context, path path.ImmutablePath) (ContentPathMetadata, error) {
begin := time.Now()
name := "IPFSBackend.ResolvePath"
ctx, span := spanTrace(ctx, name, trace.WithAttributes(attribute.String("path", path.String())))
defer span.End()
md, err := b.backend.ResolvePath(ctx, path)
b.updateBackendCallMetric(name, err, begin)
return md, err
}
func (b *ipfsBackendWithMetrics) GetCAR(ctx context.Context, path path.ImmutablePath, params CarParams) (ContentPathMetadata, io.ReadCloser, error) {
begin := time.Now()
name := "IPFSBackend.GetCAR"
ctx, span := spanTrace(ctx, name, trace.WithAttributes(attribute.String("path", path.String())))
defer span.End()
md, rc, err := b.backend.GetCAR(ctx, path, params)
b.updateBackendCallMetric(name, err, begin)
return md, rc, err
}
func (b *ipfsBackendWithMetrics) IsCached(ctx context.Context, path path.Path) bool {
begin := time.Now()
name := "IPFSBackend.IsCached"
ctx, span := spanTrace(ctx, name, trace.WithAttributes(attribute.String("path", path.String())))
defer span.End()
bln := b.backend.IsCached(ctx, path)
b.updateBackendCallMetric(name, nil, begin)
return bln
}
func (b *ipfsBackendWithMetrics) GetIPNSRecord(ctx context.Context, cid cid.Cid) ([]byte, error) {
begin := time.Now()
name := "IPFSBackend.GetIPNSRecord"
ctx, span := spanTrace(ctx, name, trace.WithAttributes(attribute.String("cid", cid.String())))
defer span.End()
r, err := b.backend.GetIPNSRecord(ctx, cid)
b.updateBackendCallMetric(name, err, begin)
return r, err
}
func (b *ipfsBackendWithMetrics) ResolveMutable(ctx context.Context, path path.Path) (path.ImmutablePath, time.Duration, time.Time, error) {
begin := time.Now()
name := "IPFSBackend.ResolveMutable"
ctx, span := spanTrace(ctx, name, trace.WithAttributes(attribute.String("path", path.String())))
defer span.End()
p, ttl, lastMod, err := b.backend.ResolveMutable(ctx, path)
b.updateBackendCallMetric(name, err, begin)
return p, ttl, lastMod, err
}
func (b *ipfsBackendWithMetrics) GetDNSLinkRecord(ctx context.Context, fqdn string) (path.Path, error) {
begin := time.Now()
name := "IPFSBackend.GetDNSLinkRecord"
ctx, span := spanTrace(ctx, name, trace.WithAttributes(attribute.String("fqdn", fqdn)))
defer span.End()
p, err := b.backend.GetDNSLinkRecord(ctx, fqdn)
b.updateBackendCallMetric(name, err, begin)
return p, err
}
var _ IPFSBackend = (*ipfsBackendWithMetrics)(nil)
var _ WithContextHint = (*ipfsBackendWithMetrics)(nil)
func (b *ipfsBackendWithMetrics) WrapContextForRequest(ctx context.Context) context.Context {
if withCtxWrap, ok := b.backend.(WithContextHint); ok {
return withCtxWrap.WrapContextForRequest(ctx)
}
return ctx
}
func newHandlerWithMetrics(c *Config, backend IPFSBackend) *handler {
i := &handler{
config: c,
backend: newIPFSBackendWithMetrics(backend),
// Response-type specific metrics
// ----------------------------
requestTypeMetric: newRequestTypeMetric(
"gw_request_types",
"The number of requests per implicit or explicit request type.",
),
// Generic: time it takes to execute a successful gateway request (all request types)
getMetric: newHistogramMetric(
"gw_get_duration_seconds",
"The time to GET a successful response to a request (all content types).",
),
// UnixFS: time it takes to return a file
unixfsFileGetMetric: newHistogramMetric(
"gw_unixfs_file_get_duration_seconds",
"The time to serve an entire UnixFS file from the gateway.",
),
// UnixFS: time it takes to find and serve an index.html file on behalf of a directory.
unixfsDirIndexGetMetric: newHistogramMetric(
"gw_unixfs_dir_indexhtml_get_duration_seconds",
"The time to serve an index.html file on behalf of a directory from the gateway. This is a subset of gw_unixfs_file_get_duration_seconds.",
),
// UnixFS: time it takes to generate static HTML with directory listing
unixfsGenDirListingGetMetric: newHistogramMetric(
"gw_unixfs_gen_dir_listing_get_duration_seconds",
"The time to serve a generated UnixFS HTML directory listing from the gateway.",
),
// CAR: time it takes to return requested CAR stream
carStreamGetMetric: newHistogramMetric(
"gw_car_stream_get_duration_seconds",
"The time to GET an entire CAR stream from the gateway.",
),
carStreamFailMetric: newHistogramMetric(
"gw_car_stream_fail_duration_seconds",
"How long a CAR was streamed before failing mid-stream.",
),
// Block: time it takes to return requested Block
rawBlockGetMetric: newHistogramMetric(
"gw_raw_block_get_duration_seconds",
"The time to GET an entire raw Block from the gateway.",
),
// TAR: time it takes to return requested TAR stream
tarStreamGetMetric: newHistogramMetric(
"gw_tar_stream_get_duration_seconds",
"The time to GET an entire TAR stream from the gateway.",
),
// TAR: time it takes to return requested TAR stream
tarStreamFailMetric: newHistogramMetric(
"gw_tar_stream_fail_duration_seconds",
"How long a TAR was streamed before failing mid-stream.",
),
// JSON/CBOR: time it takes to return requested DAG-JSON/-CBOR document
jsoncborDocumentGetMetric: newHistogramMetric(
"gw_jsoncbor_get_duration_seconds",
"The time to GET an entire DAG-JSON/CBOR block from the gateway.",
),
// IPNS Record: time it takes to return IPNS record
ipnsRecordGetMetric: newHistogramMetric(
"gw_ipns_record_get_duration_seconds",
"The time to GET an entire IPNS Record from the gateway.",
),
}
return i
}
func newRequestTypeMetric(name string, help string) *prometheus.CounterVec {
// We can add buckets as a parameter in the future, but for now using static defaults
// suggested in https://github.com/ipfs/kubo/issues/8441
metric := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "ipfs",
Subsystem: "http",
Name: name,
Help: help,
},
[]string{"gateway", "type"},
)
if err := prometheus.Register(metric); err != nil {
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
metric = are.ExistingCollector.(*prometheus.CounterVec)
} else {
log.Errorf("failed to register ipfs_http_%s: %v", name, err)
}
}
return metric
}
func newHistogramMetric(name string, help string) *prometheus.HistogramVec {
// We can add buckets as a parameter in the future, but for now using static defaults
// suggested in https://github.com/ipfs/kubo/issues/8441
histogramMetric := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "ipfs",
Subsystem: "http",
Name: name,
Help: help,
Buckets: defaultDurationHistogramBuckets,
},
[]string{"gateway"},
)
if err := prometheus.Register(histogramMetric); err != nil {
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
histogramMetric = are.ExistingCollector.(*prometheus.HistogramVec)
} else {
log.Errorf("failed to register ipfs_http_%s: %v", name, err)
}
}
return histogramMetric
}
var tracer = otel.Tracer("boxo/gateway")
func spanTrace(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
return tracer.Start(ctx, "Gateway."+spanName, opts...)
}