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
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}`;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The postgresjs documentation examples use an undeclared userId variable, which will cause a ReferenceError when the code is executed.
Severity: MEDIUM

Suggested Fix

Declare the userId variable before it is used in the SQL query. For example, add const userId = "user_123"; before the line where sql is called.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: docs/platforms/javascript/common/configuration/integrations/postgresjs.mdx#L26

Potential issue: The code examples for Cloudflare and Deno in the `postgresjs.mdx`
documentation file use the template literal `${userId}` within SQL queries without
declaring the `userId` variable. If a developer copies and pastes this code, it will
throw a `ReferenceError: userId is not defined` at runtime, causing the application to
crash. Other documentation examples in the repository, such as in `logs/index.mdx`,
correctly declare variables before they are used.

Did we get this right? 👍 / 👎 to inform future reviews.

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), {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The generic requestHook documentation example uses Cloudflare-specific env.DATABASE_URL syntax, causing a ReferenceError on other platforms.
Severity: MEDIUM

Suggested Fix

Make the example platform-agnostic by using a generic placeholder like DATABASE_URL instead of env.DATABASE_URL. Alternatively, wrap the example in platform-specific tags to clarify it is for Cloudflare.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: docs/platforms/javascript/common/configuration/integrations/postgresjs.mdx#L67

Potential issue: In the `postgresjs.mdx` documentation, the `requestHook` example under
the generic `## Options` section uses `env.DATABASE_URL`. This `env` object is specific
to the Cloudflare Workers environment and is not available on other platforms like Deno.
Developers on non-Cloudflare platforms who copy this example will encounter a
`ReferenceError` because `env` is not defined. This is inconsistent with other parts of
the document that correctly use platform-specific tags to isolate environment-dependent
code.

Did we get this right? 👍 / 👎 to inform future reviews.

requestHook(span, query) {
span.setAttribute("custom.query", query);
},
});
```
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
| [`extraErrorDataIntegration`](./extraerrordata) | | | | | ✓ |
| [`rewriteFramesIntegration`](./rewriteframes) | | ✓ | | | |
| [`supabaseIntegration`](./supabase) | | ✓ | ✓ | | |
| [`instrumentPostgresJsSql`](./postgresjs) | | | ✓ | | |
| [`honoIntegration`](./hono) | ✓ | ✓ | | | |
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
| [`extraErrorDataIntegration`](./extraerrordata) | | | | | ✓ |
| [`rewriteFramesIntegration`](./rewriteframes) | | ✓ | | | |
| [`supabaseIntegration`](./supabase) | | ✓ | ✓ | | |
| [`instrumentPostgresJsSql`](./postgresjs) | | | ✓ | | |
| [`trpcMiddleware`](./trpc) | | ✓ | ✓ | | ✓ |
| [`zodErrorsIntegration`](./zodErrors) | | | | ✓ |
Loading