-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathritm.js
More file actions
133 lines (117 loc) · 4.24 KB
/
Copy pathritm.js
File metadata and controls
133 lines (117 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Modifications copyright 2022 Datadog, Inc.
*
* Some functions are part of aws-lambda-nodejs-runtime-interface-client
* https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/blob/v2.1.0/src/utils/UserFunction.ts
*/
'use strict'
const path = require('path')
const log = require('../../log')
const { getEnvironmentVariable } = require('../../config-helper')
const Hook = require('../../../../datadog-instrumentations/src/helpers/hook')
const instrumentations = require('../../../../datadog-instrumentations/src/helpers/instrumentations')
const {
filename,
pathSepExpr
} = require('../../../../datadog-instrumentations/src/helpers/register')
/**
* Breaks the full handler string into two pieces: the module root
* and the actual handler string.
*
* @param {string} fullHandler user's lambda handler, commonly stored in `DD_LAMBDA_HANDLER`.
* @returns {string[]} an array containing the module root and the handler string.
*
* ```js
* _extractModuleRootAndHandler('./api/src/index.nested.handler')
* // => ['./api/src', 'index.nested.handler']
* ```
*/
function _extractModuleRootAndHandler (fullHandler) {
const handlerString = path.basename(fullHandler)
const moduleRoot = fullHandler.slice(0, Math.max(0, fullHandler.indexOf(handlerString)))
return [moduleRoot, handlerString]
}
/**
* Splits the handler string into two pieces: the module name
* and the path to the handler function.
*
* @param {string} handler a handler string containing the module and the handler path.
* @returns {string[]} an array containing the module name and the handler path.
*
* ```js
* _extractModuleNameAndHandlerPath('index.nested.handler')
* // => ['index', 'nested.handler']
* ```
*/
function _extractModuleNameAndHandlerPath (handler) {
const FUNCTION_EXPR = /^([^.]*)\.(.*)$/
const match = handler.match(FUNCTION_EXPR)
if (!match || match.length !== 3) {
throw new Error(`Malformed handler name: ${handler}`)
}
return [match[1], match[2]] // [module, handler-path]
}
/**
* Returns all possible paths of the files to be patched when required.
*
* @param {*} lambdaStylePath the path comprised of the `LAMBDA_TASK_ROOT`,
* the root of the module of the Lambda handler, and the module name.
* @returns the lambdaStylePath with appropiate extensions for the hook.
*/
function _getLambdaFilePaths (lambdaStylePath) {
return [
`${lambdaStylePath}.js`,
`${lambdaStylePath}.mjs`,
`${lambdaStylePath}.cjs`
]
}
/**
* Register a hook for the Lambda handler to be executed when
* the file is required.
*/
const registerLambdaHook = () => {
const lambdaTaskRoot = getEnvironmentVariable('LAMBDA_TASK_ROOT')
const originalLambdaHandler = getEnvironmentVariable('DD_LAMBDA_HANDLER')
if (originalLambdaHandler !== undefined && lambdaTaskRoot !== undefined) {
const [moduleRoot, moduleAndHandler] = _extractModuleRootAndHandler(originalLambdaHandler)
const [_module] = _extractModuleNameAndHandlerPath(moduleAndHandler)
const lambdaStylePath = path.resolve(lambdaTaskRoot, moduleRoot, _module)
const lambdaFilePaths = _getLambdaFilePaths(lambdaStylePath)
// TODO: Redo this like any other instrumentation.
Hook(lambdaFilePaths, (moduleExports, name) => {
require('./patch')
for (const { hook } of instrumentations[name]) {
try {
moduleExports = hook(moduleExports)
} catch (e) {
log.error('Error executing lambda hook', e)
}
}
return moduleExports
})
} else {
const moduleToPatch = 'datadog-lambda-js'
Hook([moduleToPatch], (moduleExports, moduleName, _) => {
moduleName = moduleName.replace(pathSepExpr, '/')
require('./patch')
for (const { name, file, hook } of instrumentations[moduleToPatch]) {
const fullFilename = filename(name, file)
if (moduleName === fullFilename) {
try {
moduleExports = hook(moduleExports)
} catch (e) {
log.error('Error executing lambda hook for datadog-lambda-js', e)
}
}
}
return moduleExports
})
}
}
module.exports = {
_extractModuleRootAndHandler,
_extractModuleNameAndHandlerPath,
_getLambdaFilePaths,
registerLambdaHook
}