-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathknex.js
91 lines (75 loc) · 2.54 KB
/
knex.js
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
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/
// Knex instrumentation exists to capture a more useful span stacktrace at
// the start of a Knex query, and use that stacktrace on the 'pg' or 'mysql'
// span.
'use strict';
var semver = require('semver');
var shimmer = require('../shimmer');
var symbols = require('../../symbols');
module.exports = function (Knex, agent, { version, enabled }) {
if (!enabled) {
return Knex;
}
if (!semver.satisfies(version, '>=0.10.0 <4.0.0')) {
agent.logger.debug(
`knex@${version} is not supported, skipping knex instrumentation`,
);
return Knex;
}
if (agent._conf.spanStackTraceMinDuration < 0) {
agent.logger.trace(
'not instrumenting knex because not capturing span stack traces (spanStackTraceMinDuration=%s)',
agent._conf.spanStackTraceMinDuration,
);
return Knex;
}
function wrapQueryStartPoint(original) {
return function wrappedQueryStartPoint() {
var builder = original.apply(this, arguments);
agent.logger.debug('capturing custom stack trace for knex');
var obj = {};
Error.captureStackTrace(obj);
builder[symbols.knexStackObj] = obj;
return builder;
};
}
function wrapRunner(original) {
return function wrappedRunner() {
var runner = original.apply(this, arguments);
agent.logger.debug('shimming knex runner.query');
shimmer.wrap(runner, 'query', wrapQuery);
return runner;
};
}
function wrapQuery(original) {
return function wrappedQuery() {
agent.logger.debug('intercepted call to knex runner.query');
if (this.connection) {
this.connection[symbols.knexStackObj] = this.builder
? this.builder[symbols.knexStackObj]
: null;
}
return original.apply(this, arguments);
};
}
return function wrappedKnex() {
const knexInstance = Knex.apply(null, arguments);
if (knexInstance && knexInstance.client) {
const QUERY_FNS = ['queryBuilder', 'raw'];
agent.logger.debug('shimming knexInstance.client.runner');
shimmer.wrap(knexInstance.client, 'runner', wrapRunner);
agent.logger.debug(
'shimming Knex.Client.prototype functions: %j',
QUERY_FNS,
);
shimmer.massWrap(knexInstance.client, QUERY_FNS, wrapQueryStartPoint);
} else {
agent.logger.debug('could not shim Knex');
}
return knexInstance;
};
};