-
Notifications
You must be signed in to change notification settings - Fork 838
/
Copy pathmysql.ts
291 lines (254 loc) · 8.7 KB
/
mysql.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*!
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { BasePlugin, isWrapped } from '@opentelemetry/core';
import { CanonicalCode, Span, SpanKind } from '@opentelemetry/api';
import * as mysqlTypes from 'mysql';
import * as shimmer from 'shimmer';
import { AttributeNames } from './enums';
import { getConnectionAttributes, getSpanName } from './utils';
import { VERSION } from './version';
export class MysqlPlugin extends BasePlugin<typeof mysqlTypes> {
readonly supportedVersions = ['2.*'];
static readonly COMPONENT = 'mysql';
static readonly DB_TYPE = 'sql';
static readonly COMMON_ATTRIBUTES = {
[AttributeNames.COMPONENT]: MysqlPlugin.COMPONENT,
[AttributeNames.DB_TYPE]: MysqlPlugin.DB_TYPE,
[AttributeNames.PEER_SERVICE]: MysqlPlugin.COMPONENT,
};
private _enabled = false;
constructor(readonly moduleName: string) {
super('@opentelemetry/plugin-mysql', VERSION);
}
protected patch(): typeof mysqlTypes {
this._enabled = true;
shimmer.wrap(
this._moduleExports,
'createConnection',
this._patchCreateConnection() as any
);
shimmer.wrap(
this._moduleExports,
'createPool',
this._patchCreatePool() as any
);
shimmer.wrap(
this._moduleExports,
'createPoolCluster',
this._patchCreatePoolCluster() as any
);
return this._moduleExports;
}
protected unpatch(): void {
this._enabled = false;
shimmer.unwrap(this._moduleExports, 'createConnection');
shimmer.unwrap(this._moduleExports, 'createPool');
shimmer.unwrap(this._moduleExports, 'createPoolCluster');
}
// global export function
private _patchCreateConnection() {
return (originalCreateConnection: Function) => {
const thisPlugin = this;
thisPlugin._logger.debug(
'MysqlPlugin#patch: patched mysql createConnection'
);
return function createConnection(
_connectionUri: string | mysqlTypes.ConnectionConfig
) {
const originalResult = originalCreateConnection(...arguments);
// This is unwrapped on next call after unpatch
shimmer.wrap(
originalResult,
'query',
thisPlugin._patchQuery(originalResult) as any
);
return originalResult;
};
};
}
// global export function
private _patchCreatePool() {
return (originalCreatePool: Function) => {
const thisPlugin = this;
thisPlugin._logger.debug('MysqlPlugin#patch: patched mysql createPool');
return function createPool(_config: string | mysqlTypes.PoolConfig) {
const pool = originalCreatePool(...arguments);
shimmer.wrap(pool, 'query', thisPlugin._patchQuery(pool));
shimmer.wrap(
pool,
'getConnection',
thisPlugin._patchGetConnection(pool)
);
return pool;
};
};
}
// global export function
private _patchCreatePoolCluster() {
return (originalCreatePoolCluster: Function) => {
const thisPlugin = this;
thisPlugin._logger.debug(
'MysqlPlugin#patch: patched mysql createPoolCluster'
);
return function createPool(_config: string | mysqlTypes.PoolConfig) {
const cluster = originalCreatePoolCluster(...arguments);
// This is unwrapped on next call after unpatch
shimmer.wrap(
cluster,
'getConnection',
thisPlugin._patchGetConnection(cluster)
);
return cluster;
};
};
}
// method on cluster or pool
private _patchGetConnection(pool: mysqlTypes.Pool | mysqlTypes.PoolCluster) {
return (originalGetConnection: Function) => {
const thisPlugin = this;
thisPlugin._logger.debug(
'MysqlPlugin#patch: patched mysql pool getConnection'
);
return function getConnection(
arg1?: unknown,
arg2?: unknown,
arg3?: unknown
) {
// Unwrap if unpatch has been called
if (!thisPlugin._enabled) {
shimmer.unwrap(pool, 'getConnection');
return originalGetConnection.apply(pool, arguments);
}
if (arguments.length === 1 && typeof arg1 === 'function') {
const patchFn = thisPlugin._getConnectionCallbackPatchFn(arg1);
return originalGetConnection.call(pool, patchFn);
}
if (arguments.length === 2 && typeof arg2 === 'function') {
const patchFn = thisPlugin._getConnectionCallbackPatchFn(arg2);
return originalGetConnection.call(pool, arg1, patchFn);
}
if (arguments.length === 3 && typeof arg3 === 'function') {
const patchFn = thisPlugin._getConnectionCallbackPatchFn(arg3);
return originalGetConnection.call(pool, arg1, arg2, patchFn);
}
return originalGetConnection.apply(pool, arguments);
};
};
}
private _getConnectionCallbackPatchFn(cb: Function) {
const thisPlugin = this;
return function() {
if (arguments[1]) {
// this is the callback passed into a query
// no need to unwrap
if (!isWrapped(arguments[1].query)) {
shimmer.wrap(
arguments[1],
'query',
thisPlugin._patchQuery(arguments[1])
);
}
}
if (typeof cb === 'function') {
cb(...arguments);
}
};
}
private _patchQuery(connection: mysqlTypes.Connection | mysqlTypes.Pool) {
return (originalQuery: Function): mysqlTypes.QueryFunction => {
const thisPlugin = this;
thisPlugin._logger.debug('MysqlPlugin: patched mysql query');
return function query(
query: string | mysqlTypes.Query | mysqlTypes.QueryOptions,
_valuesOrCallback?: unknown[] | mysqlTypes.queryCallback,
_callback?: mysqlTypes.queryCallback
) {
if (!thisPlugin._enabled) {
shimmer.unwrap(connection, 'query');
return originalQuery.apply(connection, arguments);
}
const spanName = getSpanName(query);
const span = thisPlugin._tracer.startSpan(spanName, {
kind: SpanKind.CLIENT,
attributes: {
...MysqlPlugin.COMMON_ATTRIBUTES,
...getConnectionAttributes(connection.config),
},
});
if (typeof query === 'string') {
span.setAttribute(AttributeNames.DB_STATEMENT, query);
} else if (typeof query === 'object') {
if (query.sql) {
span.setAttribute(AttributeNames.DB_STATEMENT, query.sql);
}
if (query.values) {
span.setAttribute(AttributeNames.MYSQL_VALUES, query.values);
}
}
if (arguments.length === 1) {
const streamableQuery: mysqlTypes.Query = originalQuery.apply(
connection,
arguments
);
return streamableQuery
.on('error', err =>
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: err.message,
})
)
.on('end', () => {
span.end();
});
}
if (typeof arguments[1] === 'function') {
shimmer.wrap(arguments, 1, thisPlugin._patchCallbackQuery(span));
} else if (typeof arguments[2] === 'function') {
if (Array.isArray(_valuesOrCallback)) {
span.setAttribute(AttributeNames.MYSQL_VALUES, _valuesOrCallback);
} else if (arguments[2]) {
span.setAttribute(AttributeNames.MYSQL_VALUES, [_valuesOrCallback]);
}
shimmer.wrap(arguments, 2, thisPlugin._patchCallbackQuery(span));
}
return originalQuery.apply(connection, arguments);
};
};
}
private _patchCallbackQuery(span: Span) {
return (originalCallback: Function) => {
return function(
err: mysqlTypes.MysqlError | null,
results?: any,
fields?: mysqlTypes.FieldInfo[]
) {
if (err) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: err.message,
});
} else {
span.setStatus({
code: CanonicalCode.OK,
});
}
span.end();
return originalCallback(...arguments);
};
};
}
}
export const plugin = new MysqlPlugin(MysqlPlugin.COMPONENT);