-
-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable automatic graphql tracing with a new
:graphql
patch (#2308)
- Loading branch information
1 parent
d0c9897
commit b3853e5
Showing
6 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -591,3 +591,4 @@ def utc_now | |
require "sentry/net/http" | ||
require "sentry/redis" | ||
require "sentry/puma" | ||
require "sentry/graphql" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
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.' } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
require 'spec_helper' | ||
|
||
with_graphql = begin | ||
require 'graphql' | ||
true | ||
rescue LoadError | ||
false | ||
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 | ||
if with_graphql | ||
describe 'with graphql gem' do | ||
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 | ||
|
||
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 | ||
else | ||
describe 'without graphql gem' do | ||
it 'logs warning' do | ||
string_io = StringIO.new | ||
|
||
perform_basic_setup do |config| | ||
config.enabled_patches << :graphql | ||
config.logger = Logger.new(string_io) | ||
end | ||
|
||
expect(string_io.string).to include('WARN -- sentry: 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.') | ||
end | ||
end | ||
end | ||
end | ||
end |