forked from 99designs/gqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracing.go
108 lines (90 loc) · 2.77 KB
/
tracing.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package apollofederatedtracingv1
import (
"context"
"encoding/base64"
"fmt"
"github.com/99designs/gqlgen/graphql"
"google.golang.org/protobuf/proto"
)
type (
Tracer struct {
ClientName string
Version string
Hostname string
}
treeBuilderKey string
)
const (
key = treeBuilderKey("treeBuilder")
)
var _ interface {
graphql.HandlerExtension
graphql.ResponseInterceptor
graphql.FieldInterceptor
graphql.OperationInterceptor
} = &Tracer{}
// ExtensionName returns the name of the extension
func (Tracer) ExtensionName() string {
return "ApolloFederatedTracingV1"
}
// Validate returns errors based on the schema; since this extension doesn't require validation, we return nil
func (Tracer) Validate(graphql.ExecutableSchema) error {
return nil
}
func (t *Tracer) shouldTrace(ctx context.Context) bool {
return graphql.GetOperationContext(ctx).Headers.Get("apollo-federation-include-trace") == "ftv1"
}
func (t *Tracer) getTreeBuilder(ctx context.Context) *TreeBuilder {
val := ctx.Value(key)
if val == nil {
return nil
}
if tb, ok := val.(*TreeBuilder); ok {
return tb
}
return nil
}
// InterceptOperation acts on each Graph operation; on each operation, start a tree builder and start the tree's timer for tracing
func (t *Tracer) InterceptOperation(ctx context.Context, next graphql.OperationHandler) graphql.ResponseHandler {
if !t.shouldTrace(ctx) {
return next(ctx)
}
return next(context.WithValue(ctx, key, NewTreeBuilder()))
}
// InterceptField is called on each field's resolution, including information about the path and parent node.
// This information is then used to build the relevant Node Tree used in the FTV1 tracing format
func (t *Tracer) InterceptField(ctx context.Context, next graphql.Resolver) (interface{}, error) {
if !t.shouldTrace(ctx) {
return next(ctx)
}
if tb := t.getTreeBuilder(ctx); tb != nil {
tb.WillResolveField(ctx)
}
return next(ctx)
}
// InterceptResponse is called before the overall response is sent, but before each field resolves; as a result
// the final marshaling is deferred to happen at the end of the operation
func (t *Tracer) InterceptResponse(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
if !t.shouldTrace(ctx) {
return next(ctx)
}
tb := t.getTreeBuilder(ctx)
if tb != nil {
tb.StartTimer(ctx)
}
val := new(string)
graphql.RegisterExtension(ctx, "ftv1", val)
// now that fields have finished resolving, it stops the timer to calculate trace duration
defer func(val *string) {
tb.StopTimer(ctx)
// marshal the protobuf ...
p, err := proto.Marshal(tb.Trace)
if err != nil {
fmt.Print(err)
}
// ... then set the previously instantiated string as the base64 formatted string as required
*val = base64.StdEncoding.EncodeToString(p)
}(val)
resp := next(ctx)
return resp
}