|
13 | 13 | * See the License for the specific language governing permissions and
|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
| 16 | + |
| 17 | +import { BasePlugin } from '@opentelemetry/core'; |
| 18 | +import { CanonicalCode, SpanKind } from '@opentelemetry/types'; |
| 19 | +import { AttributeNames } from './enums'; |
| 20 | +import * as shimmer from 'shimmer'; |
| 21 | +import * as pgPoolTypes from 'pg-pool'; |
| 22 | +import { |
| 23 | + PostgresPoolPluginOptions, |
| 24 | + PgPoolCallback, |
| 25 | + PgPoolExtended, |
| 26 | +} from './types'; |
| 27 | +import * as utils from './utils'; |
| 28 | + |
| 29 | +export class PostgresPoolPlugin extends BasePlugin<typeof pgPoolTypes> { |
| 30 | + protected _config: PostgresPoolPluginOptions; |
| 31 | + |
| 32 | + static readonly COMPONENT = 'pg-pool'; |
| 33 | + static readonly DB_TYPE = 'sql'; |
| 34 | + |
| 35 | + readonly supportedVersions = ['2.*']; |
| 36 | + |
| 37 | + constructor(readonly moduleName: string) { |
| 38 | + super(); |
| 39 | + this._config = {}; |
| 40 | + } |
| 41 | + |
| 42 | + protected patch(): typeof pgPoolTypes { |
| 43 | + shimmer.wrap( |
| 44 | + this._moduleExports.prototype, |
| 45 | + 'connect', |
| 46 | + this._getPoolConnectPatch() as never |
| 47 | + ); |
| 48 | + |
| 49 | + return this._moduleExports; |
| 50 | + } |
| 51 | + |
| 52 | + protected unpatch(): void { |
| 53 | + shimmer.unwrap(this._moduleExports.prototype, 'connect'); |
| 54 | + } |
| 55 | + |
| 56 | + private _getPoolConnectPatch() { |
| 57 | + const plugin = this; |
| 58 | + return (originalConnect: typeof pgPoolTypes.prototype.connect) => { |
| 59 | + plugin._logger.debug( |
| 60 | + `Patching ${PostgresPoolPlugin.COMPONENT}.prototype.connect` |
| 61 | + ); |
| 62 | + return function connect(this: PgPoolExtended, callback?: PgPoolCallback) { |
| 63 | + const jdbcString = utils.getJDBCString(this.options); |
| 64 | + // setup span |
| 65 | + const span = plugin._tracer.startSpan( |
| 66 | + `${PostgresPoolPlugin.COMPONENT}.connect`, |
| 67 | + { |
| 68 | + kind: SpanKind.CLIENT, |
| 69 | + parent: plugin._tracer.getCurrentSpan() || undefined, |
| 70 | + attributes: { |
| 71 | + [AttributeNames.COMPONENT]: PostgresPoolPlugin.COMPONENT, // required |
| 72 | + [AttributeNames.DB_TYPE]: PostgresPoolPlugin.DB_TYPE, // required |
| 73 | + [AttributeNames.DB_INSTANCE]: this.options.database, // required |
| 74 | + [AttributeNames.PEER_HOSTNAME]: this.options.host, // required |
| 75 | + [AttributeNames.PEER_ADDRESS]: jdbcString, // required |
| 76 | + [AttributeNames.PEER_PORT]: this.options.port, |
| 77 | + [AttributeNames.DB_USER]: this.options.user, |
| 78 | + [AttributeNames.IDLE_TIMEOUT_MILLIS]: this.options |
| 79 | + .idleTimeoutMillis, |
| 80 | + [AttributeNames.MAX_CLIENT]: this.options.maxClient, |
| 81 | + }, |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + if (callback) { |
| 86 | + const parentSpan = plugin._tracer.getCurrentSpan(); |
| 87 | + callback = utils.patchCallback(span, callback) as PgPoolCallback; |
| 88 | + // If a parent span exists, bind the callback |
| 89 | + if (parentSpan) { |
| 90 | + callback = plugin._tracer.bind(callback); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + const connectResult: unknown = originalConnect.call( |
| 95 | + this, |
| 96 | + callback as never |
| 97 | + ); |
| 98 | + |
| 99 | + // No callback was provided, return a promise instead |
| 100 | + if (connectResult instanceof Promise) { |
| 101 | + const connectResultPromise = connectResult as Promise<unknown>; |
| 102 | + return plugin._tracer.bind( |
| 103 | + connectResultPromise |
| 104 | + .then((result: any) => { |
| 105 | + // Resturn a pass-along promise which ends the span and then goes to user's orig resolvers |
| 106 | + return new Promise((resolve, _) => { |
| 107 | + span.setStatus({ code: CanonicalCode.OK }); |
| 108 | + span.end(); |
| 109 | + resolve(result); |
| 110 | + }); |
| 111 | + }) |
| 112 | + .catch((error: Error) => { |
| 113 | + return new Promise((_, reject) => { |
| 114 | + span.setStatus({ |
| 115 | + code: CanonicalCode.UNKNOWN, |
| 116 | + message: error.message, |
| 117 | + }); |
| 118 | + span.end(); |
| 119 | + reject(error); |
| 120 | + }); |
| 121 | + }) |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + // Else a callback was provided, so just return the result |
| 126 | + return connectResult; |
| 127 | + }; |
| 128 | + }; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +export const plugin = new PostgresPoolPlugin(PostgresPoolPlugin.COMPONENT); |
0 commit comments