Skip to content

Commit 1f5120b

Browse files
authored
Merge pull request #4878 from rmosolgo/deprecate-tracer
Deprecate Schema.tracer (use .trace_with instead)
2 parents 8a3f531 + 94fad8b commit 1f5120b

File tree

7 files changed

+38
-28
lines changed

7 files changed

+38
-28
lines changed

lib/graphql/schema.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,11 @@ def default_directives
11291129
}.freeze
11301130
end
11311131

1132-
def tracer(new_tracer)
1132+
def tracer(new_tracer, silence_deprecation_warning: false)
1133+
if !silence_deprecation_warning
1134+
warn("`Schema.tracer(#{new_tracer.inspect})` is deprecated; use module-based `trace_with` instead. See: https://graphql-ruby.org/queries/tracing.html")
1135+
warn " #{caller(1, 1).first}"
1136+
end
11331137
default_trace = trace_class_for(:default, build: true)
11341138
if default_trace.nil? || !(default_trace < GraphQL::Tracing::CallLegacyTracers)
11351139
trace_with(GraphQL::Tracing::CallLegacyTracers)

lib/graphql/tracing/platform_tracing.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def self.use(schema_defn, options = {})
8686
else
8787
warn("`use(#{self.name})` and `Tracing::PlatformTracing` are deprecated. Use a `trace_with(...)` module instead. More info: https://graphql-ruby.org/queries/tracing.html. Please open an issue on the GraphQL-Ruby repo if you want to discuss further!")
8888
tracer = self.new(**options)
89-
schema_defn.tracer(tracer)
89+
schema_defn.tracer(tracer, silence_deprecation_warning: true)
9090
end
9191
end
9292
end

spec/graphql/language/parser_spec.rb

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,14 +413,39 @@
413413
end
414414
end
415415

416+
module ParserTrace
417+
TRACES = []
418+
def parse(query_string:)
419+
TRACES << (trace = { key: "parse", query_string: query_string })
420+
result = super
421+
trace[:result] = result
422+
result
423+
end
424+
425+
def lex(query_string:)
426+
TRACES << (trace = { key: "lex", query_string: query_string })
427+
result = super
428+
trace[:result] = result
429+
result
430+
end
431+
432+
def self.clear
433+
TRACES.clear
434+
end
435+
436+
def self.traces
437+
TRACES
438+
end
439+
end
440+
416441
it "serves traces" do
417-
TestTracing.clear
442+
ParserTrace.clear
418443
schema = Class.new(GraphQL::Schema) do
419-
tracer(TestTracing)
444+
trace_with(ParserTrace)
420445
end
421446
query = GraphQL::Query.new(schema, "{ t: __typename }")
422447
subject.parse("{ t: __typename }", trace: query.current_trace)
423-
traces = TestTracing.traces
448+
traces = ParserTrace.traces
424449
expected_traces = if USING_C_PARSER
425450
2
426451
else

spec/graphql/schema_spec.rb

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class CustomSubscriptions < GraphQL::Subscriptions::ActionCableSubscriptions
4444
cursor_encoder Object.new
4545
context_class Class.new
4646
directives [DummyFeature1]
47-
tracer GraphQL::Tracing::DataDogTracing
4847
extra_types ExtraType
4948
query_analyzer Object.new
5049
multiplex_analyzer Object.new
@@ -69,7 +68,6 @@ class CustomSubscriptions < GraphQL::Subscriptions::ActionCableSubscriptions
6968
assert_equal base_schema.orphan_types, schema.orphan_types
7069
assert_equal base_schema.context_class, schema.context_class
7170
assert_equal base_schema.directives, schema.directives
72-
assert_equal base_schema.tracers, schema.tracers
7371
assert_equal base_schema.query_analyzers, schema.query_analyzers
7472
assert_equal base_schema.multiplex_analyzers, schema.multiplex_analyzers
7573
assert_equal base_schema.disable_introspection_entry_points?, schema.disable_introspection_entry_points?
@@ -123,7 +121,6 @@ class CustomSubscriptions < GraphQL::Subscriptions::ActionCableSubscriptions
123121
multiplex_analyzer = Object.new
124122
schema.multiplex_analyzer(multiplex_analyzer)
125123
schema.rescue_from(GraphQL::ExecutionError)
126-
schema.tracer(GraphQL::Tracing::NewRelicTracing)
127124

