lib/graphql/metrics/trace.rb in graphql-metrics-5.0.5 vs lib/graphql/metrics/trace.rb in graphql-metrics-5.0.6

- old
+ new

@@ -3,22 +3,23 @@ module GraphQL module Metrics module Trace def initialize(**_rest) super - context = (@multiplex || @query).context - @skip_tracing = context&.fetch(GraphQL::Metrics::SKIP_GRAPHQL_METRICS_ANALYSIS, false) + + query_or_multiplex = @query || @multiplex + @skip_tracing = query_or_multiplex.context&.fetch(SKIP_GRAPHQL_METRICS_ANALYSIS, false) if query_or_multiplex end # NOTE: These methods come from the graphql ruby gem and are in "chronological" order based on the phases # of execution of the graphql-ruby gem, though old versions of the gem aren't always consistent about this (see # https://github.com/rmosolgo/graphql-ruby/issues/3393). Most of them can be run multiple times when # multiplexing multiple queries. # wraps everything below this line; only run once def execute_multiplex(multiplex:) - return super if @skip_tracing + return super if skip_tracing?(multiplex) capture_multiplex_start_time { super } end # may not trigger if the query is passed in pre-parsed def lex(query_string:) @@ -31,45 +32,53 @@ return super if @skip_tracing capture_parsing_time { super } end def validate(query:, validate:) - return super if @skip_tracing + return super if skip_tracing?(query) capture_validation_time(query.context) { super } end # wraps all `analyze_query`s; only run once def analyze_multiplex(multiplex:) - return super if @skip_tracing + return super if skip_tracing?(multiplex) # 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 super end def analyze_query(query:) - return super if @skip_tracing + return super if skip_tracing?(query) capture_analysis_time(query.context) { super } end def execute_query(query:) - return super if @skip_tracing + return super if skip_tracing?(query) capture_query_start_time(query.context) { super } end def execute_field(field:, query:, ast_node:, arguments:, object:) - return super if @skip_tracing || query.context[SKIP_FIELD_AND_ARGUMENT_METRICS] + return super if skip_tracing?(query) || 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:) - return super if @skip_tracing || query.context[SKIP_FIELD_AND_ARGUMENT_METRICS] + return super if skip_tracing?(query) || 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 + + def skip_tracing?(query_or_multiplex) + if !defined?(@skip_tracing) + @skip_tracing = query_or_multiplex.context&.fetch(SKIP_GRAPHQL_METRICS_ANALYSIS, false) + end + + @skip_tracing + end PreContext = Struct.new( :multiplex_start_time, :multiplex_start_time_monotonic, :parsing_start_time_offset,