Skip to content

Commit

Permalink
Merge pull request #73 from rmosolgo/yield-to-super
Browse files Browse the repository at this point in the history
Fix calls to other traces
  • Loading branch information
swalkinshaw authored Mar 28, 2023
2 parents 0889d54 + 3ee1e57 commit 805d496
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 30 deletions.
59 changes: 29 additions & 30 deletions lib/graphql/metrics/trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,58 +15,57 @@ def initialize(**_rest)
# multiplexing multiple queries.

# wraps everything below this line; only run once
def execute_multiplex(multiplex:, &block)
return yield if @skip_tracing
capture_multiplex_start_time(&block)

def execute_multiplex(multiplex:)
return super if @skip_tracing
capture_multiplex_start_time { super }
end

# may not trigger if the query is passed in pre-parsed
def lex(query_string:, &block)
return yield if @skip_tracing
capture_lexing_time(&block)
def lex(query_string:)
return super if @skip_tracing
capture_lexing_time { super }
end

# may not trigger if the query is passed in pre-parsed
def parse(query_string:, &block)
return yield if @skip_tracing
capture_parsing_time(&block)
def parse(query_string:)
return super if @skip_tracing
capture_parsing_time { super }
end

def validate(query:, validate:, &block)
return yield if @skip_tracing
capture_validation_time(query.context, &block)
def validate(query:, validate:)
return super if @skip_tracing
capture_validation_time(query.context) { super }
end

# wraps all `analyze_query`s; only run once
def analyze_multiplex(multiplex:, &block)
return yield if @skip_tracing
def analyze_multiplex(multiplex:)
return super if @skip_tracing
# Ensures that we reset potentially long-lived PreContext objects between multiplexs. We reset at this point
# since all parsing and validation will be done by this point, and a GraphQL::Query::Context will exist.
pre_context.reset
yield
super
end

def analyze_query(query:, &block)
return yield if @skip_tracing
capture_analysis_time(query.context, &block)
def analyze_query(query:)
return super if @skip_tracing
capture_analysis_time(query.context) { super }
end

def execute_query(query:, &block)
return yield if @skip_tracing
capture_query_start_time(query.context, &block)
def execute_query(query:)
return super if @skip_tracing
capture_query_start_time(query.context) { super }
end

def execute_field(field:, query:, ast_node:, arguments:, object:, &block)
return yield if @skip_tracing || query.context[SKIP_FIELD_AND_ARGUMENT_METRICS]
return yield unless GraphQL::Metrics.timings_capture_enabled?(query.context)
trace_field(GraphQL::Metrics::INLINE_FIELD_TIMINGS, query, &block)
def execute_field(field:, query:, ast_node:, arguments:, object:)
return super if @skip_tracing || query.context[SKIP_FIELD_AND_ARGUMENT_METRICS]
return super unless GraphQL::Metrics.timings_capture_enabled?(query.context)
trace_field(GraphQL::Metrics::INLINE_FIELD_TIMINGS, query) { super }
end

def execute_field_lazy(field:, query:, ast_node:, arguments:, object:, &block)
return yield if @skip_tracing || query.context[SKIP_FIELD_AND_ARGUMENT_METRICS]
return yield unless GraphQL::Metrics.timings_capture_enabled?(query.context)
trace_field(GraphQL::Metrics::LAZY_FIELD_TIMINGS, query, &block)
def execute_field_lazy(field:, query:, ast_node:, arguments:, object:)
return super if @skip_tracing || query.context[SKIP_FIELD_AND_ARGUMENT_METRICS]
return super unless GraphQL::Metrics.timings_capture_enabled?(query.context)
trace_field(GraphQL::Metrics::LAZY_FIELD_TIMINGS, query) { super }
end

private
Expand Down
21 changes: 21 additions & 0 deletions test/unit/graphql/metrics/trace_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def store_metrics(context_key, metrics)
end
end

module TestTrace
def execute_multiplex(multiplex:)
multiplex.context[:test_trace_was_executed] = true
super
end
end

class SchemaWithFullMetrics < GraphQL::Schema
query QueryRoot
mutation MutationRoot
Expand All @@ -85,6 +92,7 @@ class SchemaWithFullMetrics < GraphQL::Schema

instrument :query, GraphQL::Metrics::Instrumentation.new
query_analyzer SimpleAnalyzer
trace_with TestTrace
trace_with GraphQL::Metrics::Trace

def self.parse_error(err, _context)
Expand All @@ -93,6 +101,19 @@ def self.parse_error(err, _context)
end
end

test "it doesn't short-circuit other traces" do
context = {}
query = GraphQL::Query.new(
SchemaWithFullMetrics,
kitchen_sink_query_document,
variables: { 'postId': '1', 'titleUpcase': true },
operation_name: 'PostDetails',
context: context
)
result = query.result.to_h
assert query.multiplex.context[:test_trace_was_executed]
end

test 'extracts metrics from queries, as well as their fields and arguments (when using Query#result)' do
context = {}
query = GraphQL::Query.new(
Expand Down

0 comments on commit 805d496

Please sign in to comment.