Skip to content

Commit 85ea8f3

Browse files
artemziborevxrmx
andauthored
refactor(grpc): replace SpanAttributes with semconv attributes (#3540)
* refactor(grpc): replace SpanAttributes with semconv attributes * refactor(flask): replace SpanAttributes with semconv attributes (fix ruff linting) --------- Co-authored-by: Riccardo Magliocchetti <[email protected]>
1 parent 8f7bab5 commit 85ea8f3

File tree

10 files changed

+260
-273
lines changed

10 files changed

+260
-273
lines changed

instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_aio_client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
)
2525
from opentelemetry.instrumentation.utils import is_instrumentation_enabled
2626
from opentelemetry.propagate import inject
27-
from opentelemetry.semconv.trace import SpanAttributes
27+
from opentelemetry.semconv._incubating.attributes.rpc_attributes import (
28+
RPC_GRPC_STATUS_CODE,
29+
)
2830
from opentelemetry.trace.status import Status, StatusCode
2931

3032
logger = logging.getLogger(__name__)
@@ -34,7 +36,7 @@ def _unary_done_callback(span, code, details, response_hook):
3436
def callback(call):
3537
try:
3638
span.set_attribute(
37-
SpanAttributes.RPC_GRPC_STATUS_CODE,
39+
RPC_GRPC_STATUS_CODE,
3840
code.value[0],
3941
)
4042
if code != grpc.StatusCode.OK:
@@ -75,7 +77,7 @@ def propagate_trace_in_details(client_call_details: ClientCallDetails):
7577
def add_error_details_to_span(span, exc):
7678
if isinstance(exc, grpc.RpcError):
7779
span.set_attribute(
78-
SpanAttributes.RPC_GRPC_STATUS_CODE,
80+
RPC_GRPC_STATUS_CODE,
7981
exc.code().value[0],
8082
)
8183
span.set_status(

instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_aio_server.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import grpc.aio
1717
import wrapt
1818

19-
from opentelemetry.semconv.trace import SpanAttributes
19+
from opentelemetry.semconv._incubating.attributes.rpc_attributes import (
20+
RPC_GRPC_STATUS_CODE,
21+
)
2022

2123
from ._server import OpenTelemetryServerInterceptor, _wrap_rpc_behavior
2224
from ._utilities import _server_status
@@ -34,7 +36,7 @@ async def abort(self, code, details="", trailing_metadata=tuple()):
3436
self._self_code = code
3537
self._self_details = details
3638
self._self_active_span.set_attribute(
37-
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
39+
RPC_GRPC_STATUS_CODE, code.value[0]
3840
)
3941
status = _server_status(code, details)
4042
self._self_active_span.set_status(status)
@@ -44,7 +46,7 @@ def set_code(self, code):
4446
self._self_code = code
4547
details = self._self_details or code.value[1]
4648
self._self_active_span.set_attribute(
47-
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
49+
RPC_GRPC_STATUS_CODE, code.value[0]
4850
)
4951
if code != grpc.StatusCode.OK:
5052
status = _server_status(code, details)

instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_client.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@
3131
from opentelemetry.instrumentation.utils import is_instrumentation_enabled
3232
from opentelemetry.propagate import inject
3333
from opentelemetry.propagators.textmap import Setter
34-
from opentelemetry.semconv.trace import SpanAttributes
34+
from opentelemetry.semconv._incubating.attributes.rpc_attributes import (
35+
RPC_GRPC_STATUS_CODE,
36+
RPC_METHOD,
37+
RPC_SERVICE,
38+
RPC_SYSTEM,
39+
)
3540
from opentelemetry.trace.status import Status, StatusCode
3641

3742
logger = logging.getLogger(__name__)
@@ -87,10 +92,10 @@ def __init__(
8792
def _start_span(self, method, **kwargs):
8893
service, meth = method.lstrip("/").split("/", 1)
8994
attributes = {
90-
SpanAttributes.RPC_SYSTEM: "grpc",
91-
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
92-
SpanAttributes.RPC_METHOD: meth,
93-
SpanAttributes.RPC_SERVICE: service,
95+
RPC_SYSTEM: "grpc",
96+
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
97+
RPC_METHOD: meth,
98+
RPC_SERVICE: service,
9499
}
95100

96101
return self._tracer.start_as_current_span(
@@ -153,7 +158,7 @@ def _intercept(self, request, metadata, client_info, invoker):
153158
except Exception as exc:
154159
if isinstance(exc, grpc.RpcError):
155160
span.set_attribute(
156-
SpanAttributes.RPC_GRPC_STATUS_CODE,
161+
RPC_GRPC_STATUS_CODE,
157162
exc.code().value[0],
158163
)
159164
span.set_status(
@@ -211,9 +216,7 @@ def _intercept_server_stream(
211216
yield from invoker(request_or_iterator, metadata)
212217
except grpc.RpcError as err:
213218
span.set_status(Status(StatusCode.ERROR))
214-
span.set_attribute(
215-
SpanAttributes.RPC_GRPC_STATUS_CODE, err.code().value[0]
216-
)
219+
span.set_attribute(RPC_GRPC_STATUS_CODE, err.code().value[0])
217220
raise err
218221

219222
def intercept_stream(

instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/_server.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,17 @@
3030
from opentelemetry import trace
3131
from opentelemetry.context import attach, detach
3232
from opentelemetry.propagate import extract
33-
from opentelemetry.semconv.trace import SpanAttributes
33+
from opentelemetry.semconv._incubating.attributes.net_attributes import (
34+
NET_PEER_IP,
35+
NET_PEER_NAME,
36+
NET_PEER_PORT,
37+
)
38+
from opentelemetry.semconv._incubating.attributes.rpc_attributes import (
39+
RPC_GRPC_STATUS_CODE,
40+
RPC_METHOD,
41+
RPC_SERVICE,
42+
RPC_SYSTEM,
43+
)
3444

3545
from ._utilities import _server_status
3646

@@ -122,9 +132,7 @@ def trailing_metadata(self):
122132
def abort(self, code, details):
123133
self._code = code
124134
self._details = details
125-
self._active_span.set_attribute(
126-
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
127-
)
135+
self._active_span.set_attribute(RPC_GRPC_STATUS_CODE, code.value[0])
128136
status = _server_status(code, details)
129137
self._active_span.set_status(status)
130138
return self._servicer_context.abort(code, details)
@@ -151,9 +159,7 @@ def set_code(self, code):
151159
self._code = code
152160
# use details if we already have it, otherwise the status description
153161
details = self._details or code.value[1]
154-
self._active_span.set_attribute(
155-
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
156-
)
162+
self._active_span.set_attribute(RPC_GRPC_STATUS_CODE, code.value[0])
157163
if code != grpc.StatusCode.OK:
158164
status = _server_status(code, details)
159165
self._active_span.set_status(status)
@@ -212,8 +218,8 @@ def _start_span(
212218
):
213219
# standard attributes
214220
attributes = {
215-
SpanAttributes.RPC_SYSTEM: "grpc",
216-
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
221+
RPC_SYSTEM: "grpc",
222+
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
217223
}
218224

219225
# if we have details about the call, split into service and method
@@ -223,8 +229,8 @@ def _start_span(
223229
)
224230
attributes.update(
225231
{
226-
SpanAttributes.RPC_METHOD: method,
227-
SpanAttributes.RPC_SERVICE: service,
232+
RPC_METHOD: method,
233+
RPC_SERVICE: service,
228234
}
229235
)
230236

@@ -250,14 +256,14 @@ def _start_span(
250256
ip = unquote(ip)
251257
attributes.update(
252258
{
253-
SpanAttributes.NET_PEER_IP: ip,
254-
SpanAttributes.NET_PEER_PORT: port,
259+
NET_PEER_IP: ip,
260+
NET_PEER_PORT: port,
255261
}
256262
)
257263

258264
# other telemetry sources add this, so we will too
259265
if ip in ("[::1]", "127.0.0.1"):
260-
attributes[SpanAttributes.NET_PEER_NAME] = "localhost"
266+
attributes[NET_PEER_NAME] = "localhost"
261267

262268
except IndexError:
263269
logger.warning(

instrumentation/opentelemetry-instrumentation-grpc/tests/test_aio_client_interceptor.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@
2626
)
2727
from opentelemetry.instrumentation.utils import suppress_instrumentation
2828
from opentelemetry.propagate import get_global_textmap, set_global_textmap
29-
from opentelemetry.semconv.trace import SpanAttributes
29+
from opentelemetry.semconv._incubating.attributes.rpc_attributes import (
30+
RPC_GRPC_STATUS_CODE,
31+
RPC_METHOD,
32+
RPC_SERVICE,
33+
RPC_SYSTEM,
34+
)
3035
from opentelemetry.test.mock_textmap import MockTextMapPropagator
3136
from opentelemetry.test.test_base import TestBase
3237

@@ -121,12 +126,10 @@ async def test_unary_unary(self):
121126
self.assertSpanHasAttributes(
122127
span,
123128
{
124-
SpanAttributes.RPC_METHOD: "SimpleMethod",
125-
SpanAttributes.RPC_SERVICE: "GRPCTestServer",
126-
SpanAttributes.RPC_SYSTEM: "grpc",
127-
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[
128-
0
129-
],
129+
RPC_METHOD: "SimpleMethod",
130+
RPC_SERVICE: "GRPCTestServer",
131+
RPC_SYSTEM: "grpc",
132+
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
130133
},
131134
)
132135

@@ -149,12 +152,10 @@ async def test_unary_stream(self):
149152
self.assertSpanHasAttributes(
150153
span,
151154
{
152-
SpanAttributes.RPC_METHOD: "ServerStreamingMethod",
153-
SpanAttributes.RPC_SERVICE: "GRPCTestServer",
154-
SpanAttributes.RPC_SYSTEM: "grpc",
155-
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[
156-
0
157-
],
155+
RPC_METHOD: "ServerStreamingMethod",
156+
RPC_SERVICE: "GRPCTestServer",
157+
RPC_SYSTEM: "grpc",
158+
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
158159
},
159160
)
160161

@@ -177,12 +178,10 @@ async def test_stream_unary(self):
177178
self.assertSpanHasAttributes(
178179
span,
179180
{
180-
SpanAttributes.RPC_METHOD: "ClientStreamingMethod",
181-
SpanAttributes.RPC_SERVICE: "GRPCTestServer",
182-
SpanAttributes.RPC_SYSTEM: "grpc",
183-
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[
184-
0
185-
],
181+
RPC_METHOD: "ClientStreamingMethod",
182+
RPC_SERVICE: "GRPCTestServer",
183+
RPC_SYSTEM: "grpc",
184+
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
186185
},
187186
)
188187

@@ -207,12 +206,10 @@ async def test_stream_stream(self):
207206
self.assertSpanHasAttributes(
208207
span,
209208
{
210-
SpanAttributes.RPC_METHOD: "BidirectionalStreamingMethod",
211-
SpanAttributes.RPC_SERVICE: "GRPCTestServer",
212-
SpanAttributes.RPC_SYSTEM: "grpc",
213-
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[
214-
0
215-
],
209+
RPC_METHOD: "BidirectionalStreamingMethod",
210+
RPC_SERVICE: "GRPCTestServer",
211+
RPC_SYSTEM: "grpc",
212+
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
216213
},
217214
)
218215

0 commit comments

Comments
 (0)