diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts index 774c1372756..a5eff144989 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts @@ -33,7 +33,6 @@ import type * as pgPoolTypes from 'pg-pool'; import { PgClientConnect, PgClientExtended, - PgErrorCallback, PostgresCallback, PgPoolExtended, PgPoolCallback, @@ -133,10 +132,7 @@ export class PgInstrumentation extends InstrumentationBase { private _getClientConnectPatch() { const plugin = this; return (original: PgClientConnect) => { - return function connect( - this: pgTypes.Client, - callback?: PgErrorCallback - ) { + return function connect(this: pgTypes.Client, callback?: Function) { if (utils.shouldSkipInstrumentation(plugin.getConfig())) { return original.call(this, callback); } diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/internal-types.ts b/plugins/node/opentelemetry-instrumentation-pg/src/internal-types.ts index 546d84b9ee7..12708144996 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/internal-types.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/internal-types.ts @@ -46,8 +46,6 @@ export type PgPoolCallback = ( done: (release?: any) => void ) => void; -export type PgErrorCallback = (err: Error) => void; - export interface PgPoolOptionsParams { database: string; host: string; @@ -61,6 +59,4 @@ export interface PgPoolExtended extends pgPoolTypes { options: PgPoolOptionsParams; } -export type PgClientConnect = ( - callback?: (err: Error) => void -) => Promise | void; +export type PgClientConnect = (callback?: Function) => Promise | void; diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts b/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts index 47985c9a57c..2e4176817df 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/utils.ts @@ -34,7 +34,6 @@ import { import { PgClientExtended, PostgresCallback, - PgErrorCallback, PgPoolCallback, PgPoolExtended, PgParsedConnectionParams, @@ -239,10 +238,7 @@ export function patchCallbackPGPool( }; } -export function patchClientConnectCallback( - span: Span, - cb: PgErrorCallback -): PgErrorCallback { +export function patchClientConnectCallback(span: Span, cb: Function): Function { return function patchedClientConnectCallback( this: pgTypes.Client, err: Error @@ -254,7 +250,7 @@ export function patchClientConnectCallback( }); } span.end(); - cb.call(this, err); + cb.apply(this, arguments); }; } diff --git a/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts b/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts index f447e3e7ec1..b78e036ab6f 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/test/pg.test.ts @@ -247,6 +247,17 @@ describe('pg', () => { assert.strictEqual(res, undefined, 'No promise is returned'); }); + it('should pass the client connection object in the callback function', done => { + connClient.connect(function (err: Error) { + // Even though the documented signature for connect() callback is `(err) => void` + // `pg` actually also passes the client if the connection was successful and some + // packages(`knex`) might rely on that + // https://github.com/brianc/node-postgres/blob/master/packages/pg/lib/client.js#L282 + assert.strictEqual(arguments[1], connClient); + done(); + }); + }); + it('should return a promise if callback is not provided', done => { const resPromise = connClient.connect(); resPromise