-
Notifications
You must be signed in to change notification settings - Fork 2
/
trace.go
88 lines (68 loc) · 2.7 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package dataloader
import (
"context"
"fmt"
"strings"
opentracing "github.com/opentracing/opentracing-go"
)
// Tracer is an interface that may be used to implement tracing.
type Tracer interface {
// Load handles tracing for calls to the Load function
Load(context.Context, Key) (context.Context, LoadFinishFunc)
// LoadMany handles tracing for calls to the LoadMany function
LoadMany(context.Context, []Key) (context.Context, LoadManyFinishFunc)
// Batch handles tracing for calls to the BatchFunction function
Batch(context.Context) (context.Context, BatchFinishFunc)
}
type (
// LoadFinishFunc finishes the tracing for the Load function
LoadFinishFunc func(Result)
// LoadManyFinishFunc finishes the tracing for the LoadMany function
LoadManyFinishFunc func(ResultMap)
// BatchFinishFunc finishes the tracing for the BatchFunction
BatchFinishFunc func(ResultMap)
)
// ======================================= no-op tracing implementation ======================================
// NewNoOpTracer returns an instance of a blank tracer with no action
func NewNoOpTracer() Tracer {
return &noOpTracer{}
}
type noOpTracer struct{}
func (*noOpTracer) Load(ctx context.Context, _ Key) (context.Context, LoadFinishFunc) {
return ctx, func(Result) {}
}
func (*noOpTracer) LoadMany(ctx context.Context, _ []Key) (context.Context, LoadManyFinishFunc) {
return ctx, func(ResultMap) {}
}
func (*noOpTracer) Batch(ctx context.Context) (context.Context, BatchFinishFunc) {
return ctx, func(ResultMap) {}
}
// ======================================= open tracing implementation =======================================
// NewOpenTracingTracer returns an instance of a tracer conforming to the open tracing standard
func NewOpenTracingTracer() Tracer {
return &openTracer{}
}
type openTracer struct{}
func (*openTracer) Load(ctx context.Context, key Key) (context.Context, LoadFinishFunc) {
span, spanCtx := opentracing.StartSpanFromContext(ctx, "Dataloader: load")
if key != nil {
span.SetTag("dataloader.key", key.String())
}
return spanCtx, func(Result) { span.Finish() }
}
func (*openTracer) LoadMany(ctx context.Context, keyArr []Key) (context.Context, LoadManyFinishFunc) {
span, spanCtx := opentracing.StartSpanFromContext(ctx, "Dataloader: loadMany")
keys := make([]string, 0, len(keyArr))
for _, k := range keyArr {
keys = append(keys, k.String())
}
span.SetTag("dataloader.keys", keys)
return spanCtx, func(ResultMap) {}
}
func (*openTracer) Batch(ctx context.Context) (context.Context, BatchFinishFunc) {
span, spanCtx := opentracing.StartSpanFromContext(ctx, "Dataloader: batch")
return spanCtx, func(r ResultMap) {
span.SetTag("keys", fmt.Sprintf("[%s]", strings.Join(r.Keys(), ", ")))
span.Finish()
}
}