Skip to content

Commit 370af56

Browse files
committed
fix: update GraphQL span name to follow semantic conventions
The semantic convention for GraphQL spans is specified here: https://opentelemetry.io/docs/specs/semconv/graphql/graphql-spans/. This will also resolve #560.
1 parent 92277bd commit 370af56

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

instrumentation/graphql/lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,15 @@ def analyze_query(query:, &block)
9898

9999
def execute_query(query:, &block)
100100
attributes = {}
101-
attributes['graphql.operation.name'] = query.selected_operation_name if query.selected_operation_name
102-
attributes['graphql.operation.type'] = query.selected_operation.operation_type
101+
operation_type = query.selected_operation.operation_type
102+
operation_name = query.selected_operation_name
103+
104+
attributes['graphql.operation.name'] = operation_name if operation_name
105+
attributes['graphql.operation.type'] = operation_type
103106
attributes['graphql.document'] = query.query_string
104107

105-
tracer.in_span('graphql.execute_query', attributes: attributes, &block)
108+
span_name = operation_name ? "#{operation_type} #{operation_name}" : operation_type
109+
tracer.in_span(span_name, attributes: attributes, &block)
106110
end
107111

108112
def execute_query_lazy(query:, multiplex:, &block)

instrumentation/graphql/lib/opentelemetry/instrumentation/graphql/tracers/graphql_tracer.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class GraphQLTracer < ::GraphQL::Tracing::PlatformTracing
2929
def platform_trace(platform_key, key, data)
3030
return yield if platform_key.nil?
3131

32-
tracer.in_span(platform_key, attributes: attributes_for(key, data)) do |span|
32+
tracer.in_span(span_name(platform_key, data), attributes: attributes_for(key, data)) do |span|
3333
yield.tap do |response|
3434
next unless key == 'validate'
3535

@@ -143,6 +143,18 @@ def attr_cache
143143
cache_h.compare_by_identity
144144
cache_h
145145
end
146+
147+
def span_name(key, data)
148+
case key
149+
when @platform_keys['execute_query']
150+
operation_type = data[:query].selected_operation.operation_type
151+
operation_name = data[:query].selected_operation_name
152+
153+
operation_name ? "#{operation_type} #{operation_name}" : operation_type
154+
else
155+
key
156+
end
157+
end
146158
end
147159
end
148160
end

instrumentation/graphql/test/instrumentation/graphql/tracers/graphql_trace_test.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
'graphql.validate',
5050
'graphql.analyze_query',
5151
'graphql.analyze_multiplex',
52-
'graphql.execute_query',
52+
'query',
5353
'graphql.execute_query_lazy',
5454
'graphql.execute_multiplex'
5555
]
@@ -76,7 +76,7 @@
7676

7777
SomeGraphQLAppSchema.execute('query SimpleQuery{ simpleField }')
7878

79-
span = spans.find { |s| s.name == 'graphql.execute_query' }
79+
span = spans.find { |s| s.name == 'query SimpleQuery' }
8080
_(span).wont_be_nil
8181
_(span.attributes.to_h).must_equal(expected_attributes)
8282
end
@@ -89,7 +89,7 @@
8989

9090
SomeGraphQLAppSchema.execute('{ simpleField }')
9191

92-
span = spans.find { |s| s.name == 'graphql.execute_query' }
92+
span = spans.find { |s| s.name == 'query' }
9393
_(span).wont_be_nil
9494
_(span.attributes.to_h).must_equal(expected_attributes)
9595
end

instrumentation/graphql/test/instrumentation/graphql/tracers/graphql_tracer_test.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
'graphql.validate',
5050
'graphql.analyze_query',
5151
'graphql.analyze_multiplex',
52-
'graphql.execute_query',
52+
'query',
5353
'graphql.execute_query_lazy',
5454
'graphql.execute_multiplex'
5555
]
@@ -75,7 +75,7 @@
7575

7676
SomeGraphQLAppSchema.execute('query SimpleQuery{ simpleField }')
7777

78-
span = spans.find { |s| s.name == 'graphql.execute_query' }
78+
span = spans.find { |s| s.name == 'query SimpleQuery' }
7979
_(span).wont_be_nil
8080
_(span.attributes.to_h).must_equal(expected_attributes)
8181
end
@@ -89,7 +89,7 @@
8989

9090
SomeGraphQLAppSchema.execute('{ simpleField }')
9191

92-
span = spans.find { |s| s.name == 'graphql.execute_query' }
92+
span = spans.find { |s| s.name == 'query' }
9393
_(span).wont_be_nil
9494
_(span.attributes.to_h).must_equal(expected_attributes)
9595
end

0 commit comments

Comments
 (0)