Skip to content

Commit 10189c7

Browse files
committed
PR changes
1 parent 3275f72 commit 10189c7

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

Sources/GRPCInterceptors/Tracing/SpanAttributes+GRPCTracingKeys.swift

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,11 @@ extension Span {
6565
self.attributes[GRPCTracingKeys.serverPort] = port
6666

6767
case .unixDomainSocket(let path):
68-
self.attributes[GRPCTracingKeys.networkType] = "unix"
6968
self.attributes[GRPCTracingKeys.networkPeerAddress] = path
7069

71-
case .other(let address):
72-
// We can't nicely format the span attributes to contain the appropriate information here,
73-
// so include the whole thing as part of the peer address.
74-
self.attributes[GRPCTracingKeys.networkPeerAddress] = address
70+
case .none:
71+
// We don't recognise this address format, so don't populate any fields.
72+
()
7573
}
7674
}
7775
}
@@ -80,9 +78,8 @@ package enum PeerAddress: Equatable {
8078
case ipv4(address: String, port: Int?)
8179
case ipv6(address: String, port: Int?)
8280
case unixDomainSocket(path: String)
83-
case other(String)
8481

85-
package init(_ address: String) {
82+
package init?(_ address: String) {
8683
// We expect this address to be of one of these formats:
8784
// - ipv4:<host>:<port> for ipv4 addresses
8885
// - ipv6:[<host>]:<port> for ipv6 addresses
@@ -92,9 +89,8 @@ package enum PeerAddress: Equatable {
9289
let addressComponents = address.split(separator: ":", maxSplits: 1)
9390

9491
guard addressComponents.count > 1 else {
95-
// This is some unexpected/unknown format, so we have no way of splitting it up nicely.
96-
self = .other(address)
97-
return
92+
// This is some unexpected/unknown format
93+
return nil
9894
}
9995

10096
// Check what type the transport is...
@@ -104,29 +100,31 @@ package enum PeerAddress: Equatable {
104100
if ipv4AddressComponents.count == 2, let port = Int(ipv4AddressComponents[1]) {
105101
self = .ipv4(address: String(ipv4AddressComponents[0]), port: port)
106102
} else {
107-
// This is some unexpected/unknown format, so we have no way of splitting it up nicely.
108-
self = .other(address)
103+
return nil
109104
}
110105

111106
case "ipv6":
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)
107+
if addressComponents[1].first == "[" {
108+
// At this point, we are looking at an address with format: [<address>]:<port>
109+
// We drop the first character ('[') and split by ']:' to keep two components: the address
110+
// and the port.
111+
let ipv6AddressComponents = addressComponents[1].dropFirst().split(separator: "]:")
112+
if ipv6AddressComponents.count == 2, let port = Int(ipv6AddressComponents[1]) {
113+
self = .ipv6(address: String(ipv6AddressComponents[0]), port: port)
114+
} else {
115+
return nil
116+
}
118117
} else {
119-
// This is some unexpected/unknown format, so we have no way of splitting it up nicely.
120-
self = .other(address)
118+
return nil
121119
}
122120

123121
case "unix":
124122
// Whatever comes after "unix:" is the <pathname>
125123
self = .unixDomainSocket(path: String(addressComponents[1]))
126124

127125
default:
128-
// This is some unexpected/unknown format, so we have no way of splitting it up nicely.
129-
self = .other(address)
126+
// This is some unexpected/unknown format
127+
return nil
130128
}
131129
}
132130
}

Tests/GRPCInterceptorsTests/PeerAddressTests.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,23 @@ struct PeerAddressTests {
3737
#expect(address == .unixDomainSocket(path: "some-path"))
3838
}
3939

40-
@Test("Unrecognised addresses are correctly parsed")
41-
func testOther() {
42-
let address = PeerAddress("in-process:1234")
43-
#expect(address == .other("in-process:1234"))
40+
@Test(
41+
"Unrecognised addresses return nil",
42+
arguments: [
43+
"",
44+
"unknown",
45+
"in-process:1234",
46+
"ipv4:",
47+
"ipv4:1234",
48+
"ipv6:",
49+
"ipv6:123:456:789:123",
50+
"ipv6:123:456:789]:123",
51+
"ipv6:123:456:789]",
52+
"unix",
53+
]
54+
)
55+
func testOther(address: String) {
56+
let address = PeerAddress(address)
57+
#expect(address == nil)
4458
}
4559
}

0 commit comments

Comments
 (0)