Skip to content

Commit 41aa5fe

Browse files
committed
Bring over PeerAddress parsing changes
1 parent 01186b1 commit 41aa5fe

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

Sources/GRPCInterceptors/Tracing/SpanAttributes+GRPCTracingKeys.swift

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
internal import GRPCCore
1818
internal import Tracing
1919

20-
public enum GRPCTracingKeys {
20+
enum GRPCTracingKeys {
2121
static let rpcSystem = "rpc.system"
2222
static let rpcMethod = "rpc.method"
2323
static let rpcService = "rpc.service"
@@ -87,7 +87,9 @@ package enum PeerAddress: Equatable {
8787
// - ipv4:<host>:<port> for ipv4 addresses
8888
// - ipv6:[<host>]:<port> for ipv6 addresses
8989
// - unix:<uds-pathname> for UNIX domain sockets
90-
let addressComponents = address.split(separator: ":", omittingEmptySubsequences: false)
90+
91+
// First get the first component so that we know what type of address we're dealing with
92+
let addressComponents = address.split(separator: ":", maxSplits: 1)
9193

9294
guard addressComponents.count > 1 else {
9395
// This is some unexpected/unknown format, so we have no way of splitting it up nicely.
@@ -98,32 +100,28 @@ package enum PeerAddress: Equatable {
98100
// Check what type the transport is...
99101
switch addressComponents[0] {
100102
case "ipv4":
101-
guard addressComponents.count == 3, let port = Int(addressComponents[2]) else {
103+
let ipv4AddressComponents = addressComponents[1].split(separator: ":")
104+
if ipv4AddressComponents.count == 2, let port = Int(ipv4AddressComponents[1]) {
105+
self = .ipv4(address: String(ipv4AddressComponents[0]), port: port)
106+
} else {
102107
// This is some unexpected/unknown format, so we have no way of splitting it up nicely.
103108
self = .other(address)
104-
return
105109
}
106-
self = .ipv4(address: String(addressComponents[1]), port: port)
107110

108111
case "ipv6":
109-
guard addressComponents.count > 2, let port = Int(addressComponents.last!) else {
112+
// At this point, we are looking at an address with format: [<address>]:<port>
113+
// We drop the first character ('[') and split by ']:' to keep two components: the address
114+
// and the port.
115+
let ipv6AddressComponents = addressComponents[1].dropFirst().split(separator: "]:")
116+
if ipv6AddressComponents.count == 2, let port = Int(ipv6AddressComponents[1]) {
117+
self = .ipv6(address: String(ipv6AddressComponents[0]), port: port)
118+
} else {
110119
// This is some unexpected/unknown format, so we have no way of splitting it up nicely.
111120
self = .other(address)
112-
return
113121
}
114-
self = .ipv6(
115-
address: String(
116-
addressComponents[1 ..< addressComponents.count - 1].joined(separator: ":")
117-
),
118-
port: port
119-
)
120122

121123
case "unix":
122-
guard addressComponents.count == 2 else {
123-
// This is some unexpected/unknown format, so we have no way of splitting it up nicely.
124-
self = .other(address)
125-
return
126-
}
124+
// Whatever comes after "unix:" is the <pathname>
127125
self = .unixDomainSocket(path: String(addressComponents[1]))
128126

129127
default:

Tests/GRPCInterceptorsTests/TracingInterceptorTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,8 @@ struct OTelTracingClientInterceptorTests {
440440

441441
case .ipv6:
442442
return OTelTracingInterceptorTestCaseValues(
443-
remotePeerAddress: "ipv6:2001::130F:::09C0:876A:130B:1234",
444-
localPeerAddress: "ipv6:ff06:0:0:0:0:0:0:c3:5678",
443+
remotePeerAddress: "ipv6:[2001::130F:::09C0:876A:130B]:1234",
444+
localPeerAddress: "ipv6:[ff06:0:0:0:0:0:0:c3]:5678",
445445
expectedSpanAttributes: [
446446
"rpc.system": "grpc",
447447
"rpc.method": .string(methodDescriptor.method),

0 commit comments

Comments
 (0)