forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instrumentation.go
79 lines (68 loc) · 2.39 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
76
77
78
79
package gcp
import (
"net/http"
"strconv"
"time"
otgrpc "github.com/opentracing-contrib/go-grpc"
opentracing "github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/weaveworks/common/middleware"
"google.golang.org/api/option"
"google.golang.org/grpc"
)
var (
bigtableRequestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "loki",
Name: "bigtable_request_duration_seconds",
Help: "Time spent doing Bigtable requests.",
// Bigtable latency seems to range from a few ms to a several seconds and is
// important. So use 9 buckets from 1ms to just over 1 minute (65s).
Buckets: prometheus.ExponentialBuckets(0.001, 4, 9),
}, []string{"operation", "status_code"})
gcsRequestDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "loki",
Name: "gcs_request_duration_seconds",
Help: "Time spent doing GCS requests.",
// 6 buckets from 5ms to 20s.
Buckets: prometheus.ExponentialBuckets(0.005, 4, 7),
}, []string{"operation", "status_code"})
)
func bigtableInstrumentation() ([]grpc.UnaryClientInterceptor, []grpc.StreamClientInterceptor) {
return []grpc.UnaryClientInterceptor{
otgrpc.OpenTracingClientInterceptor(opentracing.GlobalTracer()),
middleware.UnaryClientInstrumentInterceptor(bigtableRequestDuration),
},
[]grpc.StreamClientInterceptor{
otgrpc.OpenTracingStreamClientInterceptor(opentracing.GlobalTracer()),
middleware.StreamClientInstrumentInterceptor(bigtableRequestDuration),
}
}
func gcsInstrumentation(transport http.RoundTripper) *http.Client {
client := &http.Client{
Transport: instrumentedTransport{
observer: gcsRequestDuration,
next: transport,
},
}
return client
}
func toOptions(opts []grpc.DialOption) []option.ClientOption {
result := make([]option.ClientOption, 0, len(opts))
for _, opt := range opts {
result = append(result, option.WithGRPCDialOption(opt))
}
return result
}
type instrumentedTransport struct {
observer prometheus.ObserverVec
next http.RoundTripper
}
func (i instrumentedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
start := time.Now()
resp, err := i.next.RoundTrip(req)
if err == nil {
i.observer.WithLabelValues(req.Method, strconv.Itoa(resp.StatusCode)).Observe(time.Since(start).Seconds())
}
return resp, err
}