Skip to content

Commit ed24b87

Browse files
committed
Handle IPv6 hosts better
1 parent 3636b82 commit ed24b87

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

dev-packages/e2e-tests/test-applications/node-firebase/tests/transactions.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const spanAddDoc = expect.objectContaining({
1212
'firebase.firestore.type': 'collection',
1313
'otel.kind': 'CLIENT',
1414
'server.address': '127.0.0.1',
15-
'server.port': '8080',
15+
'server.port': 8080,
1616
'sentry.origin': 'auto.firebase.otel.firestore',
1717
'sentry.op': 'db.query',
1818
}),
@@ -37,7 +37,7 @@ const spanSetDocs = expect.objectContaining({
3737
'firebase.firestore.type': 'collection',
3838
'otel.kind': 'CLIENT',
3939
'server.address': '127.0.0.1',
40-
'server.port': '8080',
40+
'server.port': 8080,
4141
'sentry.origin': 'auto.firebase.otel.firestore',
4242
'sentry.op': 'db.query',
4343
}),
@@ -62,7 +62,7 @@ const spanGetDocs = expect.objectContaining({
6262
'firebase.firestore.type': 'collection',
6363
'otel.kind': 'CLIENT',
6464
'server.address': '127.0.0.1',
65-
'server.port': '8080',
65+
'server.port': 8080,
6666
'sentry.origin': 'auto.firebase.otel.firestore',
6767
'sentry.op': 'db.query',
6868
}),
@@ -87,7 +87,7 @@ const spanDeleteDoc = expect.objectContaining({
8787
'firebase.firestore.type': 'collection',
8888
'otel.kind': 'CLIENT',
8989
'server.address': '127.0.0.1',
90-
'server.port': '8080',
90+
'server.port': 8080,
9191
'sentry.origin': 'auto.firebase.otel.firestore',
9292
'sentry.op': 'db.query',
9393
}),

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as net from 'node:net';
21
import type { Span, Tracer } from '@opentelemetry/api';
32
import { context, diag, SpanKind, trace } from '@opentelemetry/api';
43
import {
@@ -273,13 +272,23 @@ function addAttributes<AppModelType, DbModelType extends DocumentData>(
273272
};
274273

275274
if (typeof settings.host === 'string') {
276-
const arr = net.isIPv6(settings.host)
277-
? settings.host.split(']:') // Handling IPv6 addresses
278-
: settings.host.split(':'); // Handling IPv4 addresses
275+
if (settings.host.startsWith('[') && settings.host.endsWith(']')) {
276+
// Handling IPv6 addresses
277+
attributes[ATTR_SERVER_ADDRESS] = settings.host.slice(1, -1);
278+
} else {
279+
// Handling IPv4 addresses
280+
attributes[ATTR_SERVER_ADDRESS] = settings.host;
281+
}
279282

280-
if (arr.length === 2) {
281-
attributes[ATTR_SERVER_ADDRESS] = arr[0];
282-
attributes[ATTR_SERVER_PORT] = Number(arr[1]);
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+
}
283292
}
284293
}
285294

0 commit comments

Comments
 (0)