Skip to content

Commit 1416017

Browse files
committed
Handle IPv6 hosts even better
1 parent ed24b87 commit 1416017

File tree

1 file changed

+35
-14
lines changed
  • packages/node/src/integrations/tracing/firebase/otel/patches

1 file changed

+35
-14
lines changed

packages/node/src/integrations/tracing/firebase/otel/patches/firestore.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -272,23 +272,44 @@ function addAttributes<AppModelType, DbModelType extends DocumentData>(
272272
};
273273

274274
if (typeof settings.host === 'string') {
275-
if (settings.host.startsWith('[') && settings.host.endsWith(']')) {
276-
// Handling IPv6 addresses
277-
attributes[ATTR_SERVER_ADDRESS] = settings.host.slice(1, -1);
275+
let address: string | undefined;
276+
let port: string | undefined;
277+
278+
if (settings.host.startsWith('[')) {
279+
if (settings.host.endsWith(']')) {
280+
// Theres no port, just the address
281+
address = settings.host.slice(1, -1);
282+
} else {
283+
// Handling IPv6 addresses with port
284+
const lastColonIndex = settings.host.lastIndexOf(':');
285+
if (lastColonIndex !== -1) {
286+
address = settings.host.slice(1, lastColonIndex);
287+
port = settings.host.slice(lastColonIndex + 1);
288+
}
289+
}
278290
} else {
279-
// Handling IPv4 addresses
280-
attributes[ATTR_SERVER_ADDRESS] = settings.host;
291+
if (settings.host.includes('::')) {
292+
// Handling IPv6 addresses with port
293+
const parts = settings.host.split(':');
294+
address = parts.slice(0, -1).join(':');
295+
port = parts[parts.length - 1];
296+
} else if (settings.host.includes(':')) {
297+
// Handling IPv4 addresses with port
298+
const parts = settings.host.split(':');
299+
address = parts[0];
300+
port = parts[1];
301+
} else {
302+
// Handling IPv4 addresses without port
303+
address = settings.host;
304+
}
281305
}
282306

283-
if (settings.host.includes(':')) {
284-
// Split the host by ':' to get the address and port
285-
// This will handle both IPv4 and IPv6 addresses correctly
286-
// It will split at the last colon
287-
const lastColonIndex = settings.host.lastIndexOf(':');
288-
if (lastColonIndex !== -1) {
289-
attributes[ATTR_SERVER_ADDRESS] = settings.host.slice(0, lastColonIndex);
290-
attributes[ATTR_SERVER_PORT] = Number(settings.host.slice(lastColonIndex + 1));
291-
}
307+
if (address) {
308+
attributes[ATTR_SERVER_ADDRESS] = address;
309+
}
310+
311+
if (port !== undefined) {
312+
attributes[ATTR_SERVER_PORT] = Number(port);
292313
}
293314
}
294315

0 commit comments

Comments
 (0)