Skip to content

Commit e950783

Browse files
committed
cleanup
1 parent 215a344 commit e950783

File tree

8 files changed

+39
-96
lines changed

8 files changed

+39
-96
lines changed

integration-tests/esbuild/app.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
require('dd-trace').init();
3+
require('dd-trace').init()
44
const assert = require('assert')
55
const express = require('express')
66
const redis = require('redis')
@@ -13,17 +13,17 @@ assert.equal(redis.Graph.name, 'Graph')
1313
assert.equal(pg.types.builtins.BOOL, 16)
1414
assert.equal(express.static.mime.types.ogg, 'audio/ogg')
1515

16-
console.log('REDIS INJECTED?', redis.__DATADOG_VERSION);
17-
console.log('PG INJECTED?', pg.__DATADOG_VERSION);
18-
console.log('EXPRESS INJECTED?', express.__DATADOG_VERSION);
16+
// console.log('REDIS INJECTED?', redis.__DATADOG_VERSION)
17+
// console.log('PG INJECTED?', pg.__DATADOG_VERSION)
18+
// console.log('EXPRESS INJECTED?', express.__DATADOG_VERSION)
1919

2020
const conn = {
2121
user: 'postgres',
2222
host: 'localhost',
2323
database: 'postgres',
2424
password: 'hunter2',
2525
port: 5433,
26-
};
26+
}
2727

2828
console.log('pg connect')
2929
const client = new pg.Client(conn)
@@ -42,5 +42,5 @@ app.get('/', async (req, res) => {
4242
})
4343

4444
app.listen(PORT, () => {
45-
console.log(`Example app listening on port ${PORT}`)
45+
console.log(`Example app listening on port ${PORT}`)
4646
})

integration-tests/esbuild/app2.js

-28
This file was deleted.

