graphql-ruby: Multiple tracers in one schema are ignored

Describe the bug

This bug almost appears to be a duplicate of https://github.com/rmosolgo/graphql-ruby/issues/4542 but this time around it’s with the ActiveSupport and OpenTelemetry tracers.

module Graph
  class Schema < GraphQL::Schema
    ...
    
    trace_with GraphQL::Tracing::ActiveSupportNotificationsTrace  end
end

This schema, with the default OpenTelemetry setup, sees the OpenTelemetry tracer completely ignored as spans are not included in traces yet metrics are emitted.

If I mix and match the old tracer API, I’m able to get both working:

module Graph
  class Schema < GraphQL::Schema
    ...

    tracer GraphQL::Tracing::ActiveSupportNotificationsTracing
  end
end

Versions

graphql: 2.3.10 opentelemetry-instrumentation-graphql: 0.28.2 rails: 7.1.3.4

Steps to reproduce

The test case demonstrates the opposite to the above due to how our app is built, but I think it’s still sufficient to show the bug.

# frozen_string_literal: true

require "bundler/inline"

gemfile do
  gem "graphql", "2.3.10"
  gem 'opentelemetry-api'
  gem 'opentelemetry-common'
  gem 'opentelemetry-instrumentation-graphql'
  gem 'opentelemetry-sdk'
  gem 'rails'
end

module MultipleTracer
  class Thing < GraphQL::Schema::Object
    field :str, String

    def str; "blah"; end
  end

  class Named < GraphQL::Schema::Union
    possible_types Thing
    def self.resolve_type(obj, ctx)
      Thing
    end
  end

  class Query < GraphQL::Schema::Object
    include GraphQL::Types::Relay::HasNodeField

    field :int, Integer, null: false

    def int
      1
    end

    field :thing, Thing
    def thing; :thing; end

    field :named, Named, resolver_method: :thing
  end

  class TestSchema < GraphQL::Schema
    query(Query)

    trace_with(GraphQL::Tracing::ActiveSupportNotificationsTrace)
  end
end

# Comment from here.... 
ENV['OTEL_TRACES_EXPORTER'] = 'console'
OpenTelemetry::SDK.configure do |c|
  c.use_all('OpenTelemetry::Instrumentation::GraphQL' => { schemas: [MultipleTracer::TestSchema] })
end
# ...to here to demonstrate bug

ActiveSupport::Notifications.monotonic_subscribe("execute_query.graphql") do |name, start, finish, _message_id, data|
  puts "#{name} handled"
  puts "#{data.inspect}"
end

query = "{ int thing { str } named { ... on Thing { str } } }"
MultipleTracer::TestSchema.execute(query)

The console will be messy, but if you search for execute_query.graphql handled it will be absent. If these lines are commented out, and the command is re-run, you’ll see the ActiveSupport handled messages.

ENV['OTEL_TRACES_EXPORTER'] = 'console'
OpenTelemetry::SDK.configure do |c|
  c.use_all('OpenTelemetry::Instrumentation::GraphQL' => { schemas: [MultipleTracer::TestSchema] })
end
$ ruby inline.rb
execute_query.graphql handled
{:query=>query ...}

Expected behavior

Both tracers should be able to emit information about the GraphQL operations. So with the above example there should be a wall of trace info, as well as the two littles lines added by the ActiveSupport subscriber.

Actual behavior

The span information is emitted, but nothing from the ActiveSupport subscriber.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

If you get a chance to try master again, please let me know what you find 😅 Thanks again for your help tracking this down.

Ok, thanks for the update. I tinkered with the original script above to try to get it to fail, but I couldn’t…

I also reopened #5045 in case that’s the cause, and I pushed a fix for the issue spotted there. Could you try your app with that patch?

Nope, it’s more complicated than that … I’ve got a failing test over at https://github.com/rmosolgo/graphql-ruby/pull/5045, I have to massage the inheritance code to make it work 😅