-
Notifications
You must be signed in to change notification settings - Fork 438
/
graphql.go
117 lines (103 loc) · 3.66 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
// 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"
"math"
"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"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
"gopkg.in/DataDog/dd-trace-go.v1/internal/telemetry"
"github.com/graph-gophers/graphql-go/errors"
"github.com/graph-gophers/graphql-go/introspection"
"github.com/graph-gophers/graphql-go/trace"
)
const componentName = "graph-gophers/graphql-go"
func init() {
telemetry.LoadIntegration(componentName)
}
const (
tagGraphqlField = "graphql.field"
tagGraphqlQuery = "graphql.query"
tagGraphqlType = "graphql.type"
tagGraphqlOperationName = "graphql.operation.name"
)
// 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, _ map[string]interface{}, _ map[string]*introspection.Type) (context.Context, trace.TraceQueryFinishFunc) {
opts := []ddtrace.StartSpanOption{
tracer.ServiceName(t.cfg.serviceName),
tracer.Tag(tagGraphqlQuery, queryString),
tracer.Tag(tagGraphqlOperationName, operationName),
tracer.Tag(ext.Component, componentName),
tracer.Measured(),
}
if !math.IsNaN(t.cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, t.cfg.analyticsRate))
}
span, ctx := tracer.StartSpanFromContext(ctx, t.cfg.querySpanName, 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, _ string, typeName string, fieldName string, trivial bool, _ map[string]interface{}) (context.Context, trace.TraceFieldFinishFunc) {
if t.cfg.omitTrivial && trivial {
return ctx, func(queryError *errors.QueryError) {}
}
opts := []ddtrace.StartSpanOption{
tracer.ServiceName(t.cfg.serviceName),
tracer.Tag(tagGraphqlField, fieldName),
tracer.Tag(tagGraphqlType, typeName),
tracer.Tag(ext.Component, componentName),
tracer.Measured(),
}
if !math.IsNaN(t.cfg.analyticsRate) {
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)
}
log.Debug("contrib/graph-gophers/graphql-go: Configuring Graphql Tracer: %#v", cfg)
return &Tracer{
cfg: cfg,
}
}