integration-tests/esbuild/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
2-
"name": "esbuild-demo",
2+
"name": "esbuild-dd-trace-demo",
3+
"private": true,
34
"version": "1.0.0",
45
"description": "basic example app bundling dd-trace via esbuild",
56
"main": "app.js",
67
"scripts": {
7-
"build": "node ./build.js",
8+
"build": "DD_TRACE_DEBUG=true node ./build.js",
89
"built": "DD_TRACE_DEBUG=true node ./out.js",
910
"raw": "DD_TRACE_DEBUG=true node ./app.js",
1011
"link": "pushd ../.. && yarn link && popd && yarn link dd-trace",

packages/datadog-esbuild/index.js

+25-44
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,33 @@ const instrumented = Object.keys(require('../datadog-instrumentations/src/helper
44
const builtins = new Set(require('module').builtinModules)
55
const packages = new Set()
66

7-
// We don't want to handle any built-in packages via DCITM.
8-
// Those packages will still be handled via RITM.
9-
// Plust, attempting to instrument them will fail as they have no package.json file.
7+
const DEBUG = !!process.env.DD_TRACE_DEBUG
8+
9+
// We don't want to handle any built-in packages via DCITM
10+
// Those packages will still be handled via RITM
11+
// Attempting to instrument them would fail as they have no package.json file
1012
for (let pkg of instrumented) {
1113
if (builtins.has(pkg)) continue
1214
if (pkg.startsWith('node:')) continue
1315
packages.add(pkg)
1416
}
1517

16-
// console.log(packages)
17-
1818
const DC_CHANNEL = 'dd-trace:bundledModuleLoadStart'
1919

2020
module.exports.name = 'datadog-esbuild'
2121

2222
module.exports.setup = function(build) {
2323
build.onResolve({ filter: /.*/ }, args => {
2424
const package_name = args.path
25-
// first call:
26-
/* args = {
27-
path: 'pg',
28-
importer: '/Users/thomas.hunter/Projects/esbuild-demo/app.js',
29-
namespace: 'file',
30-
resolveDir: '/Users/thomas.hunter/Projects/esbuild-demo',
31-
kind: 'require-call',
32-
pluginData: undefined
33-
} */
34-
// second call:
35-
/* args = {
36-
path: 'pg',
37-
importer: 'pg',
38-
namespace: 'datadog',
39-
resolveDir: '',
40-
kind: 'require-call',
41-
pluginData: undefined
42-
} */
25+
4326
if (args.namespace === 'file' && packages.has(package_name)) {
27+
// The file namespace is used when requiring files from disk in userland
4428
const pathToPackageJson = require.resolve(`${package_name}/package.json`, { paths: [ args.resolveDir ] })
4529
const pkg = require(pathToPackageJson)
4630

47-
console.log('ONRESOLVE', package_name, pkg.version, pathToPackageJson)
48-
// console.log(args)
31+
if (DEBUG) {
32+
console.log(`resolve ${package_name}@${pkg.version}`)
33+
}
4934

5035
// https://esbuild.github.io/plugins/#on-resolve-arguments
5136
return {
@@ -56,9 +41,9 @@ module.exports.setup = function(build) {
5641
}
5742
}
5843
} else if (args.namespace === 'datadog') {
59-
console.log('ONRESOLVE DD', package_name)
44+
// The datadog namespace is used when requiring files that are injected during the onLoad stage
45+
// see note in onLoad
6046

61-
// @see note in onLoad
6247
if (package_name.startsWith('node:')) return
6348

6449
return {
@@ -69,29 +54,25 @@ module.exports.setup = function(build) {
6954
})
7055

7156
build.onLoad({ filter: /.*/, namespace: NAMESPACE }, args => {
72-
/* args = {
73-
path: 'pg',
74-
namespace: 'datadog',
75-
suffix: '',
76-
pluginData: { version: '8.8.0' }
77-
} */
78-
console.log('ONLOAD', args.path, args.pluginData.version)
57+
if (DEBUG) {
58+
console.log(`load ${args.path}@${args.pluginData.version}`)
59+
}
7960
// TODO: relying on prefixing internal packages with `node:` in this intermediary module for now.
8061
// If this causes an issue we'll need to update the logic for determining if a module is internal.
8162
// Note that JSON.stringify adds double quotes for us. For perf gain we can simply add in quotes when we know it's safe.
8263
let contents = `
83-
const dc = require('node:diagnostics_channel');
84-
const channel = dc.channel(${JSON.stringify(DC_CHANNEL + ':' + args.path)});
85-
const mod = require(${JSON.stringify(args.path)});
86-
const payload = {
64+
const dc = require('node:diagnostics_channel');
65+
const channel = dc.channel(${JSON.stringify(DC_CHANNEL + ':' + args.path)});
66+
const mod = require(${JSON.stringify(args.path)});
67+
const payload = {
8768
module: mod,
8869
path: ${JSON.stringify(args.path)},
8970
version: ${JSON.stringify(args.pluginData.version)}
90-
};
91-
if (!channel.hasSubscribers) console.error('NO SUB! ${JSON.stringify(DC_CHANNEL + ':' + args.path)}');
92-
channel.publish(payload); // subscriber may mutate payload
93-
module.exports = payload.module;
94-
module.exports.__DATADOG_VERSION = ${JSON.stringify(args.pluginData.version)}; // TODO: Unneccesary but cool
71+
};
72+
// if (!channel.hasSubscribers) console.error('missing subscriber! ${JSON.stringify(DC_CHANNEL + ':' + args.path)}');
73+
channel.publish(payload); // subscriber may mutate payload
74+
module.exports = payload.module;
75+
// module.exports.__DATADOG_VERSION = ${JSON.stringify(args.pluginData.version)};
9576
`
9677
// https://esbuild.github.io/plugins/#on-load-results
9778
return {
@@ -102,7 +83,7 @@ module.exports.setup = function(build) {
10283
}
10384

10485
/**
105-
* This is just a convenience to expose a list of known externals to the application.
86+
* This could be a convenience to expose a list of known externals to the application.
10687
* Devs are free to use this list, ignore it, or merge it with their application-specific list.
10788
* TODO: Sadly, esbuild does not allow unanticipated keys on the exported object.
10889
* Could do `{ plugin: { name, setup }, knownExternals }`

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

-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const Dcitm = require('../../../dd-trace/src/dcitm')
1313
* @param {Function} onrequire callback to be executed upon encountering module
1414
*/
1515
function Hook (modules, onrequire) {
16-
console.log('hook.js Hook()', modules)
1716
if (!(this instanceof Hook)) return new Hook(modules, onrequire)
1817

1918
this._patched = Object.create(null)
@@ -22,7 +21,6 @@ function Hook (modules, onrequire) {
2221
const parts = [moduleBaseDir, moduleName].filter(v => v)
2322
const filename = path.join(...parts)
2423

25-
console.log('IS PATCHED', filename, !!this._patched[filename])
2624
if (this._patched[filename]) return moduleExports
2725

2826
this._patched[filename] = true

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

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ exports.channel = function (name) {
2121
* @param Function hook
2222
*/
2323
exports.addHook = function addHook ({ name, versions, file }, hook) {
24-
console.log('ADD HOOK', name, versions, file, hook)
2524
if (!instrumentations[name]) {
2625
instrumentations[name] = []
2726
}

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,18 @@ const loadChannel = channel('dd-trace:instrumentation:load')
1616

1717
// TODO: make this more efficient
1818

19-
for (const packageName of names) { // for every module that we instrument
20-
Hook([packageName], (moduleExports, moduleName, moduleBaseDir, moduleVersion) => { // call *ITM
21-
console.log('INSIDE HOOK CALLBACK', moduleVersion)
22-
// the hook callback is encountered upon loading a given module
19+
for (const packageName of names) {
20+
Hook([packageName], (moduleExports, moduleName, moduleBaseDir, moduleVersion) => {
2321
moduleName = moduleName.replace(pathSepExpr, '/')
2422

25-
hooks[packageName]() // builds out instrumentations database
23+
hooks[packageName]()
2624

2725
for (const { name, file, versions, hook } of instrumentations[packageName]) {
2826
const fullFilename = filename(name, file)
2927

3028
if (moduleName === fullFilename) {
3129
const version = moduleVersion || getVersion(moduleBaseDir)
3230

33-
// TODO: how to get register.js to play nicely with dcitm.js?
34-
console.log('REGISTER.JS HOOK', moduleName, version);
3531
if (matchVersion(version, versions)) {
3632
try {
3733
loadChannel.publish({ name, version, file })

packages/dd-trace/src/dcitm.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ module.exports = class DiagnosticsChannelInTheMiddle {
2626
this.modules = modules
2727
this.options = options
2828
this.onrequire = onrequire
29-
console.log('PREP', modules)
3029
for (let module of modules) {
31-
console.log('SUBSCRIBE', `${CHANNEL_PREFIX}:${module}`)
3230
dc.subscribe(`${CHANNEL_PREFIX}:${module}`, this._onModuleLoad.bind(this)) // TODO: shouldn't need a .bind
3331
}
3432
}
@@ -42,11 +40,9 @@ module.exports = class DiagnosticsChannelInTheMiddle {
4240
/**
4341
* @param {string} arg.module name of the module
4442
* @param {string} arg.path path to the module
45-
* @param {string} [arg.version] version of the module
43+
* @param {string} arg.version version of the module
4644
*/
4745
_onModuleLoad(payload) {
48-
console.log(`onModuleLoad(${payload.path}@${payload.version})`)
49-
// TODO: this returns the same module that was passed in
50-
payload.module = this.onrequire(payload.module, payload.path, undefined, payload.version) // TODO: moduleBaseDir
46+
payload.module = this.onrequire(payload.module, payload.path, undefined, payload.version)
5147
}
5248
}

0 commit comments

Comments
 (0)