/
instrumentation.go
75 lines (62 loc) · 2.58 KB
/
instrumentation.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
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/querier/queryrange/instrumentation.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
package querymiddleware
import (
"context"
"time"
"github.com/grafana/dskit/instrument"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
// newInstrumentMiddleware can be inserted into the middleware chain to expose timing information.
func newInstrumentMiddleware(name string, metrics *instrumentMiddlewareMetrics) MetricsQueryMiddleware {
var durationCol instrument.Collector
// Support the case metrics shouldn't be tracked (ie. unit tests).
if metrics != nil {
durationCol = instrument.NewHistogramCollector(metrics.duration)
} else {
durationCol = &noopCollector{}
}
return MetricsQueryMiddlewareFunc(func(next MetricsQueryHandler) MetricsQueryHandler {
return HandlerFunc(func(ctx context.Context, req MetricsQueryRequest) (Response, error) {
var resp Response
err := instrument.CollectedRequest(ctx, name, durationCol, instrument.ErrorCode, func(ctx context.Context) error {
sp := opentracing.SpanFromContext(ctx)
if sp != nil {
req.AddSpanTags(sp)
}
var err error
resp, err = next.Do(ctx, req)
return err
})
return resp, err
})
})
}
// instrumentMiddlewareMetrics holds the metrics tracked by newInstrumentMiddleware.
type instrumentMiddlewareMetrics struct {
duration *prometheus.HistogramVec
}
// newInstrumentMiddlewareMetrics makes a new instrumentMiddlewareMetrics.
func newInstrumentMiddlewareMetrics(registerer prometheus.Registerer) *instrumentMiddlewareMetrics {
return &instrumentMiddlewareMetrics{
duration: promauto.With(registerer).NewHistogramVec(prometheus.HistogramOpts{
Namespace: "cortex",
Name: "frontend_query_range_duration_seconds",
Help: "Total time spent in seconds doing query range requests.",
Buckets: prometheus.DefBuckets,
}, []string{"method", "status_code"}),
}
}
// noopCollector is a noop collector that can be used as placeholder when no metric
// should tracked by the instrumentation.
type noopCollector struct{}
// Register implements instrument.Collector.
func (c *noopCollector) Register() {}
// Before implements instrument.Collector.
func (c *noopCollector) Before(context.Context, string, time.Time) {}
// After implements instrument.Collector.
func (c *noopCollector) After(context.Context, string, string, time.Time) {}