Skip to content

Commit

Permalink
Enable automatic graphql tracing with a new :graphql patch
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed May 7, 2024
1 parent 09fa602 commit b2e4bcc
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sentry-ruby/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ gem "puma"
gem "timecop"
gem "stackprof" unless RUBY_PLATFORM == "java"

gem "graphql", ">= 2.2.6"

gem "benchmark-ips"
gem "benchmark_driver"
gem "benchmark-ipsa"
Expand Down
1 change: 1 addition & 0 deletions sentry-ruby/lib/sentry-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,4 @@ def utc_now
require "sentry/net/http"
require "sentry/redis"
require "sentry/puma"
require "sentry/graphql"
9 changes: 9 additions & 0 deletions sentry-ruby/lib/sentry/graphql.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

Sentry.register_patch(:graphql) do |config|
if defined?(::GraphQL::Schema) && defined?(::GraphQL::Tracing::SentryTrace) && ::GraphQL::Schema.respond_to?(:trace_with)
::GraphQL::Schema.trace_with(::GraphQL::Tracing::SentryTrace, set_transaction_name: true)

Check warning on line 5 in sentry-ruby/lib/sentry/graphql.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/graphql.rb#L4-L5

Added lines #L4 - L5 were not covered by tests
else
config.logger.warn(Sentry::LOGGER_PROGNAME) { 'You tried to enable the GraphQL integration but no GraphQL gem was detected. Make sure you have the `graphql` gem (>= 2.2.6) in your Gemfile.' }

Check warning on line 7 in sentry-ruby/lib/sentry/graphql.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/graphql.rb#L7

Added line #L7 was not covered by tests
end
end
56 changes: 56 additions & 0 deletions sentry-ruby/spec/sentry/graphql_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'spec_helper'
require 'graphql'

class Thing < GraphQL::Schema::Object
field :str, String
def str; 'blah'; end
end

class Query < GraphQL::Schema::Object
field :int, Integer, null: false
def int; 1; end

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

class MySchema < GraphQL::Schema
query(Query)
end


RSpec.describe 'GraphQL' do
it 'adds the graphql patch to registered patches' do
expect(Sentry.registered_patches.keys).to include(:graphql)
end

context 'when patch enabled' do
before do
perform_basic_setup do |config|
config.traces_sample_rate = 1.0
config.enabled_patches << :graphql
end
end

it 'enables the sentry tracer' do
expect(MySchema.trace_modules_for(:default)).to include(::GraphQL::Tracing::SentryTrace)
end

it 'adds graphql spans to the transaction' do
transaction = Sentry.start_transaction
Sentry.get_current_scope.set_span(transaction)
MySchema.execute('query foobar { int thing { str } }')
transaction.finish

expect(last_sentry_event.transaction).to eq('GraphQL/query.foobar')

execute_span = last_sentry_event.spans.find { |s| s[:op] == 'graphql.execute' }
expect(execute_span[:description]).to eq('query foobar')
expect(execute_span[:data]).to eq({
'graphql.document'=>'query foobar { int thing { str } }',
'graphql.operation.name'=>'foobar',
'graphql.operation.type'=>'query'
})
end
end
end

0 comments on commit b2e4bcc

Please sign in to comment.