Skip to content

Commit f6eae8f

Browse files
committed
fix(grpc): Allow gRPC connections via Unix socket
This commit addresses issue open-telemetry#1832. The way `NET_PEER_IP` and `NET_PEER_PORT` are retrieved raises a `ValueError` when gRPC connections are handled via Unix sockets. ```py ip, port = ( context.peer().split(",")[0].split(":", 1)[1].rsplit(":", 1) ) ``` When using an address like `unix:///tmp/grpc.sock` the value of `context.peer()` is `"unix:"`. Substituting that in the function above... ```py ip, port = "unix:".split(",")[0].split(":", 1)[1].rsplit(":", 1) ip, port = ["unix:"][0].split(":", 1)[1].rsplit(":", 1) ip, port = "unix:".split(":", 1)[1].rsplit(":", 1) ip, port = ["unix", ""][1].rsplit(":", 1) ip, port = "".rsplit(":", 1) ip, port = [""] # ValueError ``` I "addressed" the issue by guarding the retrieval of `net.peer.*` values under an `if` statement that checks if we are using a Unix socket. I extended the `server_interceptor` tests to run against TCP and Unix socket configurations. --- **Open Questions** - [ ] The socket tests will fail on Windows. Is there a way to annotate that? - [ ] Are there other span values we should be setting for the unix socket?
1 parent a679754 commit f6eae8f

File tree

2 files changed

+113
-123
lines changed

2 files changed

+113
-123
lines changed

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

+22-16
Original file line numberDiff line numberDiff line change
@@ -250,24 +250,30 @@ def _start_span(
250250
# * ipv4:127.0.0.1:57284
251251
# * ipv4:10.2.1.1:57284,127.0.0.1:57284
252252
#
253-
try:
254-
ip, port = (
255-
context.peer().split(",")[0].split(":", 1)[1].rsplit(":", 1)
256-
)
257-
ip = unquote(ip)
258-
attributes.update(
259-
{
260-
SpanAttributes.NET_PEER_IP: ip,
261-
SpanAttributes.NET_PEER_PORT: port,
262-
}
263-
)
253+
if context.peer() != "unix:":
254+
try:
255+
ip, port = (
256+
context.peer()
257+
.split(",")[0]
258+
.split(":", 1)[1]
259+
.rsplit(":", 1)
260+
)
261+
ip = unquote(ip)
262+
attributes.update(
263+
{
264+
SpanAttributes.NET_PEER_IP: ip,
265+
SpanAttributes.NET_PEER_PORT: port,
266+
}
267+
)
264268

265-
# other telemetry sources add this, so we will too
266-
if ip in ("[::1]", "127.0.0.1"):
267-
attributes[SpanAttributes.NET_PEER_NAME] = "localhost"
269+
# other telemetry sources add this, so we will too
270+
if ip in ("[::1]", "127.0.0.1"):
271+
attributes[SpanAttributes.NET_PEER_NAME] = "localhost"
268272

269-
except IndexError:
270-
logger.warning("Failed to parse peer address '%s'", context.peer())
273+
except IndexError:
274+
logger.warning(
275+
"Failed to parse peer address '%s'", context.peer()
276+
)
271277

272278
return self._tracer.start_as_current_span(
273279
name=handler_call_details.method,

0 commit comments

Comments
 (0)