Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OpenTelemetry legacy trace #4663

Merged
merged 4 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ def trace_class(new_class = nil)
end

# @return [Class] Return the trace class to use for this mode, looking one up on the superclass if this Schema doesn't have one defined.
def trace_class_for(mode, build: false)
def trace_class_for(mode)
own_trace_modes[mode] ||
(superclass.respond_to?(:trace_class_for) ? superclass.trace_class_for(mode) : nil)
(superclass.respond_to?(:trace_class_for) ? superclass.trace_class_for(mode) : (own_trace_modes[mode] = build_trace_mode(mode)))
end

# Configure `trace_class` to be used whenever `context: { trace_mode: mode_name }` is requested.
Expand Down Expand Up @@ -921,7 +921,6 @@ def resolve_type(type, obj, ctx)

def inherited(child_class)
if self == GraphQL::Schema
child_class.own_trace_modes[:default] = child_class.build_trace_mode(:default)
child_class.directives(default_directives.values)
end
# Make sure the child class has these built out, so that
Expand Down
6 changes: 3 additions & 3 deletions spec/graphql/tracing/legacy_trace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def self.trace(key, data)
custom_trace_module = Module.new do
def execute_query(query:)
query.context[:trace_module_ran] = true
super
end
end

Expand All @@ -69,13 +70,12 @@ def int
tracer(custom_tracer)
end


res1 = parent_schema.execute("{ int }")
assert_equal true, res1.context[:trace_module_ran]
assert_nil res1.context[:tracer_ran]

res2 = child_schema.execute("{ int }")
assert_equal true, res2.context[:trace_module_ran]
assert_equal true, res2.context[:tracer_ran]
assert_equal true, res2.context[:trace_module_ran], "New Trace Ran"
assert_equal true, res2.context[:tracer_ran], "Legacy Trace Ran"
end
end
28 changes: 28 additions & 0 deletions spec/graphql/tracing/trace_modes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,34 @@ def execute_query(query:)
assert_equal :was_configured, res.context[:configured_option]
end

describe "inheriting from GraphQL::Schema" do
it "gets CallLegacyTracers" do
# Use a new base trace mode class to avoid polluting the base class
# which already-initialized schemas have in their inheritance chain
# (It causes `CallLegacyTracers` to end up in the chain twice otherwise)
GraphQL::Schema.own_trace_modes[:default] = GraphQL::Schema.build_trace_mode(:default)

child_class = Class.new(GraphQL::Schema)

# Initialize the trace class, make sure no legacy tracers are present at this point:
refute_includes child_class.trace_class_for(:default).ancestors, GraphQL::Tracing::CallLegacyTracers
tracer_class = Class.new

# add a legacy tracer
GraphQL::Schema.tracer(tracer_class)
# A newly created child class gets the right setup:
new_child_class = Class.new(GraphQL::Schema)
assert_includes new_child_class.trace_class_for(:default).ancestors, GraphQL::Tracing::CallLegacyTracers
# But what about an already-created child class?
assert_includes child_class.trace_class_for(:default).ancestors, GraphQL::Tracing::CallLegacyTracers

# Reset GraphQL::Schema tracer state:
GraphQL::Schema.send(:own_tracers).delete(tracer_class)
GraphQL::Schema.own_trace_modes[:default] = GraphQL::Schema.build_trace_mode(:default)
refute_includes GraphQL::Schema.new_trace.class.ancestors, GraphQL::Tracing::CallLegacyTracers
end
end


describe "custom default trace mode" do
class CustomDefaultSchema < TraceModesTest::ParentSchema
Expand Down