Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions packages/datadog-esbuild/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,38 @@ for (const hook of Object.values(hooks)) {
}
}

function moduleOfInterestKey (name, file) {
return file ? `${name}/${file}` : name
}

const builtinCanonicalNames = new Set(require('module').builtinModules)

function addModuleOfInterest (name, file) {
if (!name) return

modulesOfInterest.add(moduleOfInterestKey(name, file))

if (builtinCanonicalNames.has(name)) {
modulesOfInterest.add(moduleOfInterestKey(`node:${name}`, file))
}
}

const modulesOfInterest = new Set()

for (const instrumentation of Object.values(instrumentations)) {
for (const entry of instrumentation) {
if (!entry.file) {
modulesOfInterest.add(entry.name) // e.g. "redis"
} else {
modulesOfInterest.add(`${entry.name}/${entry.file}`) // e.g. "redis/my/file.js"
}
addModuleOfInterest(entry.name, entry.file)
}
}

const RAW_BUILTINS = require('module').builtinModules
const CHANNEL = 'dd-trace:bundler:load'
const path = require('path')
const fs = require('fs')
const { execSync } = require('child_process')

const builtins = new Set()

for (const builtin of RAW_BUILTINS) {
for (const builtin of builtinCanonicalNames) {
builtins.add(builtin)
builtins.add(`node:${builtin}`)
}
Expand Down
22 changes: 9 additions & 13 deletions packages/datadog-instrumentations/src/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ const childProcessChannel = dc.tracingChannel('datadog:child_process:execution')
// ignored exec method because it calls to execFile directly
const execAsyncMethods = ['execFile', 'spawn']

const names = ['child_process', 'node:child_process']

// child_process and node:child_process returns the same object instance, we only want to add hooks once
let patched = false

Expand All @@ -37,18 +35,16 @@ function returnSpawnSyncError (error, context) {
return context.result
}

names.forEach(name => {
addHook({ name }, childProcess => {
if (!patched) {
patched = true
shimmer.massWrap(childProcess, execAsyncMethods, wrapChildProcessAsyncMethod(childProcess.ChildProcess))
shimmer.wrap(childProcess, 'execSync', wrapChildProcessSyncMethod(throwSyncError, true))
shimmer.wrap(childProcess, 'execFileSync', wrapChildProcessSyncMethod(throwSyncError))
shimmer.wrap(childProcess, 'spawnSync', wrapChildProcessSyncMethod(returnSpawnSyncError))
}
addHook({ name: 'child_process' }, childProcess => {
if (!patched) {
patched = true
shimmer.massWrap(childProcess, execAsyncMethods, wrapChildProcessAsyncMethod(childProcess.ChildProcess))
shimmer.wrap(childProcess, 'execSync', wrapChildProcessSyncMethod(throwSyncError, true))
shimmer.wrap(childProcess, 'execFileSync', wrapChildProcessSyncMethod(throwSyncError))
shimmer.wrap(childProcess, 'spawnSync', wrapChildProcessSyncMethod(returnSpawnSyncError))
}

return childProcess
})
return childProcess
})

function normalizeArgs (args, shell) {
Expand Down
3 changes: 1 addition & 2 deletions packages/datadog-instrumentations/src/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ const cryptoCipherCh = channel('datadog:crypto:cipher:start')

const hashMethods = ['createHash', 'createHmac', 'createSign', 'createVerify', 'sign', 'verify']
const cipherMethods = ['createCipheriv', 'createDecipheriv']
const names = ['crypto', 'node:crypto']

addHook({ name: names }, crypto => {
addHook({ name: 'crypto' }, crypto => {
shimmer.massWrap(crypto, hashMethods, wrapCryptoMethod(cryptoHashCh))
shimmer.massWrap(crypto, cipherMethods, wrapCryptoMethod(cryptoCipherCh))
return crypto
Expand Down
3 changes: 1 addition & 2 deletions packages/datadog-instrumentations/src/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ const rrtypes = {
}

const rrtypeMap = new WeakMap()
const names = ['dns', 'node:dns']

addHook({ name: names }, dns => {
addHook({ name: 'dns' }, dns => {
shimmer.wrap(dns, 'lookup', fn => wrap('apm:dns:lookup', fn, 2))
shimmer.wrap(dns, 'lookupService', fn => wrap('apm:dns:lookup_service', fn, 2))
shimmer.wrap(dns, 'resolve', fn => wrap('apm:dns:resolve', fn, 2))
Expand Down
55 changes: 27 additions & 28 deletions packages/datadog-instrumentations/src/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,37 +84,36 @@ const paramsByFileHandleMethods = {
writeFile: ['data', 'options'],
writev: ['buffers', 'position']
}
const names = ['fs', 'node:fs']
names.forEach(name => {
addHook({ name }, fs => {
const asyncMethods = Object.keys(paramsByMethod)
const syncMethods = asyncMethods.map(name => `${name}Sync`)

massWrap(fs, asyncMethods, createWrapFunction())
massWrap(fs, syncMethods, createWrapFunction())
massWrap(fs.promises, asyncMethods, createWrapFunction('promises.'))

wrap(fs.realpath, 'native', createWrapFunction('', 'realpath.native'))
wrap(fs.realpathSync, 'native', createWrapFunction('', 'realpath.native'))
wrap(fs.promises.realpath, 'native', createWrapFunction('', 'realpath.native'))

wrap(fs, 'createReadStream', wrapCreateStream)
wrap(fs, 'createWriteStream', wrapCreateStream)
if (fs.Dir) {
wrap(fs.Dir.prototype, 'close', createWrapFunction('dir.'))
wrap(fs.Dir.prototype, 'closeSync', createWrapFunction('dir.'))
wrap(fs.Dir.prototype, 'read', createWrapFunction('dir.'))
wrap(fs.Dir.prototype, 'readSync', createWrapFunction('dir.'))
wrap(fs.Dir.prototype, Symbol.asyncIterator, createWrapDirAsyncIterator())
}

wrap(fs, 'unwatchFile', createWatchWrapFunction())
wrap(fs, 'watch', createWatchWrapFunction())
wrap(fs, 'watchFile', createWatchWrapFunction())
addHook({ name: 'fs' }, fs => {
const asyncMethods = Object.keys(paramsByMethod)
const syncMethods = asyncMethods.map(name => `${name}Sync`)

massWrap(fs, asyncMethods, createWrapFunction())
massWrap(fs, syncMethods, createWrapFunction())
massWrap(fs.promises, asyncMethods, createWrapFunction('promises.'))

wrap(fs.realpath, 'native', createWrapFunction('', 'realpath.native'))
wrap(fs.realpathSync, 'native', createWrapFunction('', 'realpath.native'))
wrap(fs.promises.realpath, 'native', createWrapFunction('', 'realpath.native'))

wrap(fs, 'createReadStream', wrapCreateStream)
wrap(fs, 'createWriteStream', wrapCreateStream)
if (fs.Dir) {
wrap(fs.Dir.prototype, 'close', createWrapFunction('dir.'))
wrap(fs.Dir.prototype, 'closeSync', createWrapFunction('dir.'))
wrap(fs.Dir.prototype, 'read', createWrapFunction('dir.'))
wrap(fs.Dir.prototype, 'readSync', createWrapFunction('dir.'))
wrap(fs.Dir.prototype, Symbol.asyncIterator, createWrapDirAsyncIterator())
}

wrap(fs, 'unwatchFile', createWatchWrapFunction())
wrap(fs, 'watch', createWatchWrapFunction())
wrap(fs, 'watchFile', createWatchWrapFunction())

return fs
})
return fs
})

function isFirstMethodReturningFileHandle (original) {
return !kHandle && original.name === 'open'
}
Expand Down
31 changes: 12 additions & 19 deletions packages/datadog-instrumentations/src/helpers/hooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
'use strict'

module.exports = {
// Only list unprefixed node modules. They will automatically be instrumented as prefixed and unprefixed.
child_process: () => require('../child_process'),
crypto: () => require('../crypto'),
dns: () => require('../dns'),
fs: { serverless: false, fn: () => require('../fs') },
http: () => require('../http'),
http2: () => require('../http2'),
https: () => require('../http'),
net: () => require('../net'),
url: () => require('../url'),
vm: () => require('../vm'),
// Non Node.js modules
'@anthropic-ai/sdk': { esmFirst: true, fn: () => require('../anthropic') },
'@apollo/server': () => require('../apollo-server'),
'@apollo/gateway': () => require('../apollo'),
Expand Down Expand Up @@ -42,31 +54,24 @@ module.exports = {
'body-parser': () => require('../body-parser'),
bunyan: () => require('../bunyan'),
'cassandra-driver': () => require('../cassandra-driver'),
child_process: () => require('../child_process'),
connect: () => require('../connect'),
cookie: () => require('../cookie'),
'cookie-parser': () => require('../cookie-parser'),
couchbase: () => require('../couchbase'),
crypto: () => require('../crypto'),
cypress: () => require('../cypress'),
'dd-trace-api': () => require('../dd-trace-api'),
dns: () => require('../dns'),
elasticsearch: () => require('../elasticsearch'),
express: () => require('../express'),
'express-mongo-sanitize': () => require('../express-mongo-sanitize'),
'express-session': () => require('../express-session'),
fastify: () => require('../fastify'),
'find-my-way': () => require('../find-my-way'),
fs: { serverless: false, fn: () => require('../fs') },
'generic-pool': () => require('../generic-pool'),
graphql: () => require('../graphql'),
grpc: () => require('../grpc'),
handlebars: () => require('../handlebars'),
hapi: () => require('../hapi'),
hono: { esmFirst: true, fn: () => require('../hono') },
http: () => require('../http'),
http2: () => require('../http2'),
https: () => require('../http'),
ioredis: () => require('../ioredis'),
iovalkey: () => require('../iovalkey'),
'jest-circus': () => require('../jest'),
Expand Down Expand Up @@ -97,18 +102,8 @@ module.exports = {
multer: () => require('../multer'),
mysql: () => require('../mysql'),
mysql2: () => require('../mysql2'),
net: () => require('../net'),
next: () => require('../next'),
'node-serialize': () => require('../node-serialize'),
'node:child_process': () => require('../child_process'),
'node:crypto': () => require('../crypto'),
'node:dns': () => require('../dns'),
'node:http': () => require('../http'),
'node:http2': () => require('../http2'),
'node:https': () => require('../http'),
'node:net': () => require('../net'),
'node:url': () => require('../url'),
'node:vm': () => require('../vm'),
nyc: () => require('../nyc'),
oracledb: () => require('../oracledb'),
openai: { esmFirst: true, fn: () => require('../openai') },
Expand All @@ -135,9 +130,7 @@ module.exports = {
tedious: () => require('../tedious'),
tinypool: { esmFirst: true, fn: () => require('../vitest') },
undici: () => require('../undici'),
url: () => require('../url'),
vitest: { esmFirst: true, fn: () => require('../vitest') },
vm: () => require('../vm'),
when: () => require('../when'),
winston: () => require('../winston'),
workerpool: () => require('../mocha'),
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-instrumentations/src/http/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const endChannel = channel('apm:http:client:request:end')
const asyncStartChannel = channel('apm:http:client:request:asyncStart')
const errorChannel = channel('apm:http:client:request:error')

const names = ['http', 'https', 'node:http', 'node:https']
const names = ['http', 'https']

addHook({ name: names }, hookFn)

Expand Down
7 changes: 2 additions & 5 deletions packages/datadog-instrumentations/src/http/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ const startSetHeaderCh = channel('datadog:http:server:response:set-header:start'

const requestFinishedSet = new WeakSet()

const httpNames = ['http', 'node:http']
const httpsNames = ['https', 'node:https']

addHook({ name: httpNames }, http => {
addHook({ name: 'http' }, http => {
shimmer.wrap(http.ServerResponse.prototype, 'emit', wrapResponseEmit)
shimmer.wrap(http.Server.prototype, 'emit', wrapEmit)
shimmer.wrap(http.ServerResponse.prototype, 'writeHead', wrapWriteHead)
Expand All @@ -34,7 +31,7 @@ addHook({ name: httpNames }, http => {
return http
})

addHook({ name: httpsNames }, http => {
addHook({ name: 'https' }, http => {
// http.ServerResponse not present on https
shimmer.wrap(http.Server.prototype, 'emit', wrapEmit)
return http
Expand Down
4 changes: 1 addition & 3 deletions packages/datadog-instrumentations/src/http2/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ const asyncStartChannel = channel('apm:http2:client:request:asyncStart')
const asyncEndChannel = channel('apm:http2:client:request:asyncEnd')
const errorChannel = channel('apm:http2:client:request:error')

const names = ['http2', 'node:http2']

function createWrapEmit (ctx) {
return function wrapEmit (emit) {
return function (event, arg1) {
Expand Down Expand Up @@ -68,7 +66,7 @@ function wrapConnect (connect) {
}
}

addHook({ name: names }, http2 => {
addHook({ name: 'http2' }, http2 => {
shimmer.wrap(http2, 'connect', wrapConnect)
if (http2.default) http2.default.connect = http2.connect

Expand Down
4 changes: 1 addition & 3 deletions packages/datadog-instrumentations/src/http2/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ const startServerCh = channel('apm:http2:server:request:start')
const errorServerCh = channel('apm:http2:server:request:error')
const emitCh = channel('apm:http2:server:response:emit')

const names = ['http2', 'node:http2']

addHook({ name: names }, http2 => {
addHook({ name: 'http2' }, http2 => {
shimmer.wrap(http2, 'createSecureServer', wrapCreateServer)
shimmer.wrap(http2, 'createServer', wrapCreateServer)
})
Expand Down
4 changes: 1 addition & 3 deletions packages/datadog-instrumentations/src/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ const errorTCPCh = channel('apm:net:tcp:error')
const readyCh = channel('apm:net:tcp:ready')
const connectionCh = channel('apm:net:tcp:connection')

const names = ['net', 'node:net']

addHook({ name: names }, (net, version, name) => {
addHook({ name: 'net' }, (net, version, name) => {
// explicitly require dns so that net gets an instrumented instance
// so that we don't miss the dns calls
if (name === 'net') {
Expand Down
3 changes: 1 addition & 2 deletions packages/datadog-instrumentations/src/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

const { addHook, channel } = require('./helpers/instrument')
const shimmer = require('../../datadog-shimmer')
const names = ['url', 'node:url']

const parseFinishedChannel = channel('datadog:url:parse:finish')
const urlGetterChannel = channel('datadog:url:getter:finish')
const instrumentedGetters = ['host', 'origin', 'hostname']

addHook({ name: names }, function (url) {
addHook({ name: 'url' }, function (url) {
shimmer.wrap(url, 'parse', (parse) => {
return function wrappedParse (input) {
const parsedValue = parse.apply(this, arguments)
Expand Down
3 changes: 1 addition & 2 deletions packages/datadog-instrumentations/src/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

const { channel, addHook } = require('./helpers/instrument')
const shimmer = require('../../datadog-shimmer')
const names = ['vm', 'node:vm']

const runScriptStartChannel = channel('datadog:vm:run-script:start')
const sourceTextModuleStartChannel = channel('datadog:vm:source-text-module:start')

addHook({ name: names }, function (vm) {
addHook({ name: 'vm' }, function (vm) {
vm.Script = class extends vm.Script {
constructor (code) {
super(...arguments)
Expand Down
Loading