lib/opentelemetry/instrumentation/graphql/instrumentation.rb in opentelemetry-instrumentation-graphql-0.25.0 vs lib/opentelemetry/instrumentation/graphql/instrumentation.rb in opentelemetry-instrumentation-graphql-0.26.0

- old
+ new

@@ -10,23 +10,35 @@ module Instrumentation module GraphQL # The Instrumentation class contains logic to detect and install the GraphQL instrumentation class Instrumentation < OpenTelemetry::Instrumentation::Base compatible do - Gem::Requirement.new('<= 2.0.17').satisfied_by?(gem_version) || - Gem::Requirement.new('~> 2.0.19').satisfied_by?(gem_version) + if config[:legacy_tracing] + legacy_tracing_requirement_satisfied? + else + Gem::Requirement.new('>= 2.0.18', '< 3.0.0').satisfied_by?(gem_version) + end end install do |config| - require_dependencies - install_tracer(config) + if config[:legacy_tracing] + require_relative 'tracers/graphql_tracer' + install_tracer(config) + else + require_relative 'tracers/graphql_trace' + install_new_tracer(config) + end end present do defined?(::GraphQL) end + def legacy_tracing_requirement_satisfied? + Gem::Requirement.new('<= 2.0.17').satisfied_by?(gem_version) || Gem::Requirement.new('~> 2.0.19').satisfied_by?(gem_version) + end + ## Supported configuration keys for the install config hash: # # The enable_platform_field key expects a boolean value, # and enables the tracing of "execute_field" and "execute_field_lazy". # @@ -47,26 +59,35 @@ option :schemas, default: [], validate: :array option :enable_platform_field, default: false, validate: :boolean option :enable_platform_authorized, default: false, validate: :boolean option :enable_platform_resolve_type, default: false, validate: :boolean option :legacy_platform_span_names, default: false, validate: :boolean + option :legacy_tracing, default: false, validate: :boolean private def gem_version Gem::Version.new(::GraphQL::VERSION) end - def require_dependencies - require_relative 'tracers/graphql_tracer' - end - def install_tracer(config = {}) if config[:schemas].empty? ::GraphQL::Schema.tracer(Tracers::GraphQLTracer.new) else config[:schemas].each do |schema| schema.use(Tracers::GraphQLTracer) + rescue StandardError => e + OpenTelemetry.logger.error("Unable to patch schema #{schema}: #{e.message}") + end + end + end + + def install_new_tracer(config = {}) + if config[:schemas].empty? + ::GraphQL::Schema.trace_with(Tracers::GraphQLTrace) + else + config[:schemas].each do |schema| + schema.trace_with(Tracers::GraphQLTrace) rescue StandardError => e OpenTelemetry.logger.error("Unable to patch schema #{schema}: #{e.message}") end end end