@@ -5,9 +5,15 @@ import { fill, isThenable, loadModule, logger } from '@sentry/utils';
5
5
import type { LazyLoadedIntegration } from './lazy' ;
6
6
import { shouldDisableAutoInstrumentation } from './utils/node-utils' ;
7
7
8
+ type PgClientQuery = (
9
+ config : unknown ,
10
+ values ?: unknown ,
11
+ callback ?: ( err : unknown , result : unknown ) => void ,
12
+ ) => void | Promise < unknown > ;
13
+
8
14
interface PgClient {
9
15
prototype : {
10
- query : ( ) => void | Promise < unknown > ;
16
+ query : PgClientQuery ;
11
17
} ;
12
18
}
13
19
@@ -20,9 +26,23 @@ interface PgClientThis {
20
26
21
27
interface PgOptions {
22
28
usePgNative ?: boolean ;
29
+ /**
30
+ * Supply your postgres module directly, instead of having Sentry attempt automatic resolution.
31
+ * Use this if you (a) use a module that's not `pg`, or (b) use a bundler that breaks resolution (e.g. esbuild).
32
+ *
33
+ * Usage:
34
+ * ```
35
+ * import pg from 'pg';
36
+ *
37
+ * Sentry.init({
38
+ * integrations: [new Sentry.Integrations.Postgres({ module: pg })],
39
+ * });
40
+ * ```
41
+ */
42
+ module ?: PGModule ;
23
43
}
24
44
25
- type PGModule = { Client : PgClient ; native : { Client : PgClient } } ;
45
+ type PGModule = { Client : PgClient ; native : { Client : PgClient } | null } ;
26
46
27
47
/** Tracing integration for node-postgres package */
28
48
export class Postgres implements LazyLoadedIntegration < PGModule > {
@@ -43,6 +63,7 @@ export class Postgres implements LazyLoadedIntegration<PGModule> {
43
63
public constructor ( options : PgOptions = { } ) {
44
64
this . name = Postgres . id ;
45
65
this . _usePgNative = ! ! options . usePgNative ;
66
+ this . _module = options . module ;
46
67
}
47
68
48
69
/** @inheritdoc */
@@ -66,21 +87,21 @@ export class Postgres implements LazyLoadedIntegration<PGModule> {
66
87
return ;
67
88
}
68
89
69
- if ( this . _usePgNative && ! pkg . native ?. Client ) {
90
+ const Client = this . _usePgNative ? pkg . native ?. Client : pkg . Client ;
91
+
92
+ if ( ! Client ) {
70
93
__DEBUG_BUILD__ && logger . error ( "Postgres Integration was unable to access 'pg-native' bindings." ) ;
71
94
return ;
72
95
}
73
96
74
- const { Client } = this . _usePgNative ? pkg . native : pkg ;
75
-
76
97
/**
77
98
* function (query, callback) => void
78
99
* function (query, params, callback) => void
79
100
* function (query) => Promise
80
101
* function (query, params) => Promise
81
102
* function (pg.Cursor) => pg.Cursor
82
103
*/
83
- fill ( Client . prototype , 'query' , function ( orig : ( ) => void | Promise < unknown > ) {
104
+ fill ( Client . prototype , 'query' , function ( orig : PgClientQuery ) {
84
105
return function ( this : PgClientThis , config : unknown , values : unknown , callback : unknown ) {
85
106
const scope = getCurrentHub ( ) . getScope ( ) ;
86
107
const parentSpan = scope . getSpan ( ) ;
0 commit comments