-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(node): Add ESM support for postgres.js instrumentation #17961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
onurtemizkan
wants to merge
11
commits into
develop
Choose a base branch
from
onur/postgres-js-esm-support
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
03d7284
feat(node): Add ESM support for postgres.js instrumentation
onurtemizkan c215986
Add Query.prototype fallback patching for postgres.js pre-existing in…
onurtemizkan 160d50e
Increase size limit by 1 KB
onurtemizkan 9d80dd0
Fix formatting
onurtemizkan 64125c1
Make original header restorable by unpatch
onurtemizkan 11a366d
Address review comments
onurtemizkan efd789d
Sanitize / parameterize queries better
onurtemizkan e687a79
Explicitly test query properties
onurtemizkan d6ff4c2
Merge branch 'develop' into onur/postgres-js-esm-support
onurtemizkan 4ae3e66
Add complete sanitization
onurtemizkan 036d101
Lint
onurtemizkan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
dev-packages/node-integration-tests/suites/tracing/postgresjs/instrument-requestHook.cjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| const Sentry = require('@sentry/node'); | ||
| const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| transport: loggingTransport, | ||
| integrations: [ | ||
| Sentry.postgresJsIntegration({ | ||
| requestHook: (span, sanitizedSqlQuery, connectionContext) => { | ||
| // Add custom attributes to demonstrate requestHook functionality | ||
| span.setAttribute('custom.requestHook', 'called'); | ||
|
|
||
| // Set context information as extras for test validation | ||
| Sentry.setExtra('requestHookCalled', { | ||
| sanitizedQuery: sanitizedSqlQuery, | ||
| database: connectionContext?.ATTR_DB_NAMESPACE, | ||
| host: connectionContext?.ATTR_SERVER_ADDRESS, | ||
| port: connectionContext?.ATTR_SERVER_PORT, | ||
| }); | ||
| }, | ||
| }), | ||
| ], | ||
| }); |
25 changes: 25 additions & 0 deletions
25
dev-packages/node-integration-tests/suites/tracing/postgresjs/instrument-requestHook.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| transport: loggingTransport, | ||
| integrations: [ | ||
| Sentry.postgresJsIntegration({ | ||
| requestHook: (span, sanitizedSqlQuery, connectionContext) => { | ||
| // Add custom attributes to demonstrate requestHook functionality | ||
| span.setAttribute('custom.requestHook', 'called'); | ||
|
|
||
| // Set context information as extras for test validation | ||
| Sentry.setExtra('requestHookCalled', { | ||
| sanitizedQuery: sanitizedSqlQuery, | ||
| database: connectionContext?.ATTR_DB_NAMESPACE, | ||
| host: connectionContext?.ATTR_SERVER_ADDRESS, | ||
| port: connectionContext?.ATTR_SERVER_PORT, | ||
| }); | ||
| }, | ||
| }), | ||
| ], | ||
| }); |
9 changes: 9 additions & 0 deletions
9
dev-packages/node-integration-tests/suites/tracing/postgresjs/instrument.cjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| const Sentry = require('@sentry/node'); | ||
| const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| transport: loggingTransport, | ||
| }); |
9 changes: 9 additions & 0 deletions
9
dev-packages/node-integration-tests/suites/tracing/postgresjs/instrument.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| transport: loggingTransport, | ||
| }); |
49 changes: 49 additions & 0 deletions
49
dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-requestHook.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
| const Sentry = require('@sentry/node'); | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| transport: loggingTransport, | ||
| }); | ||
|
|
||
| // Stop the process from exiting before the transaction is sent | ||
| setInterval(() => {}, 1000); | ||
|
|
||
| const postgres = require('postgres'); | ||
|
|
||
| const sql = postgres({ port: 5444, user: 'test', password: 'test', database: 'test_db' }); | ||
|
|
||
| async function run() { | ||
| await Sentry.startSpan( | ||
| { | ||
| name: 'Test Transaction', | ||
| op: 'transaction', | ||
| }, | ||
| async () => { | ||
| try { | ||
| 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")); | ||
| `; | ||
|
|
||
| await sql` | ||
| INSERT INTO "User" ("email", "name") VALUES ('Foo', 'bar@baz.com'); | ||
| `; | ||
|
|
||
| await sql` | ||
| SELECT * FROM "User" WHERE "email" = 'bar@baz.com'; | ||
| `; | ||
|
|
||
| await sql` | ||
| DROP TABLE "User"; | ||
| `; | ||
| } finally { | ||
| await sql.end(); | ||
| } | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
| run(); | ||
40 changes: 40 additions & 0 deletions
40
dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-requestHook.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import postgres from 'postgres'; | ||
|
|
||
| // Stop the process from exiting before the transaction is sent | ||
| setInterval(() => {}, 1000); | ||
|
|
||
| const sql = postgres({ port: 5444, user: 'test', password: 'test', database: 'test_db' }); | ||
|
|
||
| async function run() { | ||
| await Sentry.startSpan( | ||
| { | ||
| name: 'Test Transaction', | ||
| op: 'transaction', | ||
| }, | ||
| async () => { | ||
| try { | ||
| 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")); | ||
| `; | ||
|
|
||
| await sql` | ||
| INSERT INTO "User" ("email", "name") VALUES ('Foo', 'bar@baz.com'); | ||
| `; | ||
|
|
||
| await sql` | ||
| SELECT * FROM "User" WHERE "email" = 'bar@baz.com'; | ||
| `; | ||
|
|
||
| await sql` | ||
| DROP TABLE "User"; | ||
| `; | ||
| } finally { | ||
| await sql.end(); | ||
| } | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
| run(); |
46 changes: 46 additions & 0 deletions
46
dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-unsafe.cjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
| const Sentry = require('@sentry/node'); | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| transport: loggingTransport, | ||
| }); | ||
|
|
||
| // Import postgres AFTER Sentry.init() so instrumentation is set up | ||
| const postgres = require('postgres'); | ||
|
|
||
| // Stop the process from exiting before the transaction is sent | ||
| setInterval(() => {}, 1000); | ||
|
|
||
| // Test with plain object options | ||
| const sql = postgres({ port: 5444, user: 'test', password: 'test', database: 'test_db' }); | ||
|
|
||
| async function run() { | ||
| await Sentry.startSpan( | ||
| { | ||
| name: 'Test Transaction', | ||
| op: 'transaction', | ||
| }, | ||
| async () => { | ||
| try { | ||
| // 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"))'); | ||
|
|
||
| await sql.unsafe('INSERT INTO "User" ("email") VALUES ($1)', ['test@example.com']); | ||
|
|
||
| await sql.unsafe('SELECT * FROM "User" WHERE "email" = $1', ['test@example.com']); | ||
|
|
||
| await sql.unsafe('DROP TABLE "User"'); | ||
|
|
||
| // This will be captured as an error as the table no longer exists | ||
| await sql.unsafe('SELECT * FROM "User"'); | ||
| } finally { | ||
| await sql.end(); | ||
| } | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| run(); |
37 changes: 37 additions & 0 deletions
37
dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-unsafe.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import postgres from 'postgres'; | ||
|
|
||
| // Stop the process from exiting before the transaction is sent | ||
| setInterval(() => {}, 1000); | ||
|
|
||
| // Test with plain object options | ||
| const sql = postgres({ port: 5444, user: 'test', password: 'test', database: 'test_db' }); | ||
|
|
||
| async function run() { | ||
| await Sentry.startSpan( | ||
| { | ||
| name: 'Test Transaction', | ||
| op: 'transaction', | ||
| }, | ||
| async () => { | ||
| try { | ||
| // 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"))'); | ||
|
|
||
| await sql.unsafe('INSERT INTO "User" ("email") VALUES ($1)', ['test@example.com']); | ||
|
|
||
| await sql.unsafe('SELECT * FROM "User" WHERE "email" = $1', ['test@example.com']); | ||
|
|
||
| await sql.unsafe('DROP TABLE "User"'); | ||
|
|
||
| // This will be captured as an error as the table no longer exists | ||
| await sql.unsafe('SELECT * FROM "User"'); | ||
| } finally { | ||
| await sql.end(); | ||
| } | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
| run(); |
63 changes: 63 additions & 0 deletions
63
dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-url.cjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
| const Sentry = require('@sentry/node'); | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| transport: loggingTransport, | ||
| }); | ||
|
|
||
| // Import postgres AFTER Sentry.init() so instrumentation is set up | ||
| const postgres = require('postgres'); | ||
|
|
||
| // Stop the process from exiting before the transaction is sent | ||
| setInterval(() => {}, 1000); | ||
|
|
||
| // Test URL-based initialization - this is the common pattern that was causing the regression | ||
| const sql = postgres('postgres://test:test@localhost:5444/test_db'); | ||
|
|
||
| async function run() { | ||
| await Sentry.startSpan( | ||
| { | ||
| name: 'Test Transaction', | ||
| op: 'transaction', | ||
| }, | ||
| async () => { | ||
| try { | ||
| 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")); | ||
| `; | ||
|
|
||
| await sql` | ||
| INSERT INTO "User" ("email", "name") VALUES ('Foo', 'bar@baz.com'); | ||
| `; | ||
|
|
||
| await sql` | ||
| UPDATE "User" SET "name" = 'Foo' WHERE "email" = 'bar@baz.com'; | ||
| `; | ||
|
|
||
| await sql` | ||
| SELECT * FROM "User" WHERE "email" = 'bar@baz.com'; | ||
| `; | ||
|
|
||
| await sql`SELECT * from generate_series(1,1000) as x `.cursor(10, async rows => { | ||
| await Promise.all(rows); | ||
| }); | ||
|
|
||
| await sql` | ||
| DROP TABLE "User"; | ||
| `; | ||
|
|
||
| // This will be captured as an error as the table no longer exists | ||
| await sql` | ||
| SELECT * FROM "User" WHERE "email" = 'foo@baz.com'; | ||
| `; | ||
| } finally { | ||
| await sql.end(); | ||
| } | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| run(); |
75 changes: 75 additions & 0 deletions
75
dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario-url.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import postgres from 'postgres'; | ||
|
|
||
| // Stop the process from exiting before the transaction is sent | ||
| setInterval(() => {}, 1000); | ||
|
|
||
| // Test URL-based initialization - this is the common pattern that was causing the regression | ||
| const sql = postgres('postgres://test:test@localhost:5444/test_db'); | ||
|
|
||
| async function run() { | ||
| await Sentry.startSpan( | ||
| { | ||
| name: 'Test Transaction', | ||
| op: 'transaction', | ||
| }, | ||
| async () => { | ||
| try { | ||
| 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")); | ||
| `; | ||
|
|
||
| await sql` | ||
| INSERT INTO "User" ("email", "name") VALUES ('Foo', 'bar@baz.com'); | ||
| `; | ||
|
|
||
| await sql` | ||
| UPDATE "User" SET "name" = 'Foo' WHERE "email" = 'bar@baz.com'; | ||
| `; | ||
|
|
||
| await sql` | ||
| SELECT * FROM "User" WHERE "email" = 'bar@baz.com'; | ||
| `; | ||
|
|
||
| // Test parameterized queries | ||
| await sql` | ||
| SELECT * FROM "User" WHERE "email" = ${'bar@baz.com'} AND "name" = ${'Foo'}; | ||
| `; | ||
|
|
||
| // Test DELETE operation | ||
| await sql` | ||
| DELETE FROM "User" WHERE "email" = 'bar@baz.com'; | ||
| `; | ||
|
|
||
| // Test INSERT with RETURNING | ||
| await sql` | ||
| INSERT INTO "User" ("email", "name") VALUES ('test@example.com', 'Test User') RETURNING *; | ||
| `; | ||
|
|
||
| // Test cursor-based queries | ||
| await sql`SELECT * from generate_series(1,1000) as x `.cursor(10, async rows => { | ||
| await Promise.all(rows); | ||
| }); | ||
|
|
||
| // Test multiple rows at once | ||
| await sql` | ||
| SELECT * FROM "User" LIMIT 10; | ||
| `; | ||
|
|
||
| await sql` | ||
| DROP TABLE "User"; | ||
| `; | ||
|
|
||
| // This will be captured as an error as the table no longer exists | ||
| await sql` | ||
| SELECT * FROM "User" WHERE "email" = 'foo@baz.com'; | ||
| `; | ||
| } finally { | ||
| await sql.end(); | ||
| } | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
| run(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.