forked from datastax/nodejs-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsights-client.js
492 lines (423 loc) · 14.9 KB
/
insights-client.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* Copyright DataStax, Inc.
*
* 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
*
* http://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.
*/
'use strict';
const os = require('os');
const path = require('path');
const fs = require('fs');
const utils = require('./utils');
const promiseUtils = require('./promise-utils');
const types = require('./types');
const requests = require('./requests');
const { ExecutionOptions } = require('./execution-options');
const packageInfo = require('../package.json');
const VersionNumber = require('./types/version-number');
const { NoAuthProvider } = require('./auth');
let kerberosModule;
try {
// eslint-disable-next-line
kerberosModule = require('kerberos');
}
catch (err) {
// Kerberos is an optional dependency
}
const minDse6Version = new VersionNumber(6, 0, 5);
const minDse51Version = new VersionNumber(5, 1, 13);
const dse600Version = new VersionNumber(6, 0, 0);
const rpc = "CALL InsightsRpc.reportInsight(?)";
const maxStatusErrorLogs = 5;
/**
* Contains methods and functionality to send events to DSE Insights.
*/
class InsightsClient {
/**
* Creates a new instance of the {@link InsightsClient} using the driver {@link Client}.
* @param {Client} client
* @param {Object} [options]
* @param {Number} [options.statusEventDelay]
* @param {Function} [options.errorCallback]
*/
constructor(client, options) {
this._client = client;
this._sessionId = types.Uuid.random().toString();
this._enabled = false;
this._closed = false;
this._firstTimeout = null;
this._recurrentTimeout = null;
this._statusErrorLogs = 0;
options = options || {};
this._statusEventDelay = options.statusEventDelay || 300000;
this._errorCallback = options.errorCallback || utils.noop;
}
/**
* Initializes the insights client in the background by sending the startup event and scheduling status events at
* regular intervals.
* @returns {undefined}
*/
init() {
this._enabled = this._client.options.monitorReporting.enabled && this._dseSupportsInsights();
if (!this._enabled) {
return;
}
promiseUtils.toBackground(this._init());
}
async _init() {
try {
await this._sendStartupEvent();
if (this._closed) {
// The client was shutdown
return;
}
// Send the status event the first time with a delay containing some random portion
// Initial delay should be statusEventDelay - (0 to 10%)
const firstDelay = Math.floor(this._statusEventDelay - 0.1 * this._statusEventDelay * Math.random());
// Schedule the first timer
this._firstTimeout = setTimeout(() => {
// Send the first status event, the promise will never be rejected
this._sendStatusEvent();
// The following status events are sent at regular intervals
this._recurrentTimeout = setInterval(() => this._sendStatusEvent(), this._statusEventDelay);
}, firstDelay);
} catch (err) {
if (this._closed) {
// Sending failed because the Client was shutdown
return;
}
// We shouldn't try to recover
this._client.log('verbose', `Insights startup message could not be sent (${err})`, err);
this._errorCallback(err);
}
}
/**
* Sends the startup event.
* @returns {Promise}
* @private
*/
async _sendStartupEvent() {
const message = await this._getStartupMessage();
const request = new requests.QueryRequest(rpc, [message], ExecutionOptions.empty());
await this._client.controlConnection.query(request, false);
}
/**
* Sends the status event.
* @returns {Promise} A promise that is never rejected.
* @private
*/
async _sendStatusEvent() {
const request = new requests.QueryRequest(rpc, [ this._getStatusEvent() ], ExecutionOptions.empty());
try {
await this._client.controlConnection.query(request, false);
} catch (err) {
if (this._closed) {
// Sending failed because the Client was shutdown
return;
}
if (this._statusErrorLogs < maxStatusErrorLogs) {
this._client.log('warning', `Insights status message could not be sent (${err})`, err);
this._statusErrorLogs++;
}
this._errorCallback(err);
}
}
/**
* Validates the minimum server version for all nodes in the cluster.
* @private
*/
_dseSupportsInsights() {
if (this._client.hosts.length === 0) {
return false;
}
return this._client.hosts.values().reduce((acc, host) => {
if (!acc) {
return acc;
}
const versionArr = host.getDseVersion();
if (versionArr.length === 0) {
return false;
}
const version = new VersionNumber(...versionArr);
return version.compare(minDse6Version) >= 0 ||
(version.compare(dse600Version) < 0 && version.compare(minDse51Version) >= 0);
}, true);
}
/**
* @returns {Promise<String>} Returns a json string with the startup message.
* @private
*/
async _getStartupMessage() {
const cc = this._client.controlConnection;
const options = this._client.options;
const appInfo = await this._getAppInfo(options);
const message = {
metadata: {
name: 'driver.startup',
insightMappingId: 'v1',
insightType: 'EVENT',
timestamp: Date.now(),
tags: { language: 'nodejs' }
},
data: {
driverName: packageInfo.description,
driverVersion: packageInfo.version,
clientId: options.id,
sessionId: this._sessionId,
applicationName: appInfo.applicationName,
applicationVersion: appInfo.applicationVersion,
applicationNameWasGenerated: appInfo.applicationNameWasGenerated,
contactPoints: mapToObject(cc.getResolvedContactPoints()),
dataCenters: this._getDataCenters(),
initialControlConnection: cc.host ? cc.host.address : undefined,
protocolVersion: cc.protocolVersion,
localAddress: cc.getLocalAddress(),
hostName: os.hostname(),
executionProfiles: getExecutionProfiles(this._client),
poolSizeByHostDistance: {
local: options.pooling.coreConnectionsPerHost[types.distance.local],
remote: options.pooling.coreConnectionsPerHost[types.distance.remote]
},
heartbeatInterval: options.pooling.heartBeatInterval,
compression: 'NONE',
reconnectionPolicy: getPolicyInfo(options.policies.reconnection),
ssl: {
enabled: !!options.sslOptions,
certValidation: options.sslOptions ? !!options.sslOptions.rejectUnauthorized : undefined
},
authProvider: {
type: !(options.authProvider instanceof NoAuthProvider) ? getConstructor(options.authProvider) : undefined,
},
otherOptions: {
coalescingThreshold: options.socketOptions.coalescingThreshold,
},
platformInfo: {
os: {
name: os.platform(),
version: os.release(),
arch: os.arch()
},
cpus: {
length: os.cpus().length,
model: os.cpus()[0].model
},
runtime: {
node: process.versions['node'],
v8: process.versions['v8'],
uv: process.versions['uv'],
openssl: process.versions['openssl'],
kerberos: kerberosModule ? kerberosModule.version : undefined
}
},
configAntiPatterns: this._getConfigAntiPatterns(),
periodicStatusInterval: Math.floor(this._statusEventDelay / 1000)
}
};
return JSON.stringify(message);
}
_getConfigAntiPatterns() {
const options = this._client.options;
const result = {};
if (options.sslOptions && !options.sslOptions.rejectUnauthorized) {
result.sslWithoutCertValidation =
'Client-to-node encryption is enabled but server certificate validation is disabled';
}
return result;
}
/**
* Gets an array of data centers the driver connects to.
* Whether the driver connects to a certain host is determined by the host distance (local and remote hosts)
* and the pooling options (whether connection length for remote hosts is greater than 0).
* @returns {Array}
* @private
*/
_getDataCenters() {
const remoteConnectionsLength = this._client.options.pooling.coreConnectionsPerHost[types.distance.remote];
const dataCenters = new Set();
this._client.hosts.values().forEach(h => {
const distance = this._client.profileManager.getDistance(h);
if (distance === types.distance.local || (distance === types.distance.remote && remoteConnectionsLength > 0)) {
dataCenters.add(h.datacenter);
}
});
return Array.from(dataCenters);
}
/**
* Tries to obtain the application name and version from
* @param {DseClientOptions} options
* @returns {Promise}
* @private
*/
async _getAppInfo(options) {
if (typeof options.applicationName === 'string') {
return Promise.resolve({
applicationName: options.applicationName,
applicationVersion: options.applicationVersion,
applicationNameWasGenerated: false
});
}
let readPromise = Promise.resolve();
if (require.main && require.main.filename) {
const packageInfoPath = path.dirname(require.main.filename);
readPromise = this._readPackageInfoFile(packageInfoPath);
}
const text = await readPromise;
let applicationName = 'Default Node.js Application';
let applicationVersion;
if (text) {
try {
const packageInfo = JSON.parse(text);
if (packageInfo.name) {
applicationName = packageInfo.name;
applicationVersion = packageInfo.version;
}
}
catch (err) {
// The package.json file could not be parsed
// Use the default name
}
}
return {
applicationName,
applicationVersion,
applicationNameWasGenerated: true
};
}
/**
* @private
* @returns {Promise<string>} A Promise that will never be rejected
*/
_readPackageInfoFile(packageInfoPath) {
return new Promise(resolve => {
fs.readFile(path.join(packageInfoPath, 'package.json'), 'utf8', (err, data) => {
// Swallow error
resolve(data);
});
});
}
/**
* @returns {String} Returns a json string with the startup message.
* @private
*/
_getStatusEvent() {
const cc = this._client.controlConnection;
const options = this._client.options;
const state = this._client.getState();
const connectedNodes = {};
state.getConnectedHosts().forEach(h => {
connectedNodes[h.address] = {
connections: state.getOpenConnections(h),
inFlightQueries: state.getInFlightQueries(h)
};
});
const message = {
metadata: {
name: 'driver.status',
insightMappingId: 'v1',
insightType: 'EVENT',
timestamp: Date.now(),
tags: { language: 'nodejs' }
},
data: {
clientId: options.id,
sessionId: this._sessionId,
controlConnection: cc.host ? cc.host.address : undefined,
connectedNodes
}
};
return JSON.stringify(message);
}
/**
* Cleans any timer used internally and sets the client as closed.
*/
shutdown() {
if (!this._enabled) {
return;
}
this._closed = true;
if (this._firstTimeout !== null) {
clearTimeout(this._firstTimeout);
}
if (this._recurrentTimeout !== null) {
clearInterval(this._recurrentTimeout);
}
}
}
module.exports = InsightsClient;
function mapToObject(map) {
const result = {};
map.forEach((value, key) => result[key] = value);
return result;
}
function getPolicyInfo(policy) {
if (!policy) {
return undefined;
}
const options = policy.getOptions && policy.getOptions();
return {
type: policy.constructor.name,
options: (options instanceof Map) ? mapToObject(options) : utils.emptyObject
};
}
function getConsistencyString(c) {
if (typeof c !== 'number') {
return undefined;
}
return types.consistencyToString[c];
}
function getConstructor(instance) {
return instance ? instance.constructor.name : undefined;
}
function getExecutionProfiles(client) {
const executionProfiles = {};
const defaultProfile = client.profileManager.getDefault();
setExecutionProfileProperties(client, executionProfiles, defaultProfile, defaultProfile);
client.profileManager.getAll()
.filter(p => p !== defaultProfile)
.forEach(profile => setExecutionProfileProperties(client, executionProfiles, profile, defaultProfile));
return executionProfiles;
}
function setExecutionProfileProperties(client, parent, profile, defaultProfile) {
const output = parent[profile.name] = {};
setExecutionProfileItem(output, profile, defaultProfile, 'readTimeout');
setExecutionProfileItem(output, profile, defaultProfile, 'loadBalancing', getPolicyInfo);
setExecutionProfileItem(output, profile, defaultProfile, 'retry', getPolicyInfo);
setExecutionProfileItem(output, profile, defaultProfile, 'consistency', getConsistencyString);
setExecutionProfileItem(output, profile, defaultProfile, 'serialConsistency', getConsistencyString);
if (profile === defaultProfile) {
// Speculative execution policy is included in the profiles as some drivers support
// different spec exec policy per profile, in this case is fixed for all profiles
output.speculativeExecution = getPolicyInfo(client.options.policies.speculativeExecution);
}
if (profile.graphOptions) {
output.graphOptions = {};
const defaultGraphOptions = defaultProfile.graphOptions || utils.emptyObject;
setExecutionProfileItem(output.graphOptions, profile.graphOptions, defaultGraphOptions, 'language');
setExecutionProfileItem(output.graphOptions, profile.graphOptions, defaultGraphOptions, 'name');
setExecutionProfileItem(output.graphOptions, profile.graphOptions, defaultGraphOptions, 'readConsistency',
getConsistencyString);
setExecutionProfileItem(output.graphOptions, profile.graphOptions, defaultGraphOptions, 'source');
setExecutionProfileItem(output.graphOptions, profile.graphOptions, defaultGraphOptions, 'writeConsistency',
getConsistencyString);
if (Object.keys(output.graphOptions).length === 0) {
// Properties that are undefined will not be included in the JSON
output.graphOptions = undefined;
}
}
}
function setExecutionProfileItem(output, profile, defaultProfile, prop, valueGetter) {
const value = profile[prop];
valueGetter = valueGetter || (x => x);
if ((profile === defaultProfile && value !== undefined) || value !== defaultProfile[prop]) {
output[prop] = valueGetter(value);
}
}