Skip to content

Commit

Permalink
consolidate instances of loadInst, so code isn't repeated
Browse files Browse the repository at this point in the history
  • Loading branch information
bengl committed Dec 14, 2024
1 parent baf22d9 commit 432f4a7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 103 deletions.
62 changes: 62 additions & 0 deletions packages/dd-trace/test/setup/helpers/load-inst.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict'

const fs = require('fs')
const path = require('path')
const proxyquire = require('proxyquire')

function loadInstFile (file, instrumentations) {
const instrument = {
addHook (instrumentation) {
instrumentations.push(instrumentation)
}
}

const instPath = path.join(__dirname, `../../../../datadog-instrumentations/src/${file}`)

proxyquire.noPreserveCache()(instPath, {
'./helpers/instrument': instrument,
'../helpers/instrument': instrument
})
}

function loadOneInst (name) {
const instrumentations = []

try {
loadInstFile(`${name}/server.js`, instrumentations)
loadInstFile(`${name}/client.js`, instrumentations)
} catch (e) {
try {
loadInstFile(`${name}/main.js`, instrumentations)
} catch (e) {
loadInstFile(`${name}.js`, instrumentations)
}
}

return instrumentations
}

function getAllInstrumentations () {
const names = fs.readdirSync(path.join(__dirname, '../../../../', 'datadog-instrumentations', 'src'))
.filter(file => file.endsWith('.js'))
.map(file => file.slice(0, -3))

const instrumentations = names.reduce((acc, key) => {
const name = key
let instrumentations = loadOneInst(name)

instrumentations = instrumentations.filter(i => i.versions)
if (instrumentations.length) {
acc[key] = instrumentations
}

return acc
}, {})

return instrumentations
}

module.exports = {
getInstrumentation: loadOneInst,
getAllInstrumentations
}
35 changes: 2 additions & 33 deletions packages/dd-trace/test/setup/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const agent = require('../plugins/agent')
const Nomenclature = require('../../src/service-naming')
const { storage } = require('../../../datadog-core')
const { schemaDefinitions } = require('../../src/service-naming/schemas')
const { getInstrumentation } = require('./helpers/load-inst')

global.withVersions = withVersions
global.withExports = withExports
Expand All @@ -19,38 +20,6 @@ global.withPeerService = withPeerService

const testedPlugins = agent.testedPlugins

function loadInst (plugin) {
const instrumentations = []

try {
loadInstFile(`${plugin}/server.js`, instrumentations)
loadInstFile(`${plugin}/client.js`, instrumentations)
} catch (e) {
try {
loadInstFile(`${plugin}/main.js`, instrumentations)
} catch (e) {
loadInstFile(`${plugin}.js`, instrumentations)
}
}

return instrumentations
}

function loadInstFile (file, instrumentations) {
const instrument = {
addHook (instrumentation) {
instrumentations.push(instrumentation)
}
}

const instPath = path.join(__dirname, `../../../datadog-instrumentations/src/${file}`)

proxyquire.noPreserveCache()(instPath, {
'./helpers/instrument': instrument,
'../helpers/instrument': instrument
})
}

function withNamingSchema (
spanProducerFn,
expected,
Expand Down Expand Up @@ -174,7 +143,7 @@ function withPeerService (tracer, pluginName, spanGenerationFn, service, service
}

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

modules = [].concat(modules)
Expand Down
37 changes: 5 additions & 32 deletions scripts/install_plugin_modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const os = require('os')
const path = require('path')
const crypto = require('crypto')
const semver = require('semver')
const proxyquire = require('proxyquire')
const exec = require('./helpers/exec')
const childProcess = require('child_process')
const externals = require('../packages/dd-trace/test/plugins/externals')
const { getInstrumentation } = require('../packages/dd-trace/test/setup/helpers/load-inst')

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

Expand Down Expand Up @@ -47,19 +47,7 @@ async function run () {

async function assertVersions () {
const internals = names
.map(key => {
const instrumentations = []
const name = key

try {
loadInstFile(`${name}/server.js`, instrumentations)
loadInstFile(`${name}/client.js`, instrumentations)
} catch (e) {
loadInstFile(`${name}.js`, instrumentations)
}

return instrumentations
})
.map(getInstrumentation)
.reduce((prev, next) => prev.concat(next), [])

for (const inst of internals) {
Expand Down Expand Up @@ -117,10 +105,10 @@ function assertFolder (name, version) {
}
}

async function assertPackage (name, version, dependency, external) {
const dependencies = { [name]: dependency }
async function assertPackage (name, version, dependencyVersionRange, external) {
const dependencies = { [name]: dependencyVersionRange }
if (deps[name]) {
await addDependencies(dependencies, name, dependency)
await addDependencies(dependencies, name, dependencyVersionRange)
}
const pkg = {
name: [name, sha1(name).substr(0, 8), sha1(version)].filter(val => val).join('-'),
Expand Down Expand Up @@ -240,18 +228,3 @@ function sha1 (str) {
shasum.update(str)
return shasum.digest('hex')
}

function loadInstFile (file, instrumentations) {
const instrument = {
addHook (instrumentation) {
instrumentations.push(instrumentation)
}
}

const instPath = path.join(__dirname, `../packages/datadog-instrumentations/src/${file}`)

proxyquire.noPreserveCache()(instPath, {
'./helpers/instrument': instrument,
'../helpers/instrument': instrument
})
}
41 changes: 3 additions & 38 deletions scripts/verify-ci-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,19 @@
const fs = require('fs')
const path = require('path')
const util = require('util')
const proxyquire = require('proxyquire')
const yaml = require('yaml')
const semver = require('semver')
const { execSync } = require('child_process')
const Module = require('module')
const { getAllInstrumentations } = require('../packages/dd-trace/test/setup/helpers/load-inst')

if (!Module.isBuiltin) {
Module.isBuiltin = mod => Module.builtinModules.includes(mod)
}

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

const names = fs.readdirSync(path.join(__dirname, '..', 'packages', 'datadog-instrumentations', 'src'))
.filter(file => file.endsWith('.js'))
.map(file => file.slice(0, -3))

const instrumentations = names.reduce((acc, key) => {
let instrumentations = []
const name = key

try {
loadInstFile(`${name}/server.js`, instrumentations)
loadInstFile(`${name}/client.js`, instrumentations)
} catch (e) {
loadInstFile(`${name}.js`, instrumentations)
}

instrumentations = instrumentations.filter(i => i.versions)
if (instrumentations.length) {
acc[key] = instrumentations
}

return acc
}, {})
const instrumentations = getAllInstrumentations()

const versions = {}

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

function loadInstFile (file, instrumentations) {
const instrument = {
addHook (instrumentation) {
instrumentations.push(instrumentation)
}
}

const instPath = path.join(__dirname, `../packages/datadog-instrumentations/src/${file}`)

proxyquire.noPreserveCache()(instPath, {
'./helpers/instrument': instrument,
'../helpers/instrument': instrument
})
}

function getRangesFromYaml (job) {
// eslint-disable-next-line no-template-curly-in-string
if (job.env && job.env.PACKAGE_VERSION_RANGE && job.env.PACKAGE_VERSION_RANGE !== '${{ matrix.range }}') {
Expand Down

0 comments on commit 432f4a7

Please sign in to comment.