forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobservability.go
More file actions
79 lines (66 loc) · 2.4 KB
/
observability.go
File metadata and controls
79 lines (66 loc) · 2.4 KB
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 codenav
import (
"context"
"fmt"
"time"
"github.com/sourcegraph/log"
"github.com/sourcegraph/sourcegraph/internal/metrics"
"github.com/sourcegraph/sourcegraph/internal/observation"
)
type operations struct {
getReferences *observation.Operation
getImplementations *observation.Operation
getDiagnostics *observation.Operation
getHover *observation.Operation
getDefinitions *observation.Operation
getRanges *observation.Operation
getStencil *observation.Operation
getDumpsByIDs *observation.Operation
getClosestDumpsForBlob *observation.Operation
}
func newOperations(observationContext *observation.Context) *operations {
metrics := metrics.NewREDMetrics(
observationContext.Registerer,
"codeintel_codenav",
metrics.WithLabels("op"),
metrics.WithCountHelp("Total number of method invocations."),
)
op := func(name string) *observation.Operation {
return observationContext.Operation(observation.Op{
Name: fmt.Sprintf("codeintel.codenav.%s", name),
MetricLabelValues: []string{name},
Metrics: metrics,
})
}
return &operations{
getReferences: op("getReferences"),
getImplementations: op("getImplementations"),
getDiagnostics: op("getDiagnostics"),
getHover: op("getHover"),
getDefinitions: op("getDefinitions"),
getRanges: op("getRanges"),
getStencil: op("getStencil"),
getDumpsByIDs: op("GetDumpsByIDs"),
getClosestDumpsForBlob: op("GetClosestDumpsForBlob"),
}
}
var serviceObserverThreshold = time.Second
func observeResolver(ctx context.Context, err *error, operation *observation.Operation, threshold time.Duration, observationArgs observation.Args) (context.Context, observation.TraceLogger, func()) {
start := time.Now()
ctx, trace, endObservation := operation.With(ctx, err, observationArgs)
return ctx, trace, func() {
duration := time.Since(start)
endObservation(1, observation.Args{})
if duration >= threshold {
// use trace logger which includes all relevant fields
lowSlowRequest(trace, duration, err)
}
}
}
func lowSlowRequest(logger log.Logger, duration time.Duration, err *error) {
fields := []log.Field{log.Duration("duration", duration)}
if err != nil && *err != nil {
fields = append(fields, log.Error(*err))
}
logger.Warn("Slow codeintel request", fields...)
}