Skip to content

Commit

Permalink
Bring over PeerAddress parsing changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gjcairo committed Jan 21, 2025
1 parent 01186b1 commit 3275f72
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
internal import GRPCCore
internal import Tracing

public enum GRPCTracingKeys {
enum GRPCTracingKeys {
static let rpcSystem = "rpc.system"
static let rpcMethod = "rpc.method"
static let rpcService = "rpc.service"
Expand Down Expand Up @@ -87,7 +87,9 @@ package enum PeerAddress: Equatable {
// - ipv4:<host>:<port> for ipv4 addresses
// - ipv6:[<host>]:<port> for ipv6 addresses
// - unix:<uds-pathname> for UNIX domain sockets
let addressComponents = address.split(separator: ":", omittingEmptySubsequences: false)

// First get the first component so that we know what type of address we're dealing with
let addressComponents = address.split(separator: ":", maxSplits: 1)

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

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

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

default:
Expand Down
2 changes: 1 addition & 1 deletion Tests/GRPCInterceptorsTests/PeerAddressTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct PeerAddressTests {

@Test("IPv6 addresses are correctly parsed")
func testIPv6() {
let address = PeerAddress("ipv6:2001::130F:::09C0:876A:130B:1234")
let address = PeerAddress("ipv6:[2001::130F:::09C0:876A:130B]:1234")
#expect(address == .ipv6(address: "2001::130F:::09C0:876A:130B", port: 1234))
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/GRPCInterceptorsTests/TracingInterceptorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ struct OTelTracingClientInterceptorTests {

case .ipv6:
return OTelTracingInterceptorTestCaseValues(
remotePeerAddress: "ipv6:2001::130F:::09C0:876A:130B:1234",
localPeerAddress: "ipv6:ff06:0:0:0:0:0:0:c3:5678",
remotePeerAddress: "ipv6:[2001::130F:::09C0:876A:130B]:1234",
localPeerAddress: "ipv6:[ff06:0:0:0:0:0:0:c3]:5678",
expectedSpanAttributes: [
"rpc.system": "grpc",
"rpc.method": .string(methodDescriptor.method),
Expand Down

0 comments on commit 3275f72

Please sign in to comment.