Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev-packages/node-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"node-cron": "^3.0.3",
"node-schedule": "^2.1.1",
"openai": "5.18.1",
"pg": "8.16.0",
"pg": "8.20.0",
"pino": "9.10.0",
"pino-next": "npm:pino@^9.12.0",
"postgres": "^3.4.7",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import pg from 'pg';
const { native } = pg;
const { Client } = native;

const client = new Client({ port: 5495, user: 'test', password: 'test', database: 'tests' });
// `pg-native` uses libpq, which resolves `localhost` to IPv6 (`::1`) first and does not
// fall back to IPv4. Docker Desktop only forwards the mapped port over IPv4, so we connect
// to the IPv4 loopback explicitly to avoid an `ECONNREFUSED` on `::1`.
const client = new Client({ host: '127.0.0.1', port: 5495, user: 'test', password: 'test', database: 'tests' });

async function run() {
await Sentry.startSpan(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,31 @@
* Builds the expected strict shape of a streamed postgres db span.
* The `pg.connect` span has neither a `db.statement` nor a `sentry.origin`,
* whereas query spans carry both.
*
* `host` defaults to `localhost`, but the `pg-native` scenarios connect to the
* IPv4 loopback (`127.0.0.1`) explicitly, so the reported peer name and
* connection string reflect that.
*/
function expectedDbSpan({ name, statement }: { name: string; statement?: string }): unknown {
const attributes: Record<string, unknown> = { ...COMMON_DB_ATTRIBUTES };
function expectedDbSpan({
name,
statement,
host = 'localhost',
}: {
name: string;
statement?: string;
host?: string;
}): unknown {
const attributes: Record<string, unknown> = {
...COMMON_DB_ATTRIBUTES,
'net.peer.name': {
type: 'string',
value: host,
},
'db.connection_string': {
type: 'string',
value: expect.stringMatching(new RegExp(`^postgresql://${host.replace(/\./g, '\\.')}:\\d+/tests$`)),

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High test

This does not escape backslash characters in the input.
Comment thread
mydea marked this conversation as resolved.
Dismissed
},
};

if (statement) {
attributes['db.statement'] = {
Expand Down Expand Up @@ -180,38 +202,53 @@
});

conditionalTest({ max: 25 })('pg-native', () => {
createEsmAndCjsTests(__dirname, 'scenario-native.mjs', 'instrument.mjs', (createTestRunner, test) => {
test('should auto-instrument `pg-native` package with span streaming enabled', { timeout: 90_000 }, async () => {
await createTestRunner()
.withDockerCompose({
workingDirectory: [__dirname],
setupCommand: 'yarn',
})
.expect({
span: container => {
const segmentSpan = container.items.find(item => item.is_segment);
expect(segmentSpan?.name).toBe('Test Span');

const dbSpans = getDbSpans(container);
expect(dbSpans.length).toBe(4);

expect(dbSpans).toEqual([
expectedDbSpan({ name: 'pg.connect' }),
expectedDbSpan({
name: CREATE_NATIVE_USER_TABLE_STATEMENT,
statement: CREATE_NATIVE_USER_TABLE_STATEMENT,
}),
expectedDbSpan({
name: 'INSERT INTO "NativeUser" ("email", "name") VALUES ($1, $2)',
statement: 'INSERT INTO "NativeUser" ("email", "name") VALUES ($1, $2)',
}),
expectedDbSpan({ name: 'SELECT * FROM "NativeUser"', statement: 'SELECT * FROM "NativeUser"' }),
]);
},
})
.start()
.completed();
});
});
createEsmAndCjsTests(
__dirname,
'scenario-native.mjs',
'instrument.mjs',
(createTestRunner, test) => {
test(
'should auto-instrument `pg-native` package with span streaming enabled',
{ timeout: 120_000 },
async () => {
await createTestRunner()
.withDockerCompose({
workingDirectory: [__dirname],
})
.expect({
span: container => {
const segmentSpan = container.items.find(item => item.is_segment);
expect(segmentSpan?.name).toBe('Test Span');

const dbSpans = getDbSpans(container);
expect(dbSpans.length).toBe(4);

expect(dbSpans).toEqual([
expectedDbSpan({ name: 'pg.connect', host: '127.0.0.1' }),
expectedDbSpan({
name: CREATE_NATIVE_USER_TABLE_STATEMENT,
statement: CREATE_NATIVE_USER_TABLE_STATEMENT,
host: '127.0.0.1',
}),
expectedDbSpan({
name: 'INSERT INTO "NativeUser" ("email", "name") VALUES ($1, $2)',
statement: 'INSERT INTO "NativeUser" ("email", "name") VALUES ($1, $2)',
host: '127.0.0.1',
}),
expectedDbSpan({
name: 'SELECT * FROM "NativeUser"',
statement: 'SELECT * FROM "NativeUser"',
host: '127.0.0.1',
}),
]);
},
})
.start()
.completed();
},
);
},
{ additionalDependencies: { 'pg-native': '3.7.0', pg: '8.20.0' } },
);
});
});

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import pg from 'pg';
const { native } = pg;
const { Client } = native;

const client = new Client({ port: 5494, user: 'test', password: 'test', database: 'tests' });
// `pg-native` uses libpq, which resolves `localhost` to IPv6 (`::1`) first and does not
// fall back to IPv4. Docker Desktop only forwards the mapped port over IPv4, so we connect
// to the IPv4 loopback explicitly to avoid an `ECONNREFUSED` on `::1`.
const client = new Client({ host: '127.0.0.1', port: 5494, user: 'test', password: 'test', database: 'tests' });

async function run() {
await Sentry.startSpan(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,17 +340,22 @@ describe('postgres auto instrumentation', () => {
]),
};

createEsmAndCjsTests(__dirname, 'scenario-native.mjs', 'instrument.mjs', (createTestRunner, test) => {
test('should auto-instrument `pg-native` package', { timeout: 90_000 }, async () => {
await createTestRunner()
.withDockerCompose({
workingDirectory: [__dirname],
setupCommand: 'yarn',
})
.expect({ transaction: EXPECTED_TRANSACTION })
.start()
.completed();
});
});
createEsmAndCjsTests(
__dirname,
'scenario-native.mjs',
'instrument.mjs',
(createTestRunner, test) => {
test('should auto-instrument `pg-native` package', { timeout: 120_000 }, async () => {
await createTestRunner()
.withDockerCompose({
workingDirectory: [__dirname],
})
.expect({ transaction: EXPECTED_TRANSACTION })
.start()
.completed();
});
},
{ additionalDependencies: { 'pg-native': '3.7.0', pg: '8.20.0' } },
);
});
});
Loading
Loading