22from unittest .mock import MagicMock
33
44import pytest
5+ from opentelemetry .semconv ._incubating .attributes .graphql_attributes import (
6+ GRAPHQL_DOCUMENT ,
7+ GRAPHQL_OPERATION_NAME ,
8+ GRAPHQL_OPERATION_TYPE ,
9+ GraphqlOperationTypeValues ,
10+ )
511from opentelemetry .trace import SpanKind
612from pytest_mock import MockerFixture
713
@@ -66,12 +72,14 @@ async def test_opentelemetry_sync_uses_global_tracer(global_tracer_mock):
6672def _instrumentation_stages (mocker , query ):
6773 return [
6874 mocker .call ("GraphQL Query" , kind = SpanKind .SERVER ),
69- mocker .call ().set_attribute ("component" , "graphql" ),
70- mocker .call ().set_attribute ("query" , query ),
75+ mocker .call ().set_attribute (GRAPHQL_DOCUMENT , query ),
7176 mocker .call ("GraphQL Parsing" , context = mocker .ANY ),
7277 mocker .call ().end (),
7378 mocker .call ("GraphQL Validation" , context = mocker .ANY ),
7479 mocker .call ().end (),
80+ mocker .call ().set_attribute (
81+ GRAPHQL_OPERATION_TYPE , GraphqlOperationTypeValues .QUERY .value
82+ ),
7583 mocker .call ().end (),
7684 ]
7785
@@ -101,7 +109,6 @@ async def test_open_tracing(global_tracer_mock, mocker):
101109 [
102110 mocker .call ("GraphQL Resolving: person" , context = mocker .ANY ),
103111 mocker .call ().__enter__ (),
104- mocker .call ().__enter__ ().set_attribute ("component" , "graphql" ),
105112 mocker .call ().__enter__ ().set_attribute ("graphql.parentType" , "Query" ),
106113 mocker .call ().__enter__ ().set_attribute ("graphql.path" , "person" ),
107114 mocker .call ().__exit__ (None , None , None ),
@@ -126,6 +133,7 @@ async def test_open_tracing_uses_operation_name(global_tracer_mock, mocker):
126133 [
127134 # if operation_name is supplied it is added to this span's tag
128135 mocker .call ("GraphQL Query: Example" , kind = SpanKind .SERVER ),
136+ mocker .call ().set_attribute (GRAPHQL_OPERATION_NAME , "Example" ),
129137 * _instrumentation_stages (mocker , query )[1 :],
130138 ]
131139 )
@@ -154,12 +162,16 @@ def generate_trace(*args: str, **kwargs: Any):
154162
155163 await schema .execute (query )
156164
165+ # if operation_name is in the query, it is added to this span's name
157166 tracers [0 ].update_name .assert_has_calls (
158167 [
159- # if operation_name is supplied it is added to this span's tag
160168 mocker .call ("GraphQL Query: Example" ),
161169 ]
162170 )
171+ # and the span's attributes
172+ tracers [0 ].set_attribute .assert_has_calls (
173+ [mocker .call (GRAPHQL_OPERATION_NAME , "Example" )]
174+ )
163175
164176
165177@pytest .mark .asyncio
0 commit comments