Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/datadog/tracing/contrib/graphql/unified_trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ def execute_query(*args, query:, **kwargs)
trace(
proc { super },
'execute',
query.selected_operation_name,
operation_resource(query.selected_operation),
lambda { |span|
# Ensure this span can be aggregated by in the Datadog App, and generates RED metrics.
span.set_tag(Tracing::Metadata::Ext::TAG_KIND, Tracing::Metadata::Ext::SpanKind::TAG_SERVER)

span.set_tag('graphql.source', query.query_string)
span.set_tag('graphql.operation.type', query.selected_operation.operation_type)
if query.selected_operation_name
Expand Down Expand Up @@ -194,6 +197,14 @@ def multiplex_resource(multiplex)
end
end

def operation_resource(operation)
if operation.name
"#{operation.operation_type} #{operation.name}"
else
"anonymous"
end
end

# Create a Span Event for each error that occurs at query level.
#
# These are represented in the Datadog App as special GraphQL errors,
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/tracing/contrib/graphql/unified_trace.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ module Datadog

def add_query_error_events: (SpanOperation span, Array[::GraphQL::Error] errors) -> void

def operation_resource: (GraphQL::Language::Nodes::OperationDefinition operation) -> String

def serialize_error_locations: (Array[{"line" => Integer, "column" => Integer}] locations)-> Array[String]

def trace: (Proc callable, String trace_key, String resource, ?Hash[Symbol, traceKwargsValues ] kwargs, ?before: ^(SpanOperation)-> void, ?after: ^(SpanOperation)-> void) ?{ (SpanOperation) -> void } -> traceResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def unload_test_schema(prefix: '')
['graphql.analyze_multiplex', 'Users'],
['graphql.authorized', "#{prefix}TestGraphQLQuery.authorized"],
['graphql.authorized', "#{prefix}TestUser.authorized"],
['graphql.execute', 'Users'],
['graphql.execute', 'query Users'],
['graphql.execute_lazy', 'Users'],
['graphql.execute_multiplex', 'Users'],
if Gem::Version.new(GraphQL::VERSION) < Gem::Version.new('2.2')
Expand Down Expand Up @@ -144,6 +144,8 @@ def unload_test_schema(prefix: '')
# graphql.variables.* in graphql.execute span are the ones defined outside the query
# (variables part in JSON for example)
expect(span.get_tag('graphql.variables.var')).to eq(1)

expect(span.get_tag('span.kind')).to eq('server')
end

if name == 'graphql.resolve' && resource == 'TestGraphQLQuery.user'
Expand All @@ -163,7 +165,7 @@ def unload_test_schema(prefix: '')
expect(result.to_h['errors'][0]['message']).to eq('GraphQL error')
expect(result.to_h['data']).to eq('err1' => nil, 'err2' => nil)

expect(graphql_execute.resource).to eq('Error')
expect(graphql_execute.resource).to eq('query Error')
expect(graphql_execute.service).to eq(service)
expect(graphql_execute.type).to eq('graphql')

Expand All @@ -172,6 +174,8 @@ def unload_test_schema(prefix: '')
expect(graphql_execute.get_tag('graphql.operation.type')).to eq('query')
expect(graphql_execute.get_tag('graphql.operation.name')).to eq('Error')

expect(graphql_execute.get_tag('span.kind')).to eq('server')

expect(graphql_execute.events).to contain_exactly(
a_span_event_with(
name: 'dd.graphql.query.error',
Expand Down
5 changes: 5 additions & 0 deletions vendor/rbs/graphql/0/graphql.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ module GraphQL

class Argument
end

class OperationDefinition
def name: -> String?
def operation_type: -> Symbol?
end
end
end

Expand Down