Skip to content

Commit c4f2eae

Browse files
ashleyyyashleyyy
andauthored
fix(tracer): add support for multiplex (#243)
* fix(tracer): add support for multiplex * style(tracer): make rubocop happier --------- Co-authored-by: ashleyyy <[email protected]>
1 parent 1f849e3 commit c4f2eae

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

lib/apollo-federation/tracing/tracer.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,26 @@ def self.execute_query_lazy(data, &block)
8383
result = block.call
8484

8585
query = data.fetch(:query)
86-
return result unless query.context && query.context[:tracing_enabled]
86+
multiplex = data.fetch(:multiplex)
87+
88+
if query
89+
record_trace_end_time(query)
90+
elsif multiplex
91+
multiplex.queries.each { |q| record_trace_end_time(q) }
92+
end
93+
94+
result
95+
end
96+
97+
def self.record_trace_end_time(query)
98+
return unless query.context && query.context[:tracing_enabled]
8799

88100
trace = query.context.namespace(ApolloFederation::Tracing::KEY)
89101

90102
trace.merge!(
91103
end_time: Time.now.utc,
92104
end_time_nanos: Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond),
93105
)
94-
95-
result
96106
end
97107

98108
# Step 2:

spec/apollo-federation/tracing_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,41 @@ def test
4949
result = schema.execute('{ test }', context: { tracing_enabled: true, debug_tracing: true })
5050
expect(result[:extensions][:ftv1_debug]).not_to be_nil
5151
end
52+
53+
context 'with a multiplex query' do
54+
it 'does not add tracing extension by default' do
55+
result = schema.multiplex(
56+
[
57+
{ query: '{ test }' },
58+
{ query: '{ test }' },
59+
{ query: '{ test }' },
60+
],
61+
).first
62+
expect(result[:extensions]).to be_nil
63+
end
64+
65+
it 'adds the extensions.ftv1 when the context has tracing_enabled: true' do
66+
result = schema.multiplex(
67+
[
68+
{ query: '{ test }', context: { tracing_enabled: true } },
69+
{ query: '{ test }', context: { tracing_enabled: true } },
70+
{ query: '{ test }', context: { tracing_enabled: true } },
71+
],
72+
).first
73+
expect(result[:extensions][:ftv1]).not_to be_nil
74+
end
75+
76+
it 'adds debugging info when the context has debug_tracing: true' do
77+
result = schema.multiplex(
78+
[
79+
{ query: '{ test }', context: { tracing_enabled: true, debug_tracing: true } },
80+
{ query: '{ test }', context: { tracing_enabled: true, debug_tracing: true } },
81+
{ query: '{ test }', context: { tracing_enabled: true, debug_tracing: true } },
82+
],
83+
).first
84+
expect(result[:extensions][:ftv1_debug]).not_to be_nil
85+
end
86+
end
5287
end
5388

5489
def trace(query)

0 commit comments

Comments
 (0)