-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
docs(cloudflare,deno): Add instrumentPostgresJsSql #16634
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| --- | ||
| title: Postgres.js | ||
| description: "Adds instrumentation for the postgres (postgres.js) library." | ||
| supported: | ||
| - javascript.cloudflare | ||
| - javascript.deno | ||
| --- | ||
|
|
||
| <AvailableSince version="10.41.0" /> | ||
|
|
||
| _Import name: `Sentry.instrumentPostgresJsSql`_ | ||
|
|
||
| The `instrumentPostgresJsSql` helper adds instrumentation for the [`postgres`](https://github.com/porsager/postgres) (postgres.js) library to capture spans by wrapping a postgres.js `sql` tagged-template instance. You need to manually wrap your `sql` instance with this helper: | ||
|
|
||
| ## Usage | ||
|
|
||
| <PlatformSection supported={["javascript.cloudflare"]}> | ||
|
|
||
| ```javascript | ||
| import postgres from "postgres"; | ||
| import * as Sentry from "@sentry/cloudflare"; | ||
|
|
||
| export default Sentry.withSentry((env) => ({ dsn: "__DSN__" }), { | ||
| async fetch(request, env, ctx) { | ||
| const sql = Sentry.instrumentPostgresJsSql(postgres(env.DATABASE_URL)); | ||
|
|
||
| // All queries now create Sentry spans | ||
| const users = await sql`SELECT * FROM users WHERE id = ${userId}`; | ||
| return Response.json(users); | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| </PlatformSection> | ||
|
|
||
| <PlatformSection supported={["javascript.deno"]}> | ||
|
|
||
| ```javascript | ||
| import * as Sentry from "npm:@sentry/deno"; | ||
| import postgres from "npm:postgres"; | ||
|
|
||
| const sql = Sentry.instrumentPostgresJsSql( | ||
| postgres("postgres://user:pass@localhost/mydb") | ||
| ); | ||
|
|
||
| // All queries now create Sentry spans | ||
| const users = await sql`SELECT * FROM users WHERE id = ${userId}`; | ||
| ``` | ||
|
|
||
| </PlatformSection> | ||
|
|
||
| ## Options | ||
|
|
||
| ### `requireParentSpan` | ||
|
|
||
| _Type: `boolean`_ | ||
|
|
||
| Whether the instrumentation requires a parent span to create child spans. When set to `true`, spans are only created if there is an active parent span in the current scope. | ||
|
|
||
| Default: `true` | ||
|
|
||
| ### `requestHook` | ||
|
|
||
| _Type: `(span: Span, sanitizedSqlQuery: string, postgresConnectionContext?: PostgresConnectionContext) => void`_ | ||
|
|
||
| A hook called before each span is started. Use it to set additional attributes or modify the span. | ||
|
|
||
| ```javascript | ||
| const sql = Sentry.instrumentPostgresJsSql(postgres(env.DATABASE_URL), { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The generic Suggested FixMake the example platform-agnostic by using a generic placeholder like Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
| requestHook(span, query) { | ||
| span.setAttribute("custom.query", query); | ||
| }, | ||
| }); | ||
| ``` | ||
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.
Bug: The
postgresjsdocumentation examples use an undeclareduserIdvariable, which will cause aReferenceErrorwhen the code is executed.Severity: MEDIUM
Suggested Fix
Declare the
userIdvariable before it is used in the SQL query. For example, addconst userId = "user_123";before the line wheresqlis called.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.