|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | | -'use strict'; |
17 | 16 |
|
18 | | -var shimmer = require('shimmer'); |
| 17 | +import {EventEmitter} from 'events'; |
| 18 | +import * as shimmer from 'shimmer'; |
| 19 | +import {Readable} from 'stream'; |
19 | 20 |
|
20 | | -var SUPPORTED_VERSIONS = '^6.x || ^7.x'; |
| 21 | +import {Patch, Plugin, SpanData} from '../plugin-types'; |
21 | 22 |
|
22 | | -module.exports = [ |
| 23 | +import {pg_6, pg_7} from './types'; |
| 24 | + |
| 25 | +// TS: Client#query also accepts a callback as a last argument, but TS cannot |
| 26 | +// detect this as it's a dependent type. So we don't specify it here. |
| 27 | +type ClientQueryArguments = |
| 28 | + [{submit?: Function} & pg_7.QueryConfig]|[string]|[string, {}]; |
| 29 | +type PG7QueryReturnValue = (pg_7.QueryConfig&({submit: Function}&EventEmitter)| |
| 30 | + pg_7.Query)|Promise<pg_7.QueryResult>; |
| 31 | + |
| 32 | +// tslint:disable-next-line:no-any |
| 33 | +function isSubmittable(obj: any): obj is {submit: Function} { |
| 34 | + return typeof obj.submit === 'function'; |
| 35 | +} |
| 36 | + |
| 37 | +const noOp = () => {}; |
| 38 | + |
| 39 | +function populateLabelsFromInputs(span: SpanData, args: ClientQueryArguments) { |
| 40 | + const queryObj = args[0]; |
| 41 | + if (typeof queryObj === 'object') { |
| 42 | + if (queryObj.text) { |
| 43 | + span.addLabel('query', queryObj.text); |
| 44 | + } |
| 45 | + if (queryObj.values) { |
| 46 | + span.addLabel('values', queryObj.values); |
| 47 | + } |
| 48 | + } else if (typeof queryObj === 'string') { |
| 49 | + span.addLabel('query', queryObj); |
| 50 | + if (args.length >= 2 && typeof args[1] !== 'function') { |
| 51 | + span.addLabel('values', args[1]); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +function populateLabelsFromOutputs( |
| 57 | + span: SpanData, err: Error|null, res?: pg_7.QueryResult) { |
| 58 | + if (err) { |
| 59 | + span.addLabel('error', err); |
| 60 | + } |
| 61 | + if (res) { |
| 62 | + span.addLabel('row_count', res.rowCount); |
| 63 | + span.addLabel('oid', res.oid); |
| 64 | + span.addLabel('rows', res.rows); |
| 65 | + span.addLabel('fields', res.fields); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +const plugin: Plugin = [ |
23 | 70 | { |
24 | 71 | file: 'lib/client.js', |
25 | | - versions: SUPPORTED_VERSIONS, |
26 | | - patch: function(Client, api) { |
27 | | - function queryWrap(query) { |
28 | | - return function query_trace() { |
29 | | - var span = api.createChildSpan({ |
30 | | - name: 'pg-query' |
31 | | - }); |
32 | | - var pgQuery = query.apply(this, arguments); |
| 72 | + versions: '^6.x', |
| 73 | + // TS: Client is a class name. |
| 74 | + // tslint:disable-next-line:variable-name |
| 75 | + patch: (Client, api) => { |
| 76 | + const maybePopulateLabelsFromInputs = |
| 77 | + api.enhancedDatabaseReportingEnabled() ? populateLabelsFromInputs : |
| 78 | + noOp; |
| 79 | + const maybePopulateLabelsFromOutputs = |
| 80 | + api.enhancedDatabaseReportingEnabled() ? populateLabelsFromOutputs : |
| 81 | + noOp; |
| 82 | + shimmer.wrap(Client.prototype, 'query', (query) => { |
| 83 | + return function query_trace(this: pg_6.Client) { |
| 84 | + const span = api.createChildSpan({name: 'pg-query'}); |
33 | 85 | if (!api.isRealSpan(span)) { |
34 | | - return pgQuery; |
| 86 | + return query.apply(this, arguments); |
35 | 87 | } |
36 | | - if (api.enhancedDatabaseReportingEnabled()) { |
37 | | - span.addLabel('query', pgQuery.text); |
38 | | - if (pgQuery.values) { |
39 | | - span.addLabel('values', pgQuery.values); |
40 | | - } |
| 88 | + const argLength = arguments.length; |
| 89 | + if (argLength >= 1) { |
| 90 | + const args: ClientQueryArguments = |
| 91 | + Array.prototype.slice.call(arguments, 0); |
| 92 | + // Extract query text and values, if needed. |
| 93 | + maybePopulateLabelsFromInputs(span, args); |
41 | 94 | } |
| 95 | + const pgQuery: pg_6.QueryReturnValue = query.apply(this, arguments); |
42 | 96 | api.wrapEmitter(pgQuery); |
43 | | - var done = pgQuery.callback; |
44 | | - pgQuery.callback = api.wrap(function(err, res) { |
45 | | - if (api.enhancedDatabaseReportingEnabled()) { |
46 | | - if (err) { |
47 | | - span.addLabel('error', err); |
48 | | - } |
49 | | - if (res) { |
50 | | - span.addLabel('row_count', res.rowCount); |
51 | | - span.addLabel('oid', res.oid); |
52 | | - span.addLabel('rows', res.rows); |
53 | | - span.addLabel('fields', res.fields); |
54 | | - } |
| 97 | + const done = pgQuery.callback; |
| 98 | + // TODO(kjin): Clean up this line a little bit by casting the function |
| 99 | + // passed to api.wrap as a NonNullable<typeof done>. |
| 100 | + pgQuery.callback = |
| 101 | + api.wrap((err: Error|null, res?: pg_7.QueryResult) => { |
| 102 | + maybePopulateLabelsFromOutputs(span, err, res); |
| 103 | + span.endSpan(); |
| 104 | + if (done) { |
| 105 | + done(err, res); |
| 106 | + } |
| 107 | + }); |
| 108 | + return pgQuery; |
| 109 | + }; |
| 110 | + }); |
| 111 | + }, |
| 112 | + // TS: Client is a class name. |
| 113 | + // tslint:disable-next-line:variable-name |
| 114 | + unpatch(Client) { |
| 115 | + shimmer.unwrap(Client.prototype, 'query'); |
| 116 | + } |
| 117 | + } as Patch<typeof pg_6.Client>, |
| 118 | + { |
| 119 | + file: 'lib/client.js', |
| 120 | + versions: '^7.x', |
| 121 | + // TS: Client is a class name. |
| 122 | + // tslint:disable-next-line:variable-name |
| 123 | + patch: (Client, api) => { |
| 124 | + const maybePopulateLabelsFromInputs = |
| 125 | + api.enhancedDatabaseReportingEnabled() ? populateLabelsFromInputs : |
| 126 | + noOp; |
| 127 | + const maybePopulateLabelsFromOutputs = |
| 128 | + api.enhancedDatabaseReportingEnabled() ? populateLabelsFromOutputs : |
| 129 | + noOp; |
| 130 | + shimmer.wrap(Client.prototype, 'query', (query) => { |
| 131 | + return function query_trace(this: pg_7.Client) { |
| 132 | + const span = api.createChildSpan({name: 'pg-query'}); |
| 133 | + if (!api.isRealSpan(span)) { |
| 134 | + return query.apply(this, arguments); |
| 135 | + } |
| 136 | + |
| 137 | + let pgQuery: PG7QueryReturnValue; |
| 138 | + // In 7.x, the value of pgQuery depends on how the query() was called. |
| 139 | + // It can be one of: |
| 140 | + // - (query: pg.Submittable) => EventEmitter |
| 141 | + // - Note: return value is the same as the argument. |
| 142 | + // - ([*], callback: (err, res: pg.Result) => void) => void |
| 143 | + // - ([*]) => Promise<pg.Result> |
| 144 | + // where [*] is one of: |
| 145 | + // - ...[query: { text: string, values?: Array<any> }] |
| 146 | + // - ...[text: string, values?: Array<any>] |
| 147 | + // See: https://node-postgres.com/guides/upgrading |
| 148 | + const argLength = arguments.length; |
| 149 | + if (argLength >= 1) { |
| 150 | + const args: ClientQueryArguments = |
| 151 | + Array.prototype.slice.call(arguments, 0); |
| 152 | + |
| 153 | + // Extract query text and values, if needed. |
| 154 | + maybePopulateLabelsFromInputs(span, args); |
| 155 | + |
| 156 | + // If we received a callback, bind it to the current context, |
| 157 | + // optionally adding labels as well. |
| 158 | + const callback = args[args.length - 1]; |
| 159 | + if (typeof callback === 'function') { |
| 160 | + args[args.length - 1] = |
| 161 | + api.wrap((err: Error|null, res?: pg_7.QueryResult) => { |
| 162 | + maybePopulateLabelsFromOutputs(span, err, res); |
| 163 | + span.endSpan(); |
| 164 | + // TS: Type cast is safe as we know that callback is a |
| 165 | + // Function. |
| 166 | + (callback as (err: Error|null, res?: pg_7.QueryResult) => |
| 167 | + void)(err, res); |
| 168 | + }); |
| 169 | + pgQuery = query.apply(this, args); |
| 170 | + } else { |
| 171 | + pgQuery = query.apply(this, arguments); |
55 | 172 | } |
56 | | - span.endSpan(); |
57 | | - if (done) { |
58 | | - done(err, res); |
| 173 | + } else { |
| 174 | + pgQuery = query.apply(this, arguments); |
| 175 | + } |
| 176 | + |
| 177 | + if (pgQuery) { |
| 178 | + if (pgQuery instanceof EventEmitter) { |
| 179 | + api.wrapEmitter(pgQuery); |
| 180 | + } else if (typeof pgQuery.then === 'function') { |
| 181 | + // Ensure that the span is ended, optionally adding labels as |
| 182 | + // well. |
| 183 | + pgQuery = pgQuery.then( |
| 184 | + (res) => { |
| 185 | + maybePopulateLabelsFromOutputs(span, null, res); |
| 186 | + span.endSpan(); |
| 187 | + return res; |
| 188 | + }, |
| 189 | + (err) => { |
| 190 | + maybePopulateLabelsFromOutputs(span, err); |
| 191 | + span.endSpan(); |
| 192 | + throw err; |
| 193 | + }); |
59 | 194 | } |
60 | | - }); |
| 195 | + } |
61 | 196 | return pgQuery; |
62 | 197 | }; |
63 | | - } |
64 | | - |
65 | | - shimmer.wrap(Client.prototype, 'query', queryWrap); |
| 198 | + }); |
66 | 199 | }, |
67 | | - unpatch: function(Client) { |
| 200 | + // TS: Client is a class name. |
| 201 | + // tslint:disable-next-line:variable-name |
| 202 | + unpatch(Client) { |
68 | 203 | shimmer.unwrap(Client.prototype, 'query'); |
69 | 204 | } |
70 | | - } |
| 205 | + } as Patch<typeof pg_7.Client> |
71 | 206 | ]; |
72 | 207 |
|
73 | | -export default {}; |
| 208 | +export = plugin; |
0 commit comments