Skip to content

Commit 117733a

Browse files
authored
fix: Add super calls to GraphqlTrace (#1090)
fix(graphql): Add super calls to GraphqlTrace
1 parent 35ddf66 commit 117733a

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

instrumentation/graphql/lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ def initialize(trace_scalars: false, **_options)
6060
end
6161

6262
def execute_multiplex(multiplex:, &block)
63-
tracer.in_span('graphql.execute_multiplex', &block)
63+
tracer.in_span('graphql.execute_multiplex') { super }
6464
end
6565

6666
def lex(query_string:, &block)
67-
tracer.in_span('graphql.lex', &block)
67+
tracer.in_span('graphql.lex') { super }
6868
end
6969

7070
def parse(query_string:, &block)
71-
tracer.in_span('graphql.parse', &block)
71+
tracer.in_span('graphql.parse') { super }
7272
end
7373

7474
def validate(query:, validate:, &block)
@@ -89,11 +89,11 @@ def validate(query:, validate:, &block)
8989
end
9090

9191
def analyze_multiplex(multiplex:, &block)
92-
tracer.in_span('graphql.analyze_multiplex', &block)
92+
tracer.in_span('graphql.analyze_multiplex') { super }
9393
end
9494

9595
def analyze_query(query:, &block)
96-
tracer.in_span('graphql.analyze_query', &block)
96+
tracer.in_span('graphql.analyze_query') { super }
9797
end
9898

9999
def execute_query(query:, &block)
@@ -102,11 +102,13 @@ def execute_query(query:, &block)
102102
attributes['graphql.operation.type'] = query.selected_operation.operation_type
103103
attributes['graphql.document'] = query.query_string
104104

105-
tracer.in_span('graphql.execute_query', attributes: attributes, &block)
105+
tracer.in_span('graphql.execute_query', attributes: attributes) do
106+
super
107+
end
106108
end
107109

108110
def execute_query_lazy(query:, multiplex:, &block)
109-
tracer.in_span('graphql.execute_query_lazy', &block)
111+
tracer.in_span('graphql.execute_query_lazy') { super }
110112
end
111113

112114
def execute_field(field:, query:, ast_node:, arguments:, object:, &block)
@@ -133,27 +135,27 @@ def authorized(query:, type:, object:, &block)
133135

134136
attributes = @_otel_type_attrs_cache[type]
135137

136-
tracer.in_span(platform_key, attributes: attributes, &block)
138+
tracer.in_span(platform_key, attributes: attributes) { super }
137139
end
138140

139141
def authorized_lazy(query:, type:, object:, &block)
140142
platform_key = @_otel_authorized_key_cache[type]
141143
return super unless platform_key
142144

143145
attributes = @_otel_lazy_type_attrs_cache[type]
144-
tracer.in_span(platform_key, attributes: attributes, &block)
146+
tracer.in_span(platform_key, attributes: attributes) { super }
145147
end
146148

147149
def resolve_type(query:, type:, object:, &block)
148150
platform_key = @_otel_resolve_type_key_cache[type]
149151
attributes = @_otel_type_attrs_cache[type]
150-
tracer.in_span(platform_key, attributes: attributes, &block)
152+
tracer.in_span(platform_key, attributes: attributes) { super }
151153
end
152154

153155
def resolve_type_lazy(query:, type:, object:, &block)
154156
platform_key = @_otel_resolve_type_key_cache[type]
155157
attributes = @_otel_lazy_type_attrs_cache[type]
156-
tracer.in_span(platform_key, attributes: attributes, &block)
158+
tracer.in_span(platform_key, attributes: attributes) { super }
157159
end
158160

159161
private

instrumentation/graphql/test/instrumentation/graphql/tracers/graphql_trace_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,41 @@ def platform_field_key(field)
361361
_(custom_events).must_equal(true)
362362
end
363363
end
364+
365+
module CustomTrace
366+
def execute_multiplex(multiplex:)
367+
multiplex.context[:custom_trace_execute_multiplex_ran] = true
368+
super
369+
end
370+
371+
def execute_query(query:)
372+
query.context[:custom_trace_execute_query_ran] = true
373+
super
374+
end
375+
end
376+
377+
it 'works with other trace modules' do
378+
[GraphQL::Schema, SomeOtherGraphQLAppSchema, SomeGraphQLAppSchema].each(&:_reset_tracer_for_testing)
379+
instrumentation.instance_variable_set(:@installed, false)
380+
381+
custom_trace_schema = Class.new(GraphQL::Schema) do
382+
query(Class.new(GraphQL::Schema::Object) do
383+
graphql_name 'Query'
384+
field :int, Integer, fallback_value: 5
385+
end)
386+
387+
trace_with(CustomTrace)
388+
end
389+
390+
instrumentation.install({ schemas: [custom_trace_schema] })
391+
res = custom_trace_schema.execute('{ int }')
392+
assert_equal 5, res['data']['int'], 'The query ran successfully'
393+
394+
assert res.context[:custom_trace_execute_query_ran], 'The custom execute_query hook ran'
395+
assert res.query.multiplex.context[:custom_trace_execute_multiplex_ran], 'The custom execute_multiplex hook ran'
396+
397+
assert spans.find { |s| s.name == 'graphql.execute_query' }, 'OpenTelementry ran'
398+
end
364399
end
365400
end
366401
end

0 commit comments

Comments
 (0)