-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(node): Add postgresjs instrumentation #16665
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
306269f
feat(node): Add postgresjs instrumentation
onurtemizkan 34fa611
Potential fix for code scanning alert no. 400: Inefficient regular ex…
onurtemizkan d7c93ea
Fix circular dependency
onurtemizkan deb6a9e
Add missing comments
onurtemizkan 3ba19f8
Update tests
onurtemizkan ff9248e
Clean up
onurtemizkan edcc016
Add `requireParentSpan`, `requestHook` and `origin`
onurtemizkan 9798064
Add more attributes
onurtemizkan fd79525
Move test emails to constants
onurtemizkan 92e5f33
Lint
onurtemizkan f003bd0
Merge branch 'develop' into onur/postgres.js-instrumentation
onurtemizkan 3d373cc
Export integration
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
13 changes: 13 additions & 0 deletions
13
dev-packages/node-integration-tests/suites/tracing/postgresjs/docker-compose.yml
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,13 @@ | ||
version: '3.9' | ||
|
||
services: | ||
db: | ||
image: postgres:13 | ||
restart: always | ||
container_name: integration-tests-postgresjs | ||
ports: | ||
- '5444:5432' | ||
environment: | ||
POSTGRES_USER: test | ||
POSTGRES_PASSWORD: test | ||
POSTGRES_DB: test_db |
62 changes: 62 additions & 0 deletions
62
dev-packages/node-integration-tests/suites/tracing/postgresjs/scenario.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,62 @@ | ||
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` | ||
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(); | ||
} | ||
}, | ||
); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
run(); |
225 changes: 225 additions & 0 deletions
225
dev-packages/node-integration-tests/suites/tracing/postgresjs/test.ts
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,225 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { createRunner } from '../../../utils/runner'; | ||
|
||
const EXISTING_TEST_EMAIL = 'bar@baz.com'; | ||
const NON_EXISTING_TEST_EMAIL = 'foo@baz.com'; | ||
|
||
describe('postgresjs auto instrumentation', () => { | ||
test('should auto-instrument `postgres` package', { timeout: 60_000 }, async () => { | ||
const EXPECTED_TRANSACTION = { | ||
transaction: 'Test Transaction', | ||
spans: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'db.namespace': 'test_db', | ||
'db.system.name': 'postgres', | ||
'db.operation.name': 'CREATE TABLE', | ||
'db.query.text': | ||
'CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(?) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id"))', | ||
'sentry.op': 'db', | ||
'sentry.origin': 'auto.db.otel.postgres', | ||
'server.address': 'localhost', | ||
'server.port': 5444, | ||
}), | ||
description: | ||
'CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(?) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id"))', | ||
op: 'db', | ||
status: 'ok', | ||
origin: 'auto.db.otel.postgres', | ||
parent_span_id: expect.any(String), | ||
span_id: expect.any(String), | ||
start_timestamp: expect.any(Number), | ||
timestamp: expect.any(Number), | ||
trace_id: expect.any(String), | ||
}), | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'db.namespace': 'test_db', | ||
'db.system.name': 'postgres', | ||
'db.operation.name': 'SELECT', | ||
'db.query.text': | ||
"select b.oid, b.typarray from pg_catalog.pg_type a left join pg_catalog.pg_type b on b.oid = a.typelem where a.typcategory = 'A' group by b.oid, b.typarray order by b.oid", | ||
'sentry.op': 'db', | ||
'sentry.origin': 'auto.db.otel.postgres', | ||
'server.address': 'localhost', | ||
'server.port': 5444, | ||
}), | ||
description: | ||
"select b.oid, b.typarray from pg_catalog.pg_type a left join pg_catalog.pg_type b on b.oid = a.typelem where a.typcategory = 'A' group by b.oid, b.typarray order by b.oid", | ||
op: 'db', | ||
status: 'ok', | ||
origin: 'auto.db.otel.postgres', | ||
parent_span_id: expect.any(String), | ||
span_id: expect.any(String), | ||
start_timestamp: expect.any(Number), | ||
timestamp: expect.any(Number), | ||
trace_id: expect.any(String), | ||
}), | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'db.namespace': 'test_db', | ||
'db.system.name': 'postgres', | ||
'db.operation.name': 'INSERT', | ||
'db.query.text': `INSERT INTO "User" ("email", "name") VALUES ('Foo', '${EXISTING_TEST_EMAIL}')`, | ||
'sentry.origin': 'auto.db.otel.postgres', | ||
'sentry.op': 'db', | ||
'server.address': 'localhost', | ||
'server.port': 5444, | ||
}), | ||
description: `INSERT INTO "User" ("email", "name") VALUES ('Foo', '${EXISTING_TEST_EMAIL}')`, | ||
op: 'db', | ||
status: 'ok', | ||
origin: 'auto.db.otel.postgres', | ||
parent_span_id: expect.any(String), | ||
span_id: expect.any(String), | ||
start_timestamp: expect.any(Number), | ||
timestamp: expect.any(Number), | ||
trace_id: expect.any(String), | ||
}), | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'db.namespace': 'test_db', | ||
'db.system.name': 'postgres', | ||
'db.operation.name': 'UPDATE', | ||
'db.query.text': `UPDATE "User" SET "name" = 'Foo' WHERE "email" = '${EXISTING_TEST_EMAIL}'`, | ||
'sentry.op': 'db', | ||
'sentry.origin': 'auto.db.otel.postgres', | ||
'server.address': 'localhost', | ||
'server.port': 5444, | ||
}), | ||
description: `UPDATE "User" SET "name" = 'Foo' WHERE "email" = '${EXISTING_TEST_EMAIL}'`, | ||
op: 'db', | ||
status: 'ok', | ||
origin: 'auto.db.otel.postgres', | ||
parent_span_id: expect.any(String), | ||
span_id: expect.any(String), | ||
start_timestamp: expect.any(Number), | ||
timestamp: expect.any(Number), | ||
trace_id: expect.any(String), | ||
}), | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'db.namespace': 'test_db', | ||
'db.system.name': 'postgres', | ||
'db.operation.name': 'SELECT', | ||
'db.query.text': `SELECT * FROM "User" WHERE "email" = '${EXISTING_TEST_EMAIL}'`, | ||
'sentry.op': 'db', | ||
'sentry.origin': 'auto.db.otel.postgres', | ||
'server.address': 'localhost', | ||
'server.port': 5444, | ||
}), | ||
description: `SELECT * FROM "User" WHERE "email" = '${EXISTING_TEST_EMAIL}'`, | ||
op: 'db', | ||
status: 'ok', | ||
origin: 'auto.db.otel.postgres', | ||
parent_span_id: expect.any(String), | ||
span_id: expect.any(String), | ||
start_timestamp: expect.any(Number), | ||
timestamp: expect.any(Number), | ||
trace_id: expect.any(String), | ||
}), | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'db.namespace': 'test_db', | ||
'db.system.name': 'postgres', | ||
'db.operation.name': 'SELECT', | ||
'db.query.text': 'SELECT * from generate_series(?,?) as x', | ||
'sentry.op': 'db', | ||
'sentry.origin': 'auto.db.otel.postgres', | ||
'server.address': 'localhost', | ||
'server.port': 5444, | ||
}), | ||
description: 'SELECT * from generate_series(?,?) as x', | ||
op: 'db', | ||
status: 'ok', | ||
origin: 'auto.db.otel.postgres', | ||
parent_span_id: expect.any(String), | ||
span_id: expect.any(String), | ||
start_timestamp: expect.any(Number), | ||
timestamp: expect.any(Number), | ||
trace_id: expect.any(String), | ||
}), | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'db.namespace': 'test_db', | ||
'db.system.name': 'postgres', | ||
'db.operation.name': 'DROP TABLE', | ||
'db.query.text': 'DROP TABLE "User"', | ||
'sentry.op': 'db', | ||
'sentry.origin': 'auto.db.otel.postgres', | ||
'server.address': 'localhost', | ||
'server.port': 5444, | ||
}), | ||
description: 'DROP TABLE "User"', | ||
op: 'db', | ||
status: 'ok', | ||
origin: 'auto.db.otel.postgres', | ||
parent_span_id: expect.any(String), | ||
span_id: expect.any(String), | ||
start_timestamp: expect.any(Number), | ||
timestamp: expect.any(Number), | ||
trace_id: expect.any(String), | ||
}), | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'db.namespace': 'test_db', | ||
'db.system.name': 'postgres', | ||
// No db.operation.name here, as this is an errored span | ||
'db.response.status_code': '42P01', | ||
'error.type': 'PostgresError', | ||
'db.query.text': `SELECT * FROM "User" WHERE "email" = '${NON_EXISTING_TEST_EMAIL}'`, | ||
'sentry.op': 'db', | ||
'sentry.origin': 'auto.db.otel.postgres', | ||
'server.address': 'localhost', | ||
'server.port': 5444, | ||
}), | ||
description: `SELECT * FROM "User" WHERE "email" = '${NON_EXISTING_TEST_EMAIL}'`, | ||
op: 'db', | ||
status: 'unknown_error', | ||
origin: 'auto.db.otel.postgres', | ||
parent_span_id: expect.any(String), | ||
span_id: expect.any(String), | ||
start_timestamp: expect.any(Number), | ||
timestamp: expect.any(Number), | ||
trace_id: expect.any(String), | ||
}), | ||
]), | ||
}; | ||
|
||
const EXPECTED_ERROR_EVENT = { | ||
event_id: expect.any(String), | ||
contexts: { | ||
trace: { | ||
trace_id: expect.any(String), | ||
span_id: expect.any(String), | ||
}, | ||
}, | ||
exception: { | ||
values: [ | ||
{ | ||
type: 'PostgresError', | ||
value: 'relation "User" does not exist', | ||
stacktrace: expect.objectContaining({ | ||
frames: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
function: 'handle', | ||
module: 'postgres.cjs.src:connection', | ||
filename: expect.any(String), | ||
lineno: expect.any(Number), | ||
colno: expect.any(Number), | ||
}), | ||
]), | ||
}), | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
await createRunner(__dirname, 'scenario.js') | ||
.withDockerCompose({ workingDirectory: [__dirname], readyMatches: ['port 5432'] }) | ||
.expect({ transaction: EXPECTED_TRANSACTION }) | ||
.expect({ event: EXPECTED_ERROR_EVENT }) | ||
.start() | ||
.completed(); | ||
}); | ||
}); |
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
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
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'd perhaps want to leave the email in a constant since we are repeating it over and over.
it doesn't really matter since it's a dummy email, what I'm thinking is: what if at some point we want to run the test on a specific email? (if we ever want to do that)
It would be easier to just change the value of the constant over changing every single instance of it.
Just my thought 😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, updated. Thanks 👍