2
2
from unittest .mock import MagicMock
3
3
4
4
import pytest
5
+ from opentelemetry .semconv ._incubating .attributes .graphql_attributes import (
6
+ GRAPHQL_DOCUMENT ,
7
+ GRAPHQL_OPERATION_NAME ,
8
+ GRAPHQL_OPERATION_TYPE ,
9
+ GraphqlOperationTypeValues ,
10
+ )
5
11
from opentelemetry .trace import SpanKind
6
12
from pytest_mock import MockerFixture
7
13
@@ -66,12 +72,14 @@ async def test_opentelemetry_sync_uses_global_tracer(global_tracer_mock):
66
72
def _instrumentation_stages (mocker , query ):
67
73
return [
68
74
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 ),
71
76
mocker .call ("GraphQL Parsing" , context = mocker .ANY ),
72
77
mocker .call ().end (),
73
78
mocker .call ("GraphQL Validation" , context = mocker .ANY ),
74
79
mocker .call ().end (),
80
+ mocker .call ().set_attribute (
81
+ GRAPHQL_OPERATION_TYPE , GraphqlOperationTypeValues .QUERY .value
82
+ ),
75
83
mocker .call ().end (),
76
84
]
77
85
@@ -101,7 +109,6 @@ async def test_open_tracing(global_tracer_mock, mocker):
101
109
[
102
110
mocker .call ("GraphQL Resolving: person" , context = mocker .ANY ),
103
111
mocker .call ().__enter__ (),
104
- mocker .call ().__enter__ ().set_attribute ("component" , "graphql" ),
105
112
mocker .call ().__enter__ ().set_attribute ("graphql.parentType" , "Query" ),
106
113
mocker .call ().__enter__ ().set_attribute ("graphql.path" , "person" ),
107
114
mocker .call ().__exit__ (None , None , None ),
@@ -126,6 +133,7 @@ async def test_open_tracing_uses_operation_name(global_tracer_mock, mocker):
126
133
[
127
134
# if operation_name is supplied it is added to this span's tag
128
135
mocker .call ("GraphQL Query: Example" , kind = SpanKind .SERVER ),
136
+ mocker .call ().set_attribute (GRAPHQL_OPERATION_NAME , "Example" ),
129
137
* _instrumentation_stages (mocker , query )[1 :],
130
138
]
131
139
)
@@ -154,12 +162,16 @@ def generate_trace(*args: str, **kwargs: Any):
154
162
155
163
await schema .execute (query )
156
164
165
+ # if operation_name is in the query, it is added to this span's name
157
166
tracers [0 ].update_name .assert_has_calls (
158
167
[
159
- # if operation_name is supplied it is added to this span's tag
160
168
mocker .call ("GraphQL Query: Example" ),
161
169
]
162
170
)
171
+ # and the span's attributes
172
+ tracers [0 ].set_attribute .assert_has_calls (
173
+ [mocker .call (GRAPHQL_OPERATION_NAME , "Example" )]
174
+ )
163
175
164
176
165
177
@pytest .mark .asyncio
0 commit comments