forked from elastic/apm-agent-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcassandra-driver.js
280 lines (242 loc) · 7.18 KB
/
cassandra-driver.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
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
/*
* 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.
*/
'use strict';
const semver = require('semver');
const sqlSummary = require('sql-summary');
const shimmer = require('../shimmer');
module.exports = function (cassandra, agent, { version, enabled }) {
if (!enabled) return cassandra;
if (!semver.satisfies(version, '>=3 <5')) {
agent.logger.debug(
'cassandra-driver version %s not supported - aborting...',
version,
);
return cassandra;
}
const ins = agent._instrumentation;
if (cassandra.Client) {
if (semver.gte(version, '4.4.0')) {
// Prior to v4.4.0, the regular `connect` function would be called by the
// other functions (e.g. `execute`). In newer versions an internal
// `_connect` function is called instead (this is also called by
// `connect`).
shimmer.wrap(cassandra.Client.prototype, '_connect', wrapAsyncConnect);
} else {
shimmer.wrap(cassandra.Client.prototype, 'connect', wrapConnect);
}
shimmer.wrap(cassandra.Client.prototype, 'execute', wrapExecute);
shimmer.wrap(cassandra.Client.prototype, 'eachRow', wrapEachRow);
shimmer.wrap(cassandra.Client.prototype, 'batch', wrapBatch);
}
return cassandra;
function wrapAsyncConnect(original) {
return async function wrappedAsyncConnect() {
const span = ins.createSpan(
'Cassandra: Connect',
'db',
'cassandra',
'connect',
{ exitSpan: true },
);
if (!span) {
return original.apply(this, arguments);
}
const dbContext = { type: 'cassandra' };
if (this.keyspace) {
dbContext.instance = this.keyspace;
}
span.setDbContext(dbContext);
try {
return await original.apply(this, arguments);
} finally {
span.end();
}
};
}
function wrapConnect(original) {
return function wrappedConnect(callback) {
const span = ins.createSpan(
'Cassandra: Connect',
'db',
'cassandra',
'connect',
{ exitSpan: true },
);
if (!span) {
return original.apply(this, arguments);
}
const dbContext = { type: 'cassandra' };
if (this.keyspace) {
dbContext.instance = this.keyspace;
}
span.setDbContext(dbContext);
function resolve() {
span.end();
}
// Wrap the callback
const ret = original.call(this, wrapCallback(callback));
if (typeof callback !== 'function') {
if (typeof ret.then === 'function') {
ret.then(resolve, resolve);
} else {
agent.logger.error(
'unable to identify span exit point for cassandra-driver',
);
}
}
return ret;
function wrapCallback(cb) {
if (typeof cb !== 'function') return cb;
return function wrappedCallback() {
resolve();
return cb.apply(this, arguments);
};
}
};
}
function toQueryString(query) {
return query.query;
}
function wrapBatch(original) {
return function wrappedBatch(queries, options, callback) {
const span = ins.createSpan(
'Cassandra: Batch query',
'db',
'cassandra',
'query',
{ exitSpan: true },
);
if (!span) {
return original.apply(this, arguments);
}
const queryStrings = queries.map(toQueryString);
const query = queryStrings.join(';\n');
const dbContext = { type: 'cassandra', statement: query };
const keyspace =
(options && typeof options === 'object' && options.keyspace) ||
this.keyspace;
if (keyspace) {
dbContext.instance = keyspace;
}
span.setDbContext(dbContext);
function resolve() {
span.end();
}
// Wrap the callback
const index = arguments.length - 1;
const cb = arguments[index];
const isPromise = typeof cb !== 'function';
if (!isPromise) {
arguments[index] = function wrappedCallback() {
resolve();
return cb.apply(this, arguments);
};
}
const ret = original.apply(this, arguments);
if (isPromise) {
if (typeof ret.then === 'function') {
ret.then(resolve, resolve);
} else {
agent.logger.error(
'unable to identify span exit point for cassandra-driver',
);
}
}
return ret;
};
}
function wrapExecute(original) {
return function wrappedExecute(query, params, options, callback) {
const span = ins.createSpan(null, 'db', 'cassandra', 'query', {
exitSpan: true,
});
if (!span) {
return original.apply(this, arguments);
}
span.name = sqlSummary(query);
const dbContext = { type: 'cassandra', statement: query };
const keyspace =
(options && typeof options === 'object' && options.keyspace) ||
this.keyspace;
if (keyspace) {
dbContext.instance = keyspace;
}
span.setDbContext(dbContext);
function resolve() {
span.end();
}
// Wrap the callback
const index = arguments.length - 1;
const cb = arguments[index];
const isPromise = typeof cb !== 'function';
if (!isPromise) {
arguments[index] = function wrappedCallback() {
resolve();
return cb.apply(this, arguments);
};
}
const ret = original.apply(this, arguments);
if (isPromise) {
if (typeof ret.then === 'function') {
ret.then(resolve, resolve);
} else {
agent.logger.error(
'unable to identify span exit point for cassandra-driver',
);
}
}
return ret;
};
}
function wrapEachRow(original) {
return function wrappedEachRow(
query,
params,
options,
rowCallback,
callback,
) {
const span = ins.createSpan(null, 'db', 'cassandra', 'query', {
exitSpan: true,
});
if (!span) {
return original.apply(this, arguments);
}
span.name = sqlSummary(query);
const dbContext = { type: 'cassandra', statement: query };
const keyspace =
(options && typeof options === 'object' && options.keyspace) ||
this.keyspace;
if (keyspace) {
dbContext.instance = keyspace;
}
span.setDbContext(dbContext);
// Wrap the callback
const index = arguments.length - 1;
const hasRowCallback = typeof arguments[index - 1] === 'function';
function resolve() {
span.end();
}
if (hasRowCallback) {
const cb = arguments[index];
if (typeof cb === 'function') {
arguments[index] = function wrappedCallback() {
resolve();
return cb.apply(this, arguments);
};
} else {
agent.logger.error(
'unable to identify span exit point for cassandra-driver',
);
}
} else {
arguments[index + 1] = resolve;
arguments.length++;
}
return original.apply(this, arguments);
};
}
};