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
6 changes: 4 additions & 2 deletions packages/core/src/integrations/postgresjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,10 @@ function _wrapSingleQueryHandle(

// IMPORTANT: We must replace the handle function directly, not use a Proxy,
// because Query.then() internally calls this.handle(), which would bypass a Proxy wrapper.
const wrappedHandle = async function (this: unknown, ...args: unknown[]): Promise<unknown> {
if (!_shouldCreateSpans(options)) {
const wrappedHandle = async function (this: { executed?: boolean }, ...args: unknown[]): Promise<unknown> {
// postgres.js calls handle() from then/catch/finally — only the first call executes SQL,
// subsequent calls are no-ops (guarded by this.executed). Skip span creation for no-ops.
if (this.executed || !_shouldCreateSpans(options)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

q: Is this available on all versions that we support?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, seems to have been available since v3 which we target with >= 3.0.0 < 4.

return originalHandle.apply(this, args);
}

Expand Down
37 changes: 37 additions & 0 deletions packages/core/test/lib/integrations/postgresjs.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { _reconstructQuery, _sanitizeSqlQuery, instrumentPostgresJsSql } from '../../../src/integrations/postgresjs';
import * as tracing from '../../../src/tracing';
import * as spanUtils from '../../../src/utils/spanUtils';

describe('PostgresJs portable instrumentation', () => {
Expand Down Expand Up @@ -554,6 +555,42 @@ describe('PostgresJs portable instrumentation', () => {
// handle was wrapped
expect((mockQuery.handle as any).__sentryWrapped).toBe(true);
});

it('only creates one span even when handle() is called multiple times', async () => {
const mockSpan = { setAttribute: vi.fn(), setAttributes: vi.fn(), end: vi.fn() };
const startSpanManualSpy = vi
.spyOn(tracing, 'startSpanManual')
.mockImplementation((_opts, callback) => callback(mockSpan as any, () => {}));

const originalHandle = vi.fn().mockResolvedValue([]);
const mockQuery = {
handle: originalHandle,
strings: ['SELECT 1'],
resolve: vi.fn(),
reject: vi.fn(),
executed: false,
};
const mockSql = vi.fn().mockReturnValue(mockQuery);

const instrumented = instrumentPostgresJsSql(mockSql, { requireParentSpan: false });
instrumented(['SELECT 1']);

const wrappedHandle = mockQuery.handle as (...args: unknown[]) => Promise<unknown>;

// First call — executed is false, should create a span
await wrappedHandle.call(mockQuery);
expect(startSpanManualSpy).toHaveBeenCalledTimes(1);

// Simulate postgres.js setting executed = true after first handle()
mockQuery.executed = true;

// Second and third calls (from .then/.catch/.finally) — should NOT create more spans
await wrappedHandle.call(mockQuery);
await wrappedHandle.call(mockQuery);
expect(startSpanManualSpy).toHaveBeenCalledTimes(1);

startSpanManualSpy.mockRestore();
});
});

it('does not wrap non-query results from sql call', () => {
Expand Down
7 changes: 5 additions & 2 deletions packages/node/src/integrations/tracing/postgresjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,14 @@ export class PostgresJsInstrumentation extends InstrumentationBase<PostgresJsIns
resolve: unknown;
reject: unknown;
strings?: string[];
executed?: boolean;
},
...args: unknown[]
): Promise<unknown> {
// Skip if this query came from an instrumented sql instance (already handled by wrapper)
if ((this as Record<symbol, unknown>)[QUERY_FROM_INSTRUMENTED_SQL]) {
// Skip if this query came from an instrumented sql instance (already handled by wrapper),
// or if handle() was already called (postgres.js calls handle() from then/catch/finally —
// only the first call executes SQL, subsequent calls are no-ops).
if (this.executed || (this as Record<symbol, unknown>)[QUERY_FROM_INSTRUMENTED_SQL]) {
return originalHandle.apply(this, args);
}

Expand Down
Loading