diff --git a/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-requestHook.js b/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-requestHook.js index a2b405d71f60..cd5303c3de8a 100644 --- a/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-requestHook.js +++ b/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-requestHook.js @@ -1,5 +1,6 @@ const Sentry = require('@sentry/node'); const postgres = require('postgres'); +const { waitForPostgres } = require('./wait-for-postgres.js'); // Stop the process from exiting before the transaction is sent setInterval(() => {}, 1000); @@ -14,6 +15,7 @@ async function run() { }, async () => { try { + await waitForPostgres(sql); await sql` CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id")); `; diff --git a/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-requestHook.mjs b/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-requestHook.mjs index f6e69354ccbc..c54fe084a1f6 100644 --- a/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-requestHook.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-requestHook.mjs @@ -1,6 +1,10 @@ import * as Sentry from '@sentry/node'; +import { createRequire } from 'node:module'; import postgres from 'postgres'; +const require = createRequire(import.meta.url); +const { waitForPostgres } = require('./wait-for-postgres.js'); + // Stop the process from exiting before the transaction is sent setInterval(() => {}, 1000); @@ -14,6 +18,7 @@ async function run() { }, async () => { try { + await waitForPostgres(sql); await sql` CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id")); `; diff --git a/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-unsafe.cjs b/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-unsafe.cjs index 0ee537052a4a..6ceab6f9ec3b 100644 --- a/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-unsafe.cjs +++ b/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-unsafe.cjs @@ -10,6 +10,7 @@ Sentry.init({ // Import postgres AFTER Sentry.init() so instrumentation is set up const postgres = require('postgres'); +const { waitForPostgres } = require('./wait-for-postgres.js'); // Stop the process from exiting before the transaction is sent setInterval(() => {}, 1000); @@ -25,6 +26,7 @@ async function run() { }, async () => { try { + await waitForPostgres(sql); // Test sql.unsafe() - this was not being instrumented before the fix await sql.unsafe('CREATE TABLE "User" ("id" SERIAL NOT NULL, "email" TEXT NOT NULL, PRIMARY KEY ("id"))'); diff --git a/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-unsafe.mjs b/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-unsafe.mjs index 9d2e7de99e51..8f0d5070b829 100644 --- a/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-unsafe.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-unsafe.mjs @@ -1,6 +1,10 @@ import * as Sentry from '@sentry/node'; +import { createRequire } from 'node:module'; import postgres from 'postgres'; +const require = createRequire(import.meta.url); +const { waitForPostgres } = require('./wait-for-postgres.js'); + // Stop the process from exiting before the transaction is sent setInterval(() => {}, 1000); @@ -15,6 +19,7 @@ async function run() { }, async () => { try { + await waitForPostgres(sql); // Test sql.unsafe() - this was not being instrumented before the fix await sql.unsafe('CREATE TABLE "User" ("id" SERIAL NOT NULL, "email" TEXT NOT NULL, PRIMARY KEY ("id"))'); diff --git a/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-url.cjs b/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-url.cjs index 1a5cc93e2261..fbda092cad28 100644 --- a/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-url.cjs +++ b/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-url.cjs @@ -10,6 +10,7 @@ Sentry.init({ // Import postgres AFTER Sentry.init() so instrumentation is set up const postgres = require('postgres'); +const { waitForPostgres } = require('./wait-for-postgres.js'); // Stop the process from exiting before the transaction is sent setInterval(() => {}, 1000); @@ -25,6 +26,7 @@ async function run() { }, async () => { try { + await waitForPostgres(sql); await sql` CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id")); `; diff --git a/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-url.mjs b/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-url.mjs index 2694bca96569..7edc0a6590a7 100644 --- a/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-url.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-url.mjs @@ -1,6 +1,10 @@ import * as Sentry from '@sentry/node'; +import { createRequire } from 'node:module'; import postgres from 'postgres'; +const require = createRequire(import.meta.url); +const { waitForPostgres } = require('./wait-for-postgres.js'); + // Stop the process from exiting before the transaction is sent setInterval(() => {}, 1000); @@ -15,6 +19,7 @@ async function run() { }, async () => { try { + await waitForPostgres(sql); await sql` CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id")); `; diff --git a/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario.js b/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario.js index d9049353f6eb..373da6082d2e 100644 --- a/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario.js +++ b/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario.js @@ -1,5 +1,6 @@ const { loggingTransport } = require('@sentry-internal/node-integration-tests'); const Sentry = require('@sentry/node'); +const { waitForPostgres } = require('./wait-for-postgres.js'); Sentry.init({ dsn: 'https://public@dsn.ingest.sentry.io/1337', @@ -23,6 +24,7 @@ async function run() { }, async () => { try { + await waitForPostgres(sql); await sql` CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id")); `; diff --git a/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario.mjs b/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario.mjs index 7d62c8d52dde..78536a82e3f9 100644 --- a/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario.mjs @@ -1,6 +1,10 @@ import * as Sentry from '@sentry/node'; +import { createRequire } from 'node:module'; import postgres from 'postgres'; +const require = createRequire(import.meta.url); +const { waitForPostgres } = require('./wait-for-postgres.js'); + // Stop the process from exiting before the transaction is sent setInterval(() => {}, 1000); @@ -14,6 +18,7 @@ async function run() { }, async () => { try { + await waitForPostgres(sql); await sql` CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id")); `; diff --git a/dev-packages/node-integration-tests/suites/tracing/postgresjs/wait-for-postgres.js b/dev-packages/node-integration-tests/suites/tracing/postgresjs/wait-for-postgres.js new file mode 100644 index 000000000000..c8c10c6eeb80 --- /dev/null +++ b/dev-packages/node-integration-tests/suites/tracing/postgresjs/wait-for-postgres.js @@ -0,0 +1,22 @@ +'use strict'; + +/** + * Retries until Postgres accepts connections. `docker compose up --wait` can report healthy + * before the port forward on the host is ready (flaky on busy CI). + */ +async function waitForPostgres(sql, maxWaitMs = 60_000) { + const deadline = Date.now() + maxWaitMs; + for (;;) { + try { + await sql`SELECT 1`; + return; + } catch { + if (Date.now() > deadline) { + throw new Error('Timed out waiting for Postgres to accept connections'); + } + await new Promise(r => setTimeout(r, 250)); + } + } +} + +module.exports = { waitForPostgres };