Skip to content

Commit a68768f

Browse files
authored
remove fs dependency from lambda hook (#5404)
1 parent f474d01 commit a68768f

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

packages/dd-trace/src/lambda/runtime/patch.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const path = require('path')
44

5-
const { _extractModuleNameAndHandlerPath, _extractModuleRootAndHandler, _getLambdaFilePath } = require('./ritm')
5+
const { _extractModuleNameAndHandlerPath, _extractModuleRootAndHandler, _getLambdaFilePaths } = require('./ritm')
66
const { datadog } = require('../handler')
77
const { addHook } = require('../../../../datadog-instrumentations/src/helpers/instrument')
88
const shimmer = require('../../../../datadog-shimmer')
@@ -65,9 +65,11 @@ if (originalLambdaHandler !== undefined) {
6565
const [_module, handlerPath] = _extractModuleNameAndHandlerPath(moduleAndHandler)
6666

6767
const lambdaStylePath = path.resolve(lambdaTaskRoot, moduleRoot, _module)
68-
const lambdaFilePath = _getLambdaFilePath(lambdaStylePath)
68+
const lambdaFilePaths = _getLambdaFilePaths(lambdaStylePath)
6969

70-
addHook({ name: lambdaFilePath }, patchLambdaModule(handlerPath))
70+
for (const lambdaFilePath of lambdaFilePaths) {
71+
addHook({ name: lambdaFilePath }, patchLambdaModule(handlerPath))
72+
}
7173
} else {
7274
// Instrumentation is done manually.
7375
addHook({ name: 'datadog-lambda-js' }, patchDatadogLambdaModule)

packages/dd-trace/src/lambda/runtime/ritm.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88
'use strict'
99

10-
const fs = require('fs')
1110
const path = require('path')
1211

1312
const log = require('../../log')
@@ -60,23 +59,18 @@ function _extractModuleNameAndHandlerPath (handler) {
6059
}
6160

6261
/**
63-
* Returns the correct path of the file to be patched
64-
* when required.
62+
* Returns all possible paths of the files to be patched when required.
6563
*
6664
* @param {*} lambdaStylePath the path comprised of the `LAMBDA_TASK_ROOT`,
6765
* the root of the module of the Lambda handler, and the module name.
68-
* @returns the lambdaStylePath with the appropiate extension for the hook.
66+
* @returns the lambdaStylePath with appropiate extensions for the hook.
6967
*/
70-
function _getLambdaFilePath (lambdaStylePath) {
71-
let lambdaFilePath = lambdaStylePath
72-
if (fs.existsSync(lambdaStylePath + '.js')) {
73-
lambdaFilePath += '.js'
74-
} else if (fs.existsSync(lambdaStylePath + '.mjs')) {
75-
lambdaFilePath += '.mjs'
76-
} else if (fs.existsSync(lambdaStylePath + '.cjs')) {
77-
lambdaFilePath += '.cjs'
78-
}
79-
return lambdaFilePath
68+
function _getLambdaFilePaths (lambdaStylePath) {
69+
return [
70+
`${lambdaStylePath}.js`,
71+
`${lambdaStylePath}.mjs`,
72+
`${lambdaStylePath}.cjs`
73+
]
8074
}
8175

8276
/**
@@ -92,12 +86,13 @@ const registerLambdaHook = () => {
9286
const [_module] = _extractModuleNameAndHandlerPath(moduleAndHandler)
9387

9488
const lambdaStylePath = path.resolve(lambdaTaskRoot, moduleRoot, _module)
95-
const lambdaFilePath = _getLambdaFilePath(lambdaStylePath)
89+
const lambdaFilePaths = _getLambdaFilePaths(lambdaStylePath)
9690

97-
Hook([lambdaFilePath], (moduleExports) => {
91+
// TODO: Redo this like any other instrumentation.
92+
Hook(lambdaFilePaths, (moduleExports, name) => {
9893
require('./patch')
9994

100-
for (const { hook } of instrumentations[lambdaFilePath]) {
95+
for (const { hook } of instrumentations[name]) {
10196
try {
10297
moduleExports = hook(moduleExports)
10398
} catch (e) {
@@ -133,6 +128,6 @@ const registerLambdaHook = () => {
133128
module.exports = {
134129
_extractModuleRootAndHandler,
135130
_extractModuleNameAndHandlerPath,
136-
_getLambdaFilePath,
131+
_getLambdaFilePaths,
137132
registerLambdaHook
138133
}

0 commit comments

Comments
 (0)