Skip to content

refactor(grpc): replace SpanAttributes with semconv attributes #3540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
)
from opentelemetry.instrumentation.utils import is_instrumentation_enabled
from opentelemetry.propagate import inject
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.semconv._incubating.attributes.rpc_attributes import (
RPC_GRPC_STATUS_CODE,
)
from opentelemetry.trace.status import Status, StatusCode

logger = logging.getLogger(__name__)
Expand All @@ -34,7 +36,7 @@ def _unary_done_callback(span, code, details, response_hook):
def callback(call):
try:
span.set_attribute(
SpanAttributes.RPC_GRPC_STATUS_CODE,
RPC_GRPC_STATUS_CODE,
code.value[0],
)
if code != grpc.StatusCode.OK:
Expand Down Expand Up @@ -75,7 +77,7 @@ def propagate_trace_in_details(client_call_details: ClientCallDetails):
def add_error_details_to_span(span, exc):
if isinstance(exc, grpc.RpcError):
span.set_attribute(
SpanAttributes.RPC_GRPC_STATUS_CODE,
RPC_GRPC_STATUS_CODE,
exc.code().value[0],
)
span.set_status(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import grpc.aio
import wrapt

from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.semconv._incubating.attributes.rpc_attributes import (
RPC_GRPC_STATUS_CODE,
)

from ._server import OpenTelemetryServerInterceptor, _wrap_rpc_behavior
from ._utilities import _server_status
Expand All @@ -34,7 +36,7 @@ async def abort(self, code, details="", trailing_metadata=tuple()):
self._self_code = code
self._self_details = details
self._self_active_span.set_attribute(
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
RPC_GRPC_STATUS_CODE, code.value[0]
)
status = _server_status(code, details)
self._self_active_span.set_status(status)
Expand All @@ -44,7 +46,7 @@ def set_code(self, code):
self._self_code = code
details = self._self_details or code.value[1]
self._self_active_span.set_attribute(
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
RPC_GRPC_STATUS_CODE, code.value[0]
)
if code != grpc.StatusCode.OK:
status = _server_status(code, details)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@
from opentelemetry.instrumentation.utils import is_instrumentation_enabled
from opentelemetry.propagate import inject
from opentelemetry.propagators.textmap import Setter
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.semconv._incubating.attributes.rpc_attributes import (
RPC_GRPC_STATUS_CODE,
RPC_METHOD,
RPC_SERVICE,
RPC_SYSTEM,
)
from opentelemetry.trace.status import Status, StatusCode

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -87,10 +92,10 @@ def __init__(
def _start_span(self, method, **kwargs):
service, meth = method.lstrip("/").split("/", 1)
attributes = {
SpanAttributes.RPC_SYSTEM: "grpc",
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
SpanAttributes.RPC_METHOD: meth,
SpanAttributes.RPC_SERVICE: service,
RPC_SYSTEM: "grpc",
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
RPC_METHOD: meth,
RPC_SERVICE: service,
}

return self._tracer.start_as_current_span(
Expand Down Expand Up @@ -153,7 +158,7 @@ def _intercept(self, request, metadata, client_info, invoker):
except Exception as exc:
if isinstance(exc, grpc.RpcError):
span.set_attribute(
SpanAttributes.RPC_GRPC_STATUS_CODE,
RPC_GRPC_STATUS_CODE,
exc.code().value[0],
)
span.set_status(
Expand Down Expand Up @@ -211,9 +216,7 @@ def _intercept_server_stream(
yield from invoker(request_or_iterator, metadata)
except grpc.RpcError as err:
span.set_status(Status(StatusCode.ERROR))
span.set_attribute(
SpanAttributes.RPC_GRPC_STATUS_CODE, err.code().value[0]
)
span.set_attribute(RPC_GRPC_STATUS_CODE, err.code().value[0])
raise err

def intercept_stream(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@
from opentelemetry import trace
from opentelemetry.context import attach, detach
from opentelemetry.propagate import extract
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.semconv._incubating.attributes.net_attributes import (
NET_PEER_IP,
NET_PEER_NAME,
NET_PEER_PORT,
)
from opentelemetry.semconv._incubating.attributes.rpc_attributes import (
RPC_GRPC_STATUS_CODE,
RPC_METHOD,
RPC_SERVICE,
RPC_SYSTEM,
)

from ._utilities import _server_status

Expand Down Expand Up @@ -122,9 +132,7 @@ def trailing_metadata(self):
def abort(self, code, details):
self._code = code
self._details = details
self._active_span.set_attribute(
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
)
self._active_span.set_attribute(RPC_GRPC_STATUS_CODE, code.value[0])
status = _server_status(code, details)
self._active_span.set_status(status)
return self._servicer_context.abort(code, details)
Expand All @@ -151,9 +159,7 @@ def set_code(self, code):
self._code = code
# use details if we already have it, otherwise the status description
details = self._details or code.value[1]
self._active_span.set_attribute(
SpanAttributes.RPC_GRPC_STATUS_CODE, code.value[0]
)
self._active_span.set_attribute(RPC_GRPC_STATUS_CODE, code.value[0])
if code != grpc.StatusCode.OK:
status = _server_status(code, details)
self._active_span.set_status(status)
Expand Down Expand Up @@ -212,8 +218,8 @@ def _start_span(
):
# standard attributes
attributes = {
SpanAttributes.RPC_SYSTEM: "grpc",
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
RPC_SYSTEM: "grpc",
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
}

# if we have details about the call, split into service and method
Expand All @@ -223,8 +229,8 @@ def _start_span(
)
attributes.update(
{
SpanAttributes.RPC_METHOD: method,
SpanAttributes.RPC_SERVICE: service,
RPC_METHOD: method,
RPC_SERVICE: service,
}
)

Expand All @@ -250,14 +256,14 @@ def _start_span(
ip = unquote(ip)
attributes.update(
{
SpanAttributes.NET_PEER_IP: ip,
SpanAttributes.NET_PEER_PORT: port,
NET_PEER_IP: ip,
NET_PEER_PORT: port,
}
)

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

except IndexError:
logger.warning(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
)
from opentelemetry.instrumentation.utils import suppress_instrumentation
from opentelemetry.propagate import get_global_textmap, set_global_textmap
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.semconv._incubating.attributes.rpc_attributes import (
RPC_GRPC_STATUS_CODE,
RPC_METHOD,
RPC_SERVICE,
RPC_SYSTEM,
)
from opentelemetry.test.mock_textmap import MockTextMapPropagator
from opentelemetry.test.test_base import TestBase

Expand Down Expand Up @@ -121,12 +126,10 @@ async def test_unary_unary(self):
self.assertSpanHasAttributes(
span,
{
SpanAttributes.RPC_METHOD: "SimpleMethod",
SpanAttributes.RPC_SERVICE: "GRPCTestServer",
SpanAttributes.RPC_SYSTEM: "grpc",
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[
0
],
RPC_METHOD: "SimpleMethod",
RPC_SERVICE: "GRPCTestServer",
RPC_SYSTEM: "grpc",
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
},
)

Expand All @@ -149,12 +152,10 @@ async def test_unary_stream(self):
self.assertSpanHasAttributes(
span,
{
SpanAttributes.RPC_METHOD: "ServerStreamingMethod",
SpanAttributes.RPC_SERVICE: "GRPCTestServer",
SpanAttributes.RPC_SYSTEM: "grpc",
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[
0
],
RPC_METHOD: "ServerStreamingMethod",
RPC_SERVICE: "GRPCTestServer",
RPC_SYSTEM: "grpc",
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
},
)

Expand All @@ -177,12 +178,10 @@ async def test_stream_unary(self):
self.assertSpanHasAttributes(
span,
{
SpanAttributes.RPC_METHOD: "ClientStreamingMethod",
SpanAttributes.RPC_SERVICE: "GRPCTestServer",
SpanAttributes.RPC_SYSTEM: "grpc",
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[
0
],
RPC_METHOD: "ClientStreamingMethod",
RPC_SERVICE: "GRPCTestServer",
RPC_SYSTEM: "grpc",
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
},
)

Expand All @@ -207,12 +206,10 @@ async def test_stream_stream(self):
self.assertSpanHasAttributes(
span,
{
SpanAttributes.RPC_METHOD: "BidirectionalStreamingMethod",
SpanAttributes.RPC_SERVICE: "GRPCTestServer",
SpanAttributes.RPC_SYSTEM: "grpc",
SpanAttributes.RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[
0
],
RPC_METHOD: "BidirectionalStreamingMethod",
RPC_SERVICE: "GRPCTestServer",
RPC_SYSTEM: "grpc",
RPC_GRPC_STATUS_CODE: grpc.StatusCode.OK.value[0],
},
)

Expand Down
Loading