-
Notifications
You must be signed in to change notification settings - Fork 415
/
tracer_test.go
160 lines (150 loc) · 4.37 KB
/
tracer_test.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// 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 2022 Datadog, Inc.
package gqlgen
import (
"testing"
"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler/testserver"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/stretchr/testify/assert"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer"
)
func TestOptions(t *testing.T) {
query := `{ name }`
for name, tt := range map[string]struct {
tracerOpts []Option
test func(assert *assert.Assertions, root mocktracer.Span)
}{
"default": {
test: func(assert *assert.Assertions, root mocktracer.Span) {
assert.Equal("graphql.query", root.OperationName())
assert.Equal(query, root.Tag(ext.ResourceName))
assert.Equal(defaultServiceName, root.Tag(ext.ServiceName))
assert.Equal(ext.SpanTypeGraphQL, root.Tag(ext.SpanType))
assert.Equal("99designs/gqlgen", root.Tag(ext.Component))
assert.Nil(root.Tag(ext.EventSampleRate))
},
},
"WithServiceName": {
tracerOpts: []Option{WithServiceName("TestServer")},
test: func(assert *assert.Assertions, root mocktracer.Span) {
assert.Equal("TestServer", root.Tag(ext.ServiceName))
},
},
"WithAnalytics/true": {
tracerOpts: []Option{WithAnalytics(true)},
test: func(assert *assert.Assertions, root mocktracer.Span) {
assert.Equal(1.0, root.Tag(ext.EventSampleRate))
},
},
"WithAnalytics/false": {
tracerOpts: []Option{WithAnalytics(false)},
test: func(assert *assert.Assertions, root mocktracer.Span) {
assert.Nil(root.Tag(ext.EventSampleRate))
},
},
"WithAnalyticsRate": {
tracerOpts: []Option{WithAnalyticsRate(0.5)},
test: func(assert *assert.Assertions, root mocktracer.Span) {
assert.Equal(0.5, root.Tag(ext.EventSampleRate))
},
},
} {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
mt := mocktracer.Start()
defer mt.Stop()
c := newTestClient(t, testserver.New(), NewTracer(tt.tracerOpts...))
var resp struct {
Name string
}
c.MustPost(query, &resp)
var root mocktracer.Span
for _, span := range mt.FinishedSpans() {
if span.ParentID() == 0 {
root = span
}
}
assert.NotNil(root)
tt.test(assert, root)
assert.Nil(root.Tag(ext.Error))
})
}
}
func TestError(t *testing.T) {
assert := assert.New(t)
mt := mocktracer.Start()
defer mt.Stop()
c := newTestClient(t, testserver.NewError(), NewTracer())
var resp struct {
Name string
}
err := c.Post(`{ name }`, &resp)
assert.NotNil(err)
var root mocktracer.Span
for _, span := range mt.FinishedSpans() {
if span.ParentID() == 0 {
root = span
}
}
assert.NotNil(root)
assert.NotNil(root.Tag(ext.Error))
}
func TestObfuscation(t *testing.T) {
assert := assert.New(t)
mt := mocktracer.Start()
defer mt.Stop()
c := newTestClient(t, testserver.New(), NewTracer())
var resp struct {
Name string
}
query := `query($id: Int!) {
name
find(id: $id)
}
`
err := c.Post(query, &resp, client.Var("id", 12345))
assert.Nil(err)
// No spans should contain the sensitive ID.
for _, span := range mt.FinishedSpans() {
assert.NotContains(span.Tag(ext.ResourceName), "12345")
}
}
func TestChildSpans(t *testing.T) {
assert := assert.New(t)
mt := mocktracer.Start()
defer mt.Stop()
c := newTestClient(t, testserver.New(), NewTracer())
var resp struct {
Name string
}
query := `{ name }`
err := c.Post(query, &resp)
assert.Nil(err)
var root mocktracer.Span
allSpans := mt.FinishedSpans()
var resNames []string
var opNames []string
for _, span := range allSpans {
if span.ParentID() == 0 {
root = span
}
resNames = append(resNames, span.Tag(ext.ResourceName).(string))
opNames = append(opNames, span.OperationName())
assert.Equal("99designs/gqlgen", span.Tag(ext.Component))
}
assert.ElementsMatch(resNames, []string{readOp, validationOp, parsingOp, query})
assert.ElementsMatch(opNames, []string{readOp, validationOp, parsingOp, "graphql.query"})
assert.NotNil(root)
assert.Nil(root.Tag(ext.Error))
}
func newTestClient(t *testing.T, h *testserver.TestServer, tracer graphql.HandlerExtension) *client.Client {
t.Helper()
h.AddTransport(transport.POST{})
h.Use(tracer)
return client.New(h)
}