128125
assert_equal query, schema.query
129126
assert_equal mutation, schema.mutation
@@ -142,10 +139,6 @@ class CustomSubscriptions < GraphQL::Subscriptions::ActionCableSubscriptions
142139
assert_equal base_schema.query_analyzers + [query_analyzer], schema.query_analyzers
143140
assert_equal base_schema.multiplex_analyzers + [multiplex_analyzer], schema.multiplex_analyzers
144141
assert_equal [GraphQL::Backtrace, GraphQL::Subscriptions::ActionCableSubscriptions, CustomSubscriptions], schema.plugins.map(&:first)
145-
assert_equal [GraphQL::Tracing::DataDogTracing], base_schema.tracers
146-
assert_includes base_schema.new_trace.class.ancestors, GraphQL::Tracing::CallLegacyTracers
147-
assert_equal [GraphQL::Tracing::DataDogTracing, GraphQL::Tracing::NewRelicTracing], schema.tracers
148-
assert_includes schema.new_trace.class.ancestors, GraphQL::Tracing::CallLegacyTracers
149142
assert_equal custom_query_class, schema.query_class
150143
assert_equal [ExtraType, extra_type_2], schema.extra_types
151144
assert_instance_of CustomSubscriptions, schema.subscriptions
@@ -217,16 +210,7 @@ def self.reset_calls
217210
end
218211
end
219212

220-
describe "`use` works with plugins that attach instrumentation, tracers, query analyzers" do
221-
class NoOpTracer
222-
def trace(_key, data)
223-
if (query = data[:query])
224-
query.context[:no_op_tracer_ran] = true
225-
end
226-
yield
227-
end
228-
end
229-
213+
describe "`use` works with plugins that attach instrumentation, trace modules, query analyzers" do
230214
module NoOpTrace
231215
def execute_query(query:)
232216
query.context[:no_op_trace_ran_before_query] = true
@@ -254,7 +238,6 @@ def result
254238
module PluginWithInstrumentationTracingAndAnalyzer
255239
def self.use(schema_defn)
256240
schema_defn.trace_with(NoOpTrace)
257-
schema_defn.tracer NoOpTracer.new
258241
schema_defn.query_analyzer NoOpAnalyzer
259242
end
260243
end
@@ -281,7 +264,6 @@ def foobar; 1337; end
281264

282265
assert_equal true, query.context[:no_op_trace_ran_before_query]
283266
assert_equal true, query.context[:no_op_trace_ran_after_query]
284-
assert_equal true, query.context[:no_op_tracer_ran]
285267
assert_equal true, query.context[:no_op_analyzer_ran_initialize]
286268
assert_equal true, query.context[:no_op_analyzer_ran_on_leave_field]
287269
assert_equal true, query.context[:no_op_analyzer_ran_result]
@@ -308,7 +290,6 @@ def foobar; 1337; end
308290

309291
assert_equal true, query.context[:no_op_trace_ran_before_query]
310292
assert_equal true, query.context[:no_op_trace_ran_after_query]
311-
assert_equal true, query.context[:no_op_tracer_ran]
312293
assert_equal true, query.context[:no_op_analyzer_ran_initialize]
313294
assert_equal true, query.context[:no_op_analyzer_ran_on_leave_field]
314295
assert_equal true, query.context[:no_op_analyzer_ran_result]

spec/graphql/tracing/legacy_trace_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def int
2222

2323
parent_schema = Class.new(GraphQL::Schema) do
2424
query(query_type)
25-
tracer(custom_tracer)
25+
tracer(custom_tracer, silence_deprecation_warning: true)
2626
end
2727

2828
child_schema = Class.new(parent_schema)

spec/graphql/tracing/notifications_tracing_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def trigger_fake_notifications_tracer(schema)
4040
end
4141

4242
tracer = GraphQL::Tracing::NotificationsTracing.new(engine)
43-
schema.tracer(tracer)
43+
schema.tracer(tracer, silence_deprecation_warning: true)
4444
schema.execute "query X { int }"
4545

4646
dispatched_events

spec/graphql/tracing/trace_modes_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def execute_query(query:)
137137
tracer_class = Class.new
138138

139139
# add a legacy tracer
140-
GraphQL::Schema.tracer(tracer_class)
140+
GraphQL::Schema.tracer(tracer_class, silence_deprecation_warning: true)
141141
# A newly created child class gets the right setup:
142142
new_child_class = Class.new(GraphQL::Schema)
143143
assert_includes new_child_class.trace_class_for(:default).ancestors, GraphQL::Tracing::CallLegacyTracers

0 commit comments

Comments
 (0)