Skip to content

Commit ce5814f

Browse files
committed
refactor(grpc): replace SpanAttributes with semconv attributes
1 parent c1a6895 commit ce5814f

File tree

10 files changed

+261
-199
lines changed

10 files changed

+261
-199
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 & 7 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_SYSTEM,
36+
RPC_GRPC_STATUS_CODE,
37+
RPC_METHOD,
38+
RPC_SERVICE,
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(
@@ -212,7 +217,7 @@ def _intercept_server_stream(
212217
except grpc.RpcError as err:
213218
span.set_status(Status(StatusCode.ERROR))
214219
span.set_attribute(
215-
SpanAttributes.RPC_GRPC_STATUS_CODE, err.code().value[0]
220+
RPC_GRPC_STATUS_CODE, err.code().value[0]
216221
)
217222
raise err
218223

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

Lines changed: 20 additions & 10 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.rpc_attributes import (
34+
RPC_GRPC_STATUS_CODE,
35+
RPC_METHOD,
36+
RPC_SERVICE,
37+
RPC_SYSTEM,
38+
)
39+
from opentelemetry.semconv._incubating.attributes.net_attributes import (
40+
NET_PEER_IP,
41+
NET_PEER_NAME,
42+
NET_PEER_PORT,
43+
)
3444

3545
from ._utilities import _server_status
3646

@@ -123,7 +133,7 @@ def abort(self, code, details):
123133
self._code = code
124134
self._details = details
125135
self._active_span.set_attribute(
126-
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
136+
RPC_GRPC_STATUS_CODE, code.value[0]
127137
)
128138
status = _server_status(code, details)
129139
self._active_span.set_status(status)
@@ -152,7 +162,7 @@ def set_code(self, code):
152162
# use details if we already have it, otherwise the status description
153163
details = self._details or code.value[1]
154164
self._active_span.set_attribute(
155-
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
165+
RPC_GRPC_STATUS_CODE, code.value[0]
156166
)
157167
if code != grpc.StatusCode.OK:
158168
status = _server_status(code, details)
@@ -212,8 +222,8 @@ def _start_span(
212222
):
213223
# standard attributes
214224
attributes = {
215-
SpanAttributes.RPC_SYSTEM: "grpc",
216-
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
225+
RPC_SYSTEM: "grpc",
226+
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
217227
}
218228

219229
# if we have details about the call, split into service and method
@@ -223,8 +233,8 @@ def _start_span(
223233
)
224234
attributes.update(
225235
{
226-
SpanAttributes.RPC_METHOD: method,
227-
SpanAttributes.RPC_SERVICE: service,
236+
RPC_METHOD: method,
237+
RPC_SERVICE: service,
228238
}
229239
)
230240

@@ -250,14 +260,14 @@ def _start_span(
250260
ip = unquote(ip)
251261
attributes.update(
252262
{
253-
SpanAttributes.NET_PEER_IP: ip,
254-
SpanAttributes.NET_PEER_PORT: port,
263+
NET_PEER_IP: ip,
264+
NET_PEER_PORT: port,
255265
}
256266
)
257267

258268
# other telemetry sources add this, so we will too
259269
if ip in ("[::1]", "127.0.0.1"):
260-
attributes[SpanAttributes.NET_PEER_NAME] = "localhost"
270+
attributes[NET_PEER_NAME] = "localhost"
261271

262272
except IndexError:
263273
logger.warning(

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

Lines changed: 22 additions & 17 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_METHOD,
31+
RPC_SERVICE,
32+
RPC_SYSTEM,
33+
RPC_GRPC_STATUS_CODE,
34+
)
3035
from opentelemetry.test.mock_textmap import MockTextMapPropagator
3136
from opentelemetry.test.test_base import TestBase
3237

@@ -121,10 +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[
129+
RPC_METHOD: "SimpleMethod",
130+
RPC_SERVICE: "GRPCTestServer",
131+
RPC_SYSTEM: "grpc",
132+
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[
128133
0
129134
],
130135
},
@@ -149,10 +154,10 @@ async def test_unary_stream(self):
149154
self.assertSpanHasAttributes(
150155
span,
151156
{
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[
157+
RPC_METHOD: "ServerStreamingMethod",
158+
RPC_SERVICE: "GRPCTestServer",
159+
RPC_SYSTEM: "grpc",
160+
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[
156161
0
157162
],
158163
},
@@ -177,10 +182,10 @@ async def test_stream_unary(self):
177182
self.assertSpanHasAttributes(
178183
span,
179184
{
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[
185+
RPC_METHOD: "ClientStreamingMethod",
186+
RPC_SERVICE: "GRPCTestServer",
187+
RPC_SYSTEM: "grpc",
188+
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[
184189
0
185190
],
186191
},
@@ -207,10 +212,10 @@ async def test_stream_stream(self):
207212
self.assertSpanHasAttributes(
208213
span,
209214
{
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[
215+
RPC_METHOD: "BidirectionalStreamingMethod",
216+
RPC_SERVICE: "GRPCTestServer",
217+
RPC_SYSTEM: "grpc",
218+
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[
214219
0
215220
],
216221
},

0 commit comments

Comments
 (0)