Skip to content

Commit 94ee7c6

Browse files
committed
Update register and install_plugin_modules
Taken from Bryan
1 parent 1a0f085 commit 94ee7c6

File tree

2 files changed

+31
-56
lines changed

2 files changed

+31
-56
lines changed

packages/datadog-instrumentations/src/helpers/register.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616

1717
const hooks = require('./hooks')
1818
const instrumentations = require('./instrumentations')
19+
const latests = require('./latests.json')
1920
const names = Object.keys(hooks)
2021
const pathSepExpr = new RegExp(`\\${path.sep}`, 'g')
2122
const disabledInstrumentations = new Set(
@@ -111,7 +112,7 @@ for (const packageName of names) {
111112
namesAndSuccesses[`${name}@${version}`] = false
112113
}
113114

114-
if (matchVersion(version, versions)) {
115+
if (matchVersion(version, versions) && matchesLatestSupported(name, version)) {
115116
// Check if the hook already has a set moduleExport
116117
if (hook[HOOK_SYMBOL].has(moduleExports)) {
117118
namesAndSuccesses[`${name}@${version}`] = true
@@ -159,6 +160,17 @@ function matchVersion (version, ranges) {
159160
return !version || (ranges && ranges.some(range => semver.satisfies(semver.coerce(version), range)))
160161
}
161162

163+
function matchesLatestSupported (name, version) {
164+
if (latests.pinned.includes(name)) {
165+
// These ones are deliberately pinned to a specific version. That
166+
// means we can skip this check, since it will already have been checked
167+
// to be lower than latest.
168+
return true
169+
}
170+
const latest = latests.latests[name]
171+
return matchVersion(version, ['<=' + latest])
172+
}
173+
162174
function getVersion (moduleBaseDir) {
163175
if (moduleBaseDir) {
164176
return requirePackageJson(moduleBaseDir, module).version

scripts/install_plugin_modules.js

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ 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')
10-
const childProcess = require('child_process')
119
const externals = require('../packages/dd-trace/test/plugins/externals')
10+
const {
11+
getVersionList,
12+
getInternals,
13+
npmView
14+
} = require('./helpers/versioning')
15+
const latests = require('../packages/datadog-instrumentations/src/helpers/latests.json')
1216

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

1519
// Can remove aerospike after removing support for aerospike < 5.2.0 (for Node.js 22, v5.12.1 is required)
1620
// Can remove couchbase after removing support for couchbase <= 3.2.0
1721
const excludeList = os.arch() === 'arm64' ? ['aerospike', 'couchbase', 'grpc', 'oracledb'] : []
1822
const workspaces = new Set()
19-
const versionLists = {}
2023
const deps = {}
2124
const filter = process.env.hasOwnProperty('PLUGINS') && process.env.PLUGINS.split('|')
2225

@@ -46,21 +49,7 @@ async function run () {
4649
}
4750

4851
async function assertVersions () {
49-
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-
})
63-
.reduce((prev, next) => prev.concat(next), [])
52+
const internals = getInternals()
6453

6554
for (const inst of internals) {
6655
await assertInstrumentation(inst, false)
@@ -145,7 +134,17 @@ async function assertPackage (name, version, dependency, external) {
145134
}
146135

147136
async function addDependencies (dependencies, name, versionRange) {
148-
const versionList = await getVersionList(name)
137+
let versionList = await getVersionList(name)
138+
if (!latests.pinned.includes(name)) {
139+
const maxVersion = latests.latests[name]
140+
versionList = versionList.map(version => {
141+
if (version.startsWith('>=') && !version.includes('<')) {
142+
return version + ' <=' + maxVersion
143+
} else {
144+
return version
145+
}
146+
})
147+
}
149148
const version = semver.maxSatisfying(versionList, versionRange)
150149
const pkgJson = await npmView(`${name}@${version}`)
151150
for (const dep of deps[name]) {
@@ -158,27 +157,6 @@ async function addDependencies (dependencies, name, versionRange) {
158157
}
159158
}
160159

161-
async function getVersionList (name) {
162-
if (versionLists[name]) {
163-
return versionLists[name]
164-
}
165-
const list = await npmView(`${name} versions`)
166-
versionLists[name] = list
167-
return list
168-
}
169-
170-
function npmView (input) {
171-
return new Promise((resolve, reject) => {
172-
childProcess.exec(`npm view ${input} --json`, (err, stdout) => {
173-
if (err) {
174-
reject(err)
175-
return
176-
}
177-
resolve(JSON.parse(stdout.toString('utf8')))
178-
})
179-
})
180-
}
181-
182160
function assertIndex (name, version) {
183161
const index = `'use strict'
184162
@@ -234,18 +212,3 @@ function sha1 (str) {
234212
shasum.update(str)
235213
return shasum.digest('hex')
236214
}
237-
238-
function loadInstFile (file, instrumentations) {
239-
const instrument = {
240-
addHook (instrumentation) {
241-
instrumentations.push(instrumentation)
242-
}
243-
}
244-
245-
const instPath = path.join(__dirname, `../packages/datadog-instrumentations/src/${file}`)
246-
247-
proxyquire.noPreserveCache()(instPath, {
248-
'./helpers/instrument': instrument,
249-
'../helpers/instrument': instrument
250-
})
251-
}

0 commit comments

Comments
 (0)