forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace.go
72 lines (59 loc) · 1.94 KB
/
trace.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
package backend
import (
"context"
"fmt"
"strconv"
"time"
"github.com/inconshreveable/log15"
"github.com/prometheus/client_golang/prometheus"
"github.com/sourcegraph/sourcegraph/internal/actor"
tracepkg "github.com/sourcegraph/sourcegraph/internal/trace"
"github.com/sourcegraph/sourcegraph/internal/trace/ot"
)
var metricLabels = []string{"method", "success"}
var requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "src",
Subsystem: "backend",
Name: "client_request_duration_seconds",
Help: "Total time spent on backend endpoints.",
Buckets: tracepkg.UserLatencyBuckets,
}, metricLabels)
var requestGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "src",
Subsystem: "backend",
Name: "client_requests",
Help: "Current number of requests running for a method.",
}, []string{"method"})
func init() {
prometheus.MustRegister(requestDuration)
prometheus.MustRegister(requestGauge)
}
func trace(ctx context.Context, server, method string, arg interface{}, err *error) (context.Context, func()) {
requestGauge.WithLabelValues(server + "." + method).Inc()
span, ctx := ot.StartSpanFromContext(ctx, server+"."+method)
span.SetTag("Server", server)
span.SetTag("Method", method)
span.SetTag("Argument", fmt.Sprintf("%#v", arg))
start := time.Now()
done := func() {
elapsed := time.Since(start)
if err != nil && *err != nil {
span.SetTag("Error", (*err).Error())
}
span.Finish()
name := server + "." + method
labels := prometheus.Labels{
"method": name,
"success": strconv.FormatBool(err == nil),
}
requestDuration.With(labels).Observe(elapsed.Seconds())
requestGauge.WithLabelValues(name).Dec()
uid := actor.FromContext(ctx).UID
errStr := ""
if err != nil && *err != nil {
errStr = (*err).Error()
}
log15.Debug("TRACE backend", "rpc", name, "uid", uid, "trace", tracepkg.SpanURL(span), "error", errStr, "duration", elapsed)
}
return ctx, done
}