-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #295 from DataDog/delner/add_graphql_configuration
Add GraphQL configuration
- Loading branch information
Showing
10 changed files
with
267 additions
and
8 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
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'ddtrace/ext/app_types' | ||
require 'ddtrace/ext/http' | ||
|
||
module Datadog | ||
module Contrib | ||
module GraphQL | ||
# Provides instrumentation for `graphql` through the GraphQL tracing framework | ||
module Patcher | ||
include Base | ||
register_as :graphql | ||
|
||
option :tracer, default: Datadog.tracer | ||
option :service_name, default: 'ruby-graphql', depends_on: [:tracer] do |value| | ||
get_option(:tracer).set_service_info(value, 'ruby-graphql', Ext::AppTypes::WEB) | ||
value | ||
end | ||
option :schemas | ||
|
||
class << self | ||
def patch | ||
return patched? if patched? || !compatible? || get_option(:schemas).nil? | ||
|
||
get_option(:schemas).each { |s| patch_schema!(s) } | ||
|
||
@patched = true | ||
end | ||
|
||
def patch_schema!(schema) | ||
tracer = get_option(:tracer) | ||
service_name = get_option(:service_name) | ||
|
||
schema.define do | ||
use( | ||
::GraphQL::Tracing::DataDogTracing, | ||
tracer: tracer, | ||
service: service_name | ||
) | ||
end | ||
end | ||
|
||
def patched? | ||
return @patched if defined?(@patched) | ||
@patched = false | ||
end | ||
|
||
private | ||
|
||
def compatible? | ||
defined?(::GraphQL) \ | ||
&& defined?(::GraphQL::Tracing::DataDogTracing) \ | ||
&& Gem.loaded_specs['graphql'].version >= Gem::Version.new('1.7.9') | ||
end | ||
end | ||
end | ||
end | ||
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,54 @@ | ||
LogHelpers.without_warnings do | ||
require 'graphql' | ||
end | ||
|
||
RSpec.shared_context 'GraphQL test schema' do | ||
let(:schema) do | ||
qt = query_type | ||
|
||
::GraphQL::Schema.define do | ||
query(qt) | ||
end | ||
end | ||
|
||
let(:query_type_name) { 'Query' } | ||
let(:query_type) do | ||
qtn = query_type_name | ||
ot = object_type | ||
oc = object_class | ||
|
||
::GraphQL::ObjectType.define do | ||
name qtn | ||
field ot.name.downcase.to_sym do | ||
type ot | ||
argument :id, !types.ID | ||
description 'Find an object by ID' | ||
resolve ->(_obj, args, _ctx) { oc.new(args['id']) } | ||
end | ||
end | ||
end | ||
|
||
let(:object_type_name) { 'Foo' } | ||
let(:object_type) do | ||
otn = object_type_name | ||
|
||
::GraphQL::ObjectType.define do | ||
name otn | ||
field :id, !types.ID | ||
field :name, types.String | ||
field :created_at, !types.String | ||
field :updated_at, !types.String | ||
end | ||
end | ||
|
||
let(:object_class) do | ||
Class.new do | ||
attr_accessor :id, :name | ||
|
||
def initialize(id, name = 'bar') | ||
@id = id | ||
@name = name | ||
end | ||
end | ||
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,73 @@ | ||
require 'spec_helper' | ||
require 'ddtrace/contrib/graphql/test_types' | ||
|
||
require 'ddtrace' | ||
|
||
# rubocop:disable Metrics/BlockLength | ||
RSpec.describe 'GraphQL patcher' do | ||
include_context 'GraphQL test schema' | ||
|
||
# GraphQL generates tons of warnings. | ||
# This suppresses those warnings. | ||
around(:each) do |example| | ||
without_warnings do | ||
example.run | ||
end | ||
end | ||
|
||
let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } | ||
|
||
def pop_spans | ||
tracer.writer.spans(:keep) | ||
end | ||
|
||
let(:all_spans) { pop_spans } | ||
let(:root_span) { all_spans.find { |s| s.parent.nil? } } | ||
|
||
before(:each) do | ||
Datadog.configure do |c| | ||
c.use :graphql, | ||
service_name: 'graphql-test', | ||
tracer: tracer, | ||
schemas: [schema] | ||
end | ||
end | ||
|
||
describe 'query trace' do | ||
subject(:result) { schema.execute(query, variables: {}, context: {}, operation_name: nil) } | ||
|
||
let(:query) { '{ foo(id: 1) { name } }' } | ||
let(:variables) { {} } | ||
let(:context) { {} } | ||
let(:operation_name) { nil } | ||
|
||
it do | ||
# Expect no errors | ||
expect(result.to_h['errors']).to be nil | ||
|
||
# Expect nine spans | ||
expect(all_spans).to have(9).items | ||
|
||
# List of valid resource names | ||
# (If this is too brittle, revist later.) | ||
valid_resource_names = [ | ||
'Query.foo', | ||
'analyze.graphql', | ||
'execute.graphql', | ||
'lex.graphql', | ||
'parse.graphql', | ||
'validate.graphql' | ||
] | ||
|
||
# Expect root span to be 'execute.graphql' | ||
expect(root_span.name).to eq('execute.graphql') | ||
expect(root_span.resource).to eq('execute.graphql') | ||
|
||
# Expect each span to be properly named | ||
all_spans.each do |span| | ||
expect(span.service).to eq('graphql-test') | ||
expect(valid_resource_names).to include(span.resource.to_s) | ||
end | ||
end | ||
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
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,15 @@ | ||
module LogHelpers | ||
def without_warnings(&block) | ||
LogHelpers.without_warnings(&block) | ||
end | ||
|
||
def self.without_warnings | ||
v = $VERBOSE | ||
$VERBOSE = nil | ||
begin | ||
yield | ||
ensure | ||
$VERBOSE = v | ||
end | ||
end | ||
end |