Skip to content

Commit 432f4a7

Browse files
committed
consolidate instances of loadInst, so code isn't repeated
1 parent baf22d9 commit 432f4a7

File tree

4 files changed

+72
-103
lines changed

4 files changed

+72
-103
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict'
2+
3+
const fs = require('fs')
4+
const path = require('path')
5+
const proxyquire = require('proxyquire')
6+
7+
function loadInstFile (file, instrumentations) {
8+
const instrument = {
9+
addHook (instrumentation) {
10+
instrumentations.push(instrumentation)
11+
}
12+
}
13+
14+
const instPath = path.join(__dirname, `../../../../datadog-instrumentations/src/${file}`)
15+
16+
proxyquire.noPreserveCache()(instPath, {
17+
'./helpers/instrument': instrument,
18+
'../helpers/instrument': instrument
19+
})
20+
}
21+
22+
function loadOneInst (name) {
23+
const instrumentations = []
24+
25+
try {
26+
loadInstFile(`${name}/server.js`, instrumentations)
27+
loadInstFile(`${name}/client.js`, instrumentations)
28+
} catch (e) {
29+
try {
30+
loadInstFile(`${name}/main.js`, instrumentations)
31+
} catch (e) {
32+
loadInstFile(`${name}.js`, instrumentations)
33+
}
34+
}
35+
36+
return instrumentations
37+
}
38+
39+
function getAllInstrumentations () {
40+
const names = fs.readdirSync(path.join(__dirname, '../../../../', 'datadog-instrumentations', 'src'))
41+
.filter(file => file.endsWith('.js'))
42+
.map(file => file.slice(0, -3))
43+
44+
const instrumentations = names.reduce((acc, key) => {
45+
const name = key
46+
let instrumentations = loadOneInst(name)
47+
48+
instrumentations = instrumentations.filter(i => i.versions)
49+
if (instrumentations.length) {
50+
acc[key] = instrumentations
51+
}
52+
53+
return acc
54+
}, {})
55+
56+
return instrumentations
57+
}
58+
59+
module.exports = {
60+
getInstrumentation: loadOneInst,
61+
getAllInstrumentations
62+
}

packages/dd-trace/test/setup/mocha.js

+2-33
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const agent = require('../plugins/agent')
1111
const Nomenclature = require('../../src/service-naming')
1212
const { storage } = require('../../../datadog-core')
1313
const { schemaDefinitions } = require('../../src/service-naming/schemas')
14+
const { getInstrumentation } = require('./helpers/load-inst')
1415

1516
global.withVersions = withVersions
1617
global.withExports = withExports
@@ -19,38 +20,6 @@ global.withPeerService = withPeerService
1920

2021
const testedPlugins = agent.testedPlugins
2122

