|
| 1 | +/*! |
| 2 | + * Copyright 2019, OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { BasePlugin } from '@opentelemetry/core'; |
| 18 | +import { CanonicalCode, Span } from '@opentelemetry/types'; |
| 19 | +import { |
| 20 | + PostgresPluginOptions, |
| 21 | + PgClientExtended, |
| 22 | + PgPluginQueryConfig, |
| 23 | + PostgresCallback, |
| 24 | +} from './types'; |
| 25 | +import * as pgTypes from 'pg'; |
| 26 | +import * as shimmer from 'shimmer'; |
| 27 | +import * as utils from './utils'; |
| 28 | + |
| 29 | +export class PostgresPlugin extends BasePlugin<typeof pgTypes> { |
| 30 | + protected _config: PostgresPluginOptions; |
| 31 | + |
| 32 | + static readonly COMPONENT = 'pg'; |
| 33 | + static readonly DB_TYPE = 'sql'; |
| 34 | + |
| 35 | + static readonly BASE_SPAN_NAME = PostgresPlugin.COMPONENT + '.query'; |
| 36 | + |
| 37 | + readonly supportedVersions = ['7.*']; |
| 38 | + |
| 39 | + constructor(readonly moduleName: string) { |
| 40 | + super(); |
| 41 | + this._config = {}; |
| 42 | + } |
| 43 | + |
| 44 | + protected patch(): typeof pgTypes { |
| 45 | + if (this._moduleExports.Client.prototype.query) { |
| 46 | + shimmer.wrap( |
| 47 | + this._moduleExports.Client.prototype, |
| 48 | + 'query', |
| 49 | + this._getClientQueryPatch() as never |
| 50 | + ); |
| 51 | + } |
| 52 | + return this._moduleExports; |
| 53 | + } |
| 54 | + |
| 55 | + protected unpatch(): void { |
| 56 | + if (this._moduleExports.Client.prototype.query) { |
| 57 | + shimmer.unwrap(this._moduleExports.Client.prototype, 'query'); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private _getClientQueryPatch() { |
| 62 | + const plugin = this; |
| 63 | + return (original: typeof pgTypes.Client.prototype.query) => { |
| 64 | + plugin._logger.debug( |
| 65 | + `Patching ${PostgresPlugin.COMPONENT}.Client.prototype.query` |
| 66 | + ); |
| 67 | + return function query( |
| 68 | + this: pgTypes.Client & PgClientExtended, |
| 69 | + ...args: unknown[] |
| 70 | + ) { |
| 71 | + let span: Span; |
| 72 | + |
| 73 | + // Handle different client.query(...) signatures |
| 74 | + if (typeof args[0] === 'string') { |
| 75 | + if (args.length > 1 && args[1] instanceof Array) { |
| 76 | + span = utils.handleParameterizedQuery.call( |
| 77 | + this, |
| 78 | + plugin._tracer, |
| 79 | + ...args |
| 80 | + ); |
| 81 | + } else { |
| 82 | + span = utils.handleTextQuery.call(this, plugin._tracer, ...args); |
| 83 | + } |
| 84 | + } else if (typeof args[0] === 'object') { |
| 85 | + span = utils.handleConfigQuery.call(this, plugin._tracer, ...args); |
| 86 | + } else { |
| 87 | + return utils.handleInvalidQuery.call( |
| 88 | + this, |
| 89 | + plugin._tracer, |
| 90 | + original, |
| 91 | + ...args |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + // Bind callback to parent span |
| 96 | + if (args.length > 0) { |
| 97 | + const parentSpan = plugin._tracer.getCurrentSpan(); |
| 98 | + if (typeof args[args.length - 1] === 'function') { |
| 99 | + // Patch ParameterQuery callback |
| 100 | + args[args.length - 1] = utils.patchCallback(span, args[ |
| 101 | + args.length - 1 |
| 102 | + ] as PostgresCallback); |
| 103 | + // If a parent span exists, bind the callback |
| 104 | + if (parentSpan) { |
| 105 | + args[args.length - 1] = plugin._tracer.bind( |
| 106 | + args[args.length - 1] |
| 107 | + ); |
| 108 | + } |
| 109 | + } else if ( |
| 110 | + typeof (args[0] as PgPluginQueryConfig).callback === 'function' |
| 111 | + ) { |
| 112 | + // Patch ConfigQuery callback |
| 113 | + let callback = utils.patchCallback( |
| 114 | + span, |
| 115 | + (args[0] as PgPluginQueryConfig).callback! |
| 116 | + ); |
| 117 | + // If a parent span existed, bind the callback |
| 118 | + if (parentSpan) { |
| 119 | + callback = plugin._tracer.bind(callback); |
| 120 | + } |
| 121 | + |
| 122 | + // Copy the callback instead of writing to args.callback so that we don't modify user's |
| 123 | + // original callback reference |
| 124 | + args[0] = { ...(args[0] as object), callback }; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Perform the original query |
| 129 | + const result: unknown = original.apply(this, args as never); |
| 130 | + |
| 131 | + // Bind promise to parent span and end the span |
| 132 | + if (result instanceof Promise) { |
| 133 | + return result |
| 134 | + .then((result: unknown) => { |
| 135 | + // Return a pass-along promise which ends the span and then goes to user's orig resolvers |
| 136 | + return new Promise((resolve, _) => { |
| 137 | + span.setStatus({ code: CanonicalCode.OK }); |
| 138 | + span.end(); |
| 139 | + resolve(result); |
| 140 | + }); |
| 141 | + }) |
| 142 | + .catch((error: Error) => { |
| 143 | + return new Promise((_, reject) => { |
| 144 | + span.setStatus({ |
| 145 | + code: CanonicalCode.UNKNOWN, |
| 146 | + message: error.message, |
| 147 | + }); |
| 148 | + span.end(); |
| 149 | + reject(error); |
| 150 | + }); |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + // else returns void |
| 155 | + return result; // void |
| 156 | + }; |
| 157 | + }; |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +export const plugin = new PostgresPlugin(PostgresPlugin.COMPONENT); |
0 commit comments