forked from rmosolgo/graphql-ruby
/
opentelemetry_tracing_spec.rb
156 lines (124 loc) · 5.21 KB
/
opentelemetry_tracing_spec.rb
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
# frozen_string_literal: true
require "spec_helper"
describe ::GraphQL::Tracing::OpenTelemetryTracing do
module OpenTelemetryTest
class Thing < GraphQL::Schema::Object
implements GraphQL::Types::Relay::Node
end
class Query < GraphQL::Schema::Object
include GraphQL::Types::Relay::HasNodeField
field :int, Integer, null: false
def int
1
end
end
class SchemaWithPerRequestTracing < GraphQL::Schema
query(Query)
use(GraphQL::Tracing::OpenTelemetryTracing)
orphan_types(Thing)
def self.object_from_id(_id, _ctx)
:thing
end
def self.resolve_type(_type, _obj, _ctx)
Thing
end
end
end
before do
OpenTelemetry::Instrumentation::GraphQL::Instrumentation.clear_all
end
it "captures all keys when tracing is enabled in config and in query execution context" do
query = GraphQL::Query.new(OpenTelemetryTest::SchemaWithPerRequestTracing, '{ node(id: "1") { __typename } }')
query.context.namespace(:opentelemetry)[:enable_platform_field] = true
query.context.namespace(:opentelemetry)[:enable_platform_authorized] = true
query.context.namespace(:opentelemetry)[:enable_platform_resolve_type] = true
query.result
assert_span("Query.authorized")
assert_span("Query.node")
assert_span("Node.resolve_type")
assert_span("Thing.authorized")
assert_span("DynamicFields.authorized")
end
it "does not capture the keys when tracing is not enabled in config but is enabled in query execution context" do
OpenTelemetry::Instrumentation::GraphQL::Instrumentation.instance.stub(:config, {
schemas: [],
enable_platform_field: false,
enable_platform_authorized: false,
enable_platform_resolve_type: false
}) do
query = GraphQL::Query.new(OpenTelemetryTest::SchemaWithPerRequestTracing, '{ node(id: "1") { __typename } }')
query.context.namespace(:opentelemetry)[:enable_platform_field] = true
query.context.namespace(:opentelemetry)[:enable_platform_authorized] = true
query.context.namespace(:opentelemetry)[:enable_platform_resolve_type] = true
query.result
refute_span("Query.authorized")
refute_span("Query.node")
refute_span("Node.resolve_type")
refute_span("Thing.authorized")
refute_span("DynamicFields.authorized")
end
end
it "does not capture any key when tracing is not enabled in config and tracing is not set in context" do
OpenTelemetry::Instrumentation::GraphQL::Instrumentation.instance.stub(:config, {
schemas: [],
enable_platform_field: false,
enable_platform_authorized: false,
enable_platform_resolve_type: false
}) do
query = GraphQL::Query.new(OpenTelemetryTest::SchemaWithPerRequestTracing, '{ node(id: "1") { __typename } }')
query.result
refute_span("Query.authorized")
refute_span("Query.node")
refute_span("Node.resolve_type")
refute_span("Thing.authorized")
refute_span("DynamicFields.authorized")
end
end
it "does not capture any key when tracing is not enabled in config and context" do
OpenTelemetry::Instrumentation::GraphQL::Instrumentation.instance.stub(:config, {
schemas: [],
enable_platform_field: false,
enable_platform_authorized: false,
enable_platform_resolve_type: false
}) do
query = GraphQL::Query.new(OpenTelemetryTest::SchemaWithPerRequestTracing, '{ node(id: "1") { __typename } }')
query.context.namespace(:opentelemetry)[:enable_platform_field] = false
query.context.namespace(:opentelemetry)[:enable_platform_authorized] = false
query.context.namespace(:opentelemetry)[:enable_platform_resolve_type] = false
query.result
refute_span("Query.authorized")
refute_span("Query.node")
refute_span("Node.resolve_type")
refute_span("Thing.authorized")
refute_span("DynamicFields.authorized")
end
end
it "captures all keys when tracing is enabled in config but is not set in context" do
query = GraphQL::Query.new(OpenTelemetryTest::SchemaWithPerRequestTracing, '{ node(id: "1") { __typename } }')
query.result
assert_span("Query.authorized")
assert_span("Query.node")
assert_span("Node.resolve_type")
assert_span("Thing.authorized")
assert_span("DynamicFields.authorized")
end
it "does not capture any key when tracing is enabled in config but is not enabled in context" do
query = GraphQL::Query.new(OpenTelemetryTest::SchemaWithPerRequestTracing, '{ node(id: "1") { __typename } }')
query.context.namespace(:opentelemetry)[:enable_platform_field] = false
query.context.namespace(:opentelemetry)[:enable_platform_authorized] = false
query.context.namespace(:opentelemetry)[:enable_platform_resolve_type] = false
query.result
refute_span("Query.authorized")
refute_span("Query.node")
refute_span("Node.resolve_type")
refute_span("Thing.authorized")
refute_span("DynamicFields.authorized")
end
private
def assert_span(span)
assert OpenTelemetry::Instrumentation::GraphQL::Instrumentation::EVENTS.include?(span)
end
def refute_span(span)
refute OpenTelemetry::Instrumentation::GraphQL::Instrumentation::EVENTS.include?(span)
end
end