diff --git a/instrumentation/graphql/lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb b/instrumentation/graphql/lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb index 70c9666235..cb82bf2c7f 100644 --- a/instrumentation/graphql/lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb +++ b/instrumentation/graphql/lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb @@ -60,15 +60,15 @@ def initialize(trace_scalars: false, **_options) end def execute_multiplex(multiplex:, &block) - tracer.in_span('graphql.execute_multiplex', &block) + tracer.in_span('graphql.execute_multiplex') { super } end def lex(query_string:, &block) - tracer.in_span('graphql.lex', &block) + tracer.in_span('graphql.lex') { super } end def parse(query_string:, &block) - tracer.in_span('graphql.parse', &block) + tracer.in_span('graphql.parse') { super } end def validate(query:, validate:, &block) @@ -89,11 +89,11 @@ def validate(query:, validate:, &block) end def analyze_multiplex(multiplex:, &block) - tracer.in_span('graphql.analyze_multiplex', &block) + tracer.in_span('graphql.analyze_multiplex') { super } end def analyze_query(query:, &block) - tracer.in_span('graphql.analyze_query', &block) + tracer.in_span('graphql.analyze_query') { super } end def execute_query(query:, &block) @@ -102,11 +102,13 @@ def execute_query(query:, &block) attributes['graphql.operation.type'] = query.selected_operation.operation_type attributes['graphql.document'] = query.query_string - tracer.in_span('graphql.execute_query', attributes: attributes, &block) + tracer.in_span('graphql.execute_query', attributes: attributes) do + super + end end def execute_query_lazy(query:, multiplex:, &block) - tracer.in_span('graphql.execute_query_lazy', &block) + tracer.in_span('graphql.execute_query_lazy') { super } end def execute_field(field:, query:, ast_node:, arguments:, object:, &block) @@ -133,7 +135,7 @@ def authorized(query:, type:, object:, &block) attributes = @_otel_type_attrs_cache[type] - tracer.in_span(platform_key, attributes: attributes, &block) + tracer.in_span(platform_key, attributes: attributes) { super } end def authorized_lazy(query:, type:, object:, &block) @@ -141,19 +143,19 @@ def authorized_lazy(query:, type:, object:, &block) return super unless platform_key attributes = @_otel_lazy_type_attrs_cache[type] - tracer.in_span(platform_key, attributes: attributes, &block) + tracer.in_span(platform_key, attributes: attributes) { super } end def resolve_type(query:, type:, object:, &block) platform_key = @_otel_resolve_type_key_cache[type] attributes = @_otel_type_attrs_cache[type] - tracer.in_span(platform_key, attributes: attributes, &block) + tracer.in_span(platform_key, attributes: attributes) { super } end def resolve_type_lazy(query:, type:, object:, &block) platform_key = @_otel_resolve_type_key_cache[type] attributes = @_otel_lazy_type_attrs_cache[type] - tracer.in_span(platform_key, attributes: attributes, &block) + tracer.in_span(platform_key, attributes: attributes) { super } end private diff --git a/instrumentation/graphql/test/instrumentation/graphql/tracers/graphql_trace_test.rb b/instrumentation/graphql/test/instrumentation/graphql/tracers/graphql_trace_test.rb index 22e194036f..f1d477741f 100644 --- a/instrumentation/graphql/test/instrumentation/graphql/tracers/graphql_trace_test.rb +++ b/instrumentation/graphql/test/instrumentation/graphql/tracers/graphql_trace_test.rb @@ -361,6 +361,15 @@ def platform_field_key(field) _(custom_events).must_equal(true) end end + + it 'works with other trace modules' do + res = SomeGraphQLAppSchema.execute('{ vehicle { __typename } }') + pp [res.query.schema, res.query.schema.trace_class.ancestors] + assert_equal 'Car', res['data']['vehicle']['__typename'] + pp res.context.to_h.keys + assert res.context[:custom_trace_execute_query_ran] + assert res.query.multiplex.context[:custom_trace_execute_multiplex_ran] + end end end end diff --git a/instrumentation/graphql/test/test_helper.rb b/instrumentation/graphql/test/test_helper.rb index 7129b14082..5943c578c0 100644 --- a/instrumentation/graphql/test/test_helper.rb +++ b/instrumentation/graphql/test/test_helper.rb @@ -106,6 +106,24 @@ def self.resolve_type(_type, _obj, ctx) Car end end + + module CustomTrace + def execute_multiplex(multiplex) + puts 'In CustomTrace#execute_multiplex' + multiplex.context[:custom_trace_execute_multiplex_ran] = true + super + end + + def execute_query(query) + puts 'In CustomTrace#execute_query' + multiplex.context[:custom_trace_execute_query_ran] = true + super + end + end + + trace_with CustomTrace + + p [self, trace_class.ancestors] end class SomeOtherGraphQLAppSchema < GraphQL::Schema