22-
function loadInst (plugin) {
23-
const instrumentations = []
24-
25-
try {
26-
loadInstFile(`${plugin}/server.js`, instrumentations)
27-
loadInstFile(`${plugin}/client.js`, instrumentations)
28-
} catch (e) {
29-
try {
30-
loadInstFile(`${plugin}/main.js`, instrumentations)
31-
} catch (e) {
32-
loadInstFile(`${plugin}.js`, instrumentations)
33-
}
34-
}
35-
36-
return instrumentations
37-
}
38-
39-
function loadInstFile (file, instrumentations) {
40-
const instrument = {
41-
addHook (instrumentation) {
42-
instrumentations.push(instrumentation)
43-
}
44-
}
45-
46-
const instPath = path.join(__dirname, `../../../datadog-instrumentations/src/${file}`)
47-
48-
proxyquire.noPreserveCache()(instPath, {
49-
'./helpers/instrument': instrument,
50-
'../helpers/instrument': instrument
51-
})
52-
}
53-
5423
function withNamingSchema (
5524
spanProducerFn,
5625
expected,
@@ -174,7 +143,7 @@ function withPeerService (tracer, pluginName, spanGenerationFn, service, service
174143
}
175144

176145
function withVersions (plugin, modules, range, cb) {
177-
const instrumentations = typeof plugin === 'string' ? loadInst(plugin) : [].concat(plugin)
146+
const instrumentations = typeof plugin === 'string' ? getInstrumentation(plugin) : [].concat(plugin)
178147
const names = instrumentations.map(instrumentation => instrumentation.name)
179148

180149
modules = [].concat(modules)

scripts/install_plugin_modules.js

+5-32
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ const os = require('os')
55
const path = require('path')
66
const crypto = require('crypto')
77
const semver = require('semver')
8-
const proxyquire = require('proxyquire')
98
const exec = require('./helpers/exec')
109
const childProcess = require('child_process')
1110
const externals = require('../packages/dd-trace/test/plugins/externals')
11+
const { getInstrumentation } = require('../packages/dd-trace/test/setup/helpers/load-inst')
1212

1313
const requirePackageJsonPath = require.resolve('../packages/dd-trace/src/require-package-json')
1414

@@ -47,19 +47,7 @@ async function run () {
4747

4848
async function assertVersions () {
4949
const internals = names
50-
.map(key => {
51-
const instrumentations = []
52-
const name = key
53-
54-
try {
55-
loadInstFile(`${name}/server.js`, instrumentations)
56-
loadInstFile(`${name}/client.js`, instrumentations)
57-
} catch (e) {
58-
loadInstFile(`${name}.js`, instrumentations)
59-
}
60-
61-
return instrumentations
62-
})
50+
.map(getInstrumentation)
6351
.reduce((prev, next) => prev.concat(next), [])
6452

6553
for (const inst of internals) {
@@ -117,10 +105,10 @@ function assertFolder (name, version) {
117105
}
118106
}
119107

120-
async function assertPackage (name, version, dependency, external) {
121-
const dependencies = { [name]: dependency }
108+
async function assertPackage (name, version, dependencyVersionRange, external) {
109+
const dependencies = { [name]: dependencyVersionRange }
122110
if (deps[name]) {
123-
await addDependencies(dependencies, name, dependency)
111+
await addDependencies(dependencies, name, dependencyVersionRange)
124112
}
125113
const pkg = {
126114
name: [name, sha1(name).substr(0, 8), sha1(version)].filter(val => val).join('-'),
@@ -240,18 +228,3 @@ function sha1 (str) {
240228
shasum.update(str)
241229
return shasum.digest('hex')
242230
}
243-
244-
function loadInstFile (file, instrumentations) {
245-
const instrument = {
246-
addHook (instrumentation) {
247-
instrumentations.push(instrumentation)
248-
}
249-
}
250-
251-
const instPath = path.join(__dirname, `../packages/datadog-instrumentations/src/${file}`)
252-
253-
proxyquire.noPreserveCache()(instPath, {
254-
'./helpers/instrument': instrument,
255-
'../helpers/instrument': instrument
256-
})
257-
}

scripts/verify-ci-config.js

+3-38
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,19 @@
44
const fs = require('fs')
55
const path = require('path')
66
const util = require('util')
7-
const proxyquire = require('proxyquire')
87
const yaml = require('yaml')
98
const semver = require('semver')
109
const { execSync } = require('child_process')
1110
const Module = require('module')
11+
const { getAllInstrumentations } = require('../packages/dd-trace/test/setup/helpers/load-inst')
12+
1213
if (!Module.isBuiltin) {
1314
Module.isBuiltin = mod => Module.builtinModules.includes(mod)
1415
}
1516

1617
const nodeMajor = Number(process.versions.node.split('.')[0])
1718

18-
const names = fs.readdirSync(path.join(__dirname, '..', 'packages', 'datadog-instrumentations', 'src'))
19-
.filter(file => file.endsWith('.js'))
20-
.map(file => file.slice(0, -3))
21-
22-
const instrumentations = names.reduce((acc, key) => {
23-
let instrumentations = []
24-
const name = key
25-
26-
try {
27-
loadInstFile(`${name}/server.js`, instrumentations)
28-
loadInstFile(`${name}/client.js`, instrumentations)
29-
} catch (e) {
30-
loadInstFile(`${name}.js`, instrumentations)
31-
}
32-
33-
instrumentations = instrumentations.filter(i => i.versions)
34-
if (instrumentations.length) {
35-
acc[key] = instrumentations
36-
}
37-
38-
return acc
39-
}, {})
19+
const instrumentations = getAllInstrumentations()
4020

4121
const versions = {}
4222

@@ -84,21 +64,6 @@ Note that versions may be dependent on Node.js version. This is Node.js v${color
8464
}
8565
}
8666

87-
function loadInstFile (file, instrumentations) {
88-
const instrument = {
89-
addHook (instrumentation) {
90-
instrumentations.push(instrumentation)
91-
}
92-
}
93-
94-
const instPath = path.join(__dirname, `../packages/datadog-instrumentations/src/${file}`)
95-
96-
proxyquire.noPreserveCache()(instPath, {
97-
'./helpers/instrument': instrument,
98-
'../helpers/instrument': instrument
99-
})
100-
}
101-
10267
function getRangesFromYaml (job) {
10368
// eslint-disable-next-line no-template-curly-in-string
10469
if (job.env && job.env.PACKAGE_VERSION_RANGE && job.env.PACKAGE_VERSION_RANGE !== '${{ matrix.range }}') {

0 commit comments

Comments
 (0)