-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathdependencies.js
More file actions
279 lines (244 loc) · 10.1 KB
/
Copy pathdependencies.js
File metadata and controls
279 lines (244 loc) · 10.1 KB
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
/*
* (c) Copyright IBM Corp. 2021
* (c) Copyright Instana Inc. and contributors 2015
*/
'use strict';
const path = require('path');
const { util, uninstrumentedFs: fs } = require('@instana/core');
const CountDownLatch = require('./util/CountDownLatch');
const { DependencyDistanceCalculator, MAX_DEPTH } = require('./util/DependencyDistanceCalculator');
/** @type {import('@instana/core/src/core').GenericLogger} */
let logger;
/**
* @param {import('@instana/core/src/config').InstanaConfig} config
*/
exports.init = function init(config) {
logger = config.logger;
};
/** @type {number} */
exports.MAX_DEPENDENCIES = 750;
/** @type {string} */
exports.payloadPrefix = 'dependencies';
const MAX_DEPTH_NODE_MODULES = 2;
/** @type {Object.<string, string>} */
const preliminaryPayload = {};
/** @type {Object.<string, string>} */
// @ts-ignore: Cannot redeclare exported variable 'currentPayload'
exports.currentPayload = {};
// @ts-ignore
exports.activate = function activate(config, packageJsonObj) {
const started = Date.now();
if (!packageJsonObj || !packageJsonObj.file) {
util.applicationUnderMonitoring.findNodeModulesFolder((errNodeModules, nodeModulesFolder) => {
if (errNodeModules) {
return logger.warn(
`Failed to determine node_modules folder. Reason: ${errNodeModules?.message}, ${errNodeModules?.stack}`
);
} else if (!nodeModulesFolder) {
return logger.warn(
'Neither the package.json file nor the node_modules folder could be found. Stopping dependency analysis.'
);
}
addAllDependencies(path.join(nodeModulesFolder), started, null);
});
return;
}
let dependencyDir;
if (util.applicationUnderMonitoring.isAppInstalledIntoNodeModules()) {
dependencyDir = path.join(path.dirname(packageJsonObj.path), '..', '..', 'node_modules');
} else {
dependencyDir = path.join(path.dirname(packageJsonObj.path), 'node_modules');
}
addAllDependencies(dependencyDir, started, packageJsonObj.path);
};
/**
* Finds all installed modules in the given dependencyDir (say, /path/to/app/node_modules) and saves the dependency with
* the associated version into preliminaryPayload.
*
* @param {string} dependencyDir
* @param {number} started
* @param {string} packageJsonPath
*/
function addAllDependencies(dependencyDir, started, packageJsonPath) {
addDependenciesFromDir(dependencyDir, 0, () => {
// TODO: This check happens AFTER we have already collected the dependencies.
// This is quiet useless for a large dependency tree, because we consume resources to collect
// all the dependencies (fs.stats, fs.readFile etc), but then discard most of them here.
// This is only critical for a very large number of defined dependencies in package.json (vertical).
// NOTE: There is an extra protection in the `addDependenciesFromDir` fn to
// limit the depth of traversing node_modules.
if (Object.keys(preliminaryPayload).length <= exports.MAX_DEPENDENCIES) {
// @ts-ignore: Cannot redeclare exported variable 'currentPayload'
exports.currentPayload = preliminaryPayload;
logger.debug(`Collection of dependencies took ${Date.now() - started} ms.`);
return;
}
if (packageJsonPath) {
new DependencyDistanceCalculator().calculateDistancesFrom(packageJsonPath, distancesFromRoot => {
logger.debug(`Collection of dependencies took ${Date.now() - started} ms.`);
limitAndSet(distancesFromRoot);
});
} else {
logger.debug(`Collection of dependencies took ${Date.now() - started} ms.`);
limitAndSet();
}
});
}
/**
* Finds all installed modules in dependencyDir (say, /path/to/app/node_modules) and saves the dependency with the
* associated version into preliminaryPayload.
*
* @param {string} dependencyDir
* @param {() => void} callback
*/
function addDependenciesFromDir(dependencyDir, currentDepth = 0, callback) {
if (currentDepth >= MAX_DEPTH_NODE_MODULES) {
return callback();
}
fs.readdir(dependencyDir, (readDirErr, dependencies) => {
if (readDirErr || !dependencies) {
logger.warn(`Cannot analyse dependencies due to ${readDirErr?.message}`);
callback();
return;
}
const filteredDependendencies = dependencies.filter(
(
dependency // exclude the .bin directory
) => dependency !== '.bin'
);
if (filteredDependendencies.length === 0) {
callback();
return;
}
// This latch fires once all dependencies of the current directory in the node_modules tree have been analysed.
const countDownLatch = new CountDownLatch(filteredDependendencies.length);
countDownLatch.once('done', () => {
callback();
});
filteredDependendencies.forEach(dependency => {
if (dependency.indexOf('@') === 0) {
// NOTE: We do not increase currentDepth because scoped packages are just a folder containing more packages.
addDependenciesFromDir(path.join(dependencyDir, dependency), currentDepth, () => {
countDownLatch.countDown();
});
} else {
const fullDirPath = path.join(dependencyDir, dependency);
// Only check directories. For example, yarn adds a .yarn-integrity file to /node_modules/ which we need to
// exclude, otherwise we get a confusing "Failed to identify version of .yarn-integrity dependency due to:
// ENOTDIR: not a directory, open '.../node_modules/.yarn-integrity/package.json'." in the logs.
fs.stat(fullDirPath, (statErr, stats) => {
if (statErr) {
countDownLatch.countDown();
logger.warn(`Cannot analyse dependency ${fullDirPath} due to ${statErr?.message}`);
return;
}
if (!stats.isDirectory()) {
countDownLatch.countDown();
return;
}
addDependency(dependency, fullDirPath, countDownLatch, currentDepth);
});
}
});
});
}
/**
* Parses the package.json file in the given directory and then adds the given dependency (with its version) to
* preliminaryPayload.
*
* @param {string} dependency
* @param {string} dependencyDirPath
* @param {import('./util/CountDownLatch')} countDownLatch
* @param {number} currentDepth
*/
function addDependency(dependency, dependencyDirPath, countDownLatch, currentDepth) {
const packageJsonPath = path.join(dependencyDirPath, 'package.json');
fs.readFile(packageJsonPath, { encoding: 'utf8' }, (err, contents) => {
if (err && err.code === 'ENOENT') {
// This directory does not contain a package json. This happens for example for node_modules/.cache etc.
// We can simply ignore this.
countDownLatch.countDown();
logger.debug(`No package.json at ${packageJsonPath}, ignoring this directory.`);
return;
} else if (err) {
countDownLatch.countDown();
logger.info(
`Failed to identify version of ${dependency} dependency due to: ${err?.message}. ` +
'This means that you will not be able to see details about this dependency within Instana.'
);
return;
}
try {
const parsedPackageJson = JSON.parse(contents);
if (!preliminaryPayload[parsedPackageJson.name]) {
preliminaryPayload[parsedPackageJson.name] = parsedPackageJson.version;
}
} catch (parseErr) {
// TODO: countDownLatch.countDown(); needs to be called here too?
// countDownLatch.countDown();
return logger.info(
`Failed to identify version of ${dependency} dependency due to: ${parseErr?.message}.
This means that you will not be able to see details about this dependency within Instana.`
);
}
// NOTE: The dependency metric collector does not respect if the node_modules are dev dependencies or production
// dependencies. It collects all dependencies that are installed in the node_modules folder.
const potentialNestedNodeModulesFolder = path.join(dependencyDirPath, 'node_modules');
fs.stat(potentialNestedNodeModulesFolder, (statErr, stats) => {
if (statErr || !stats.isDirectory()) {
countDownLatch.countDown();
return;
}
addDependenciesFromDir(potentialNestedNodeModulesFolder, currentDepth + 1, () => {
countDownLatch.countDown();
});
});
});
}
/**
* Limits the collected dependencies to exports.MAX_DEPENDENCIES entries and commits them to exports.currentPayload.
*
* @param {Object<string, any>} distances
*/
function limitAndSet(distances = {}) {
const keys = Object.keys(preliminaryPayload);
keys.sort(sortByDistance.bind(null, distances));
// After sorting, the most distant (and therefore, most uninteresting) packages are a the start of the array. For
// packages with the same distance, we sort in a reverse lexicographic order. That means, that if no distances are
// available at all, packages will be in reverse lexicographical order.
//
// At any rate, we start deleting collected depenencies from the payload at index 0, that is, we either remove the
// most distant ones or the ones that are at the end of the lexicographic order.
for (let i = 0; i < keys.length - exports.MAX_DEPENDENCIES; i++) {
delete preliminaryPayload[keys[i]];
}
// @ts-ignore: Cannot redeclare exported variable 'currentPayload'
exports.currentPayload = preliminaryPayload;
}
/**
* Compares the given dependencies by their distance.
*
* @param {Object<string, any>} distances
* @param {string} dependency1
* @param {string} dependency2
*/
function sortByDistance(distances, dependency1, dependency2) {
// To make troubleshooting easier, we always want to include the Instana dependencies, therefore they will be sorted
// to the end of the array.
const isInstana1 = dependency1.indexOf('instana') >= 0;
const isInstana2 = dependency2.indexOf('instana') >= 0;
if (isInstana1 && isInstana2) {
return dependency2.localeCompare(dependency1);
} else if (isInstana1) {
return 1;
} else if (isInstana2) {
return -1;
}
const d1 = distances[dependency1] || MAX_DEPTH + 1;
const d2 = distances[dependency2] || MAX_DEPTH + 1;
if (d1 === d2) {
// for the same distance, sort lexicographically
return dependency2.localeCompare(dependency1);
}
return d2 - d1;
}