|
| 1 | +const fs = require('fs') |
| 2 | +const path = require('path') |
| 3 | +const childProcess = require('child_process') |
| 4 | +const proxyquire = require('proxyquire') |
| 5 | + |
| 6 | +const versionLists = {} |
| 7 | +const names = [] |
| 8 | + |
| 9 | +const filter = process.env.hasOwnProperty('PLUGINS') && process.env.PLUGINS.split('|') |
| 10 | + |
| 11 | +fs.readdirSync(path.join(__dirname, '../../packages/datadog-instrumentations/src')) |
| 12 | + .filter(file => file.endsWith('js')) |
| 13 | + .forEach(file => { |
| 14 | + file = file.replace('.js', '') |
| 15 | + |
| 16 | + if (!filter || filter.includes(file)) { |
| 17 | + names.push(file) |
| 18 | + } |
| 19 | + }) |
| 20 | + |
| 21 | +async function getVersionList (name) { |
| 22 | + if (versionLists[name]) { |
| 23 | + return versionLists[name] |
| 24 | + } |
| 25 | + const list = await npmView(`${name} versions`) |
| 26 | + versionLists[name] = list |
| 27 | + return list |
| 28 | +} |
| 29 | + |
| 30 | +function npmView (input) { |
| 31 | + return new Promise((resolve, reject) => { |
| 32 | + childProcess.exec(`npm view ${input} --json`, (err, stdout) => { |
| 33 | + if (err) { |
| 34 | + reject(err) |
| 35 | + return |
| 36 | + } |
| 37 | + resolve(JSON.parse(stdout.toString('utf8'))) |
| 38 | + }) |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +function loadInstFile (file, instrumentations) { |
| 43 | + const instrument = { |
| 44 | + addHook (instrumentation) { |
| 45 | + instrumentations.push(instrumentation) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + const instPath = path.join(__dirname, `../../packages/datadog-instrumentations/src/${file}`) |
| 50 | + |
| 51 | + proxyquire.noPreserveCache()(instPath, { |
| 52 | + './helpers/instrument': instrument, |
| 53 | + '../helpers/instrument': instrument |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +function getInternals () { |
| 58 | + return names.map(key => { |
| 59 | + const instrumentations = [] |
| 60 | + const name = key |
| 61 | + |
| 62 | + try { |
| 63 | + loadInstFile(`${name}/server.js`, instrumentations) |
| 64 | + loadInstFile(`${name}/client.js`, instrumentations) |
| 65 | + } catch (e) { |
| 66 | + loadInstFile(`${name}.js`, instrumentations) |
| 67 | + } |
| 68 | + |
| 69 | + return instrumentations |
| 70 | + }).reduce((prev, next) => prev.concat(next), []) |
| 71 | +} |
| 72 | + |
| 73 | +module.exports = { |
| 74 | + getVersionList, |
| 75 | + npmView, |
| 76 | + loadInstFile, |
| 77 | + getInternals |
| 78 | +} |
0 commit comments