forked from DataDog/dd-trace-go
/
graphql.go
92 lines (82 loc) · 2.79 KB
/
graphql.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
// Package graphql provides functions to trace the graph-gophers/graphql-go package (https://github.com/graph-gophers/graphql-go).
//
// We use the tracing mechanism available in the
// https://godoc.org/github.com/graph-gophers/graphql-go/trace subpackage.
// Create a new Tracer with `NewTracer` and pass it as an additional option to
// `MustParseSchema`.
package graphql // import "gopkg.in/DataDog/dd-trace-go.v1/contrib/graph-gophers/graphql-go"
import (
"context"
"fmt"
"github.com/graph-gophers/graphql-go/errors"
"github.com/graph-gophers/graphql-go/introspection"
"github.com/graph-gophers/graphql-go/trace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
const (
tagGraphqlField = "graphql.field"
tagGraphqlQuery = "graphql.query"
tagGraphqlType = "graphql.type"
)
// A Tracer implements the graphql-go/trace.Tracer interface by sending traces
// to the Datadog tracer.
type Tracer struct {
cfg *config
}
var _ trace.Tracer = (*Tracer)(nil)
// TraceQuery traces a GraphQL query.
func (t *Tracer) TraceQuery(ctx context.Context, queryString string, operationName string, variables map[string]interface{}, varTypes map[string]*introspection.Type) (context.Context, trace.TraceQueryFinishFunc) {
opts := []ddtrace.StartSpanOption{
tracer.ServiceName(t.cfg.serviceName),
tracer.Tag(tagGraphqlQuery, queryString),
}
if t.cfg.analyticsRate > 0 {
opts = append(opts, tracer.Tag(ext.EventSampleRate, t.cfg.analyticsRate))
}
span, ctx := tracer.StartSpanFromContext(ctx, "graphql.request", opts...)
return ctx, func(errs []*errors.QueryError) {
var err error
switch n := len(errs); n {
case 0:
// err = nil
case 1:
err = errs[0]
default:
err = fmt.Errorf("%s (and %d more errors)", errs[0], n-1)
}
span.Finish(tracer.WithError(err))
}
}
// TraceField traces a GraphQL field access.
func (t *Tracer) TraceField(ctx context.Context, label string, typeName string, fieldName string, trivial bool, args map[string]interface{}) (context.Context, trace.TraceFieldFinishFunc) {
opts := []ddtrace.StartSpanOption{
tracer.ServiceName(t.cfg.serviceName),
tracer.Tag(tagGraphqlField, fieldName),
tracer.Tag(tagGraphqlType, typeName),
}
if t.cfg.analyticsRate > 0 {
opts = append(opts, tracer.Tag(ext.EventSampleRate, t.cfg.analyticsRate))
}
span, ctx := tracer.StartSpanFromContext(ctx, "graphql.field", opts...)
return ctx, func(err *errors.QueryError) {
// must explicitly check for nil, see issue golang/go#22729
if err != nil {
span.Finish(tracer.WithError(err))
} else {
span.Finish()
}
}
}
// NewTracer creates a new Tracer.
func NewTracer(opts ...Option) trace.Tracer {
cfg := new(config)
defaults(cfg)
for _, opt := range opts {
opt(cfg)
}
return &Tracer{
cfg: cfg,
}
}