Skip to content

feat(nodejs): bundling with webpack #1679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
3,571 changes: 2,411 additions & 1,160 deletions nodejs/package-lock.json

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions nodejs/packages/layer/install-externals.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

set -euf -o pipefail

rm -rf ./build/workspace/node_modules

# Space separated list of external NPM packages
EXTERNAL_PACKAGES=( "import-in-the-middle" )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

js newbie here :)

Instead of having this separated script file with a list of external dependencies, would it work if we had a separated node package with package.json where you define all the dependencies. This would be a "noop package", but it would allow you to explicitly select what version of the external dependencies you want to use. and then you install it with just npm install --prefix ./build/workspace --production --ignore-scripts.

Copy link
Contributor Author

@serkan-ozal serkan-ozal Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why I have used bash script to install external packages is that I want to use the same version of import-in-the-middle coming from OTEL packages. So, I have to resolve its version dynamically. That is why I first resolve its version by npm query below and then install it with that specific version.


for EXTERNAL_PACKAGE in "${EXTERNAL_PACKAGES[@]}"
do
echo "Installing external package $EXTERNAL_PACKAGE ..."

PACKAGE_VERSION=$(npm query "#$EXTERNAL_PACKAGE" \
| grep version \
| head -1 \
| awk -F: '{ print $2 }' \
| sed 's/[",]//g')

echo "Resolved version of the external package $EXTERNAL_PACKAGE: $PACKAGE_VERSION"

npm install "$EXTERNAL_PACKAGE@$PACKAGE_VERSION" --prefix ./build/workspace --production --ignore-scripts

echo "Installed external package $EXTERNAL_PACKAGE"
done
1,417 changes: 1,336 additions & 81 deletions nodejs/packages/layer/package-lock.json

Large diffs are not rendered by default.

30 changes: 20 additions & 10 deletions nodejs/packages/layer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
"description": "Layer including OpenTelemetry SDK for use with AWS Lambda.",
"repository": "open-telemetry/opentelemetry-lambda",
"scripts": {
"build": "npm run clean && npm run compile && npm run install-externals && npm run package",
"clean": "rimraf build/*",
"compile:tsc": "tsc --build tsconfig.json",
"compile:webpack": "webpack",
"compile": "npm run compile:webpack",
"copy-js-files": "copyfiles -f 'src/**/*.js' build/workspace && copyfiles 'test/**/*.js' build",
"copy-esm-files": "copyfiles -f 'src/**/*.mjs' build/workspace && copyfiles 'test/**/*.mjs' build",
"install-externals": "./install-externals.sh",
"lint": "ESLINT_USE_FLAT_CONFIG=false eslint . --ext .ts",
"lint:fix": "ESLINT_USE_FLAT_CONFIG=false eslint . --ext .ts --fix",
"build": "npm run clean && npm run compile && npm run postcompile",
"copy-esm-files": "copyfiles 'src/**/*.mjs' build && copyfiles 'test/**/*.mjs' build",
"compile": "tsc -p .",
"postcompile": "npm run copy-esm-files && copyfiles 'package*.json' build/workspace/nodejs && npm install --production --ignore-scripts --prefix build/workspace/nodejs && rm build/workspace/nodejs/package.json build/workspace/nodejs/package-lock.json && copyfiles -f 'scripts/*' build/workspace && copyfiles -f 'build/src/*' build/workspace && cd build/workspace && bestzip ../layer.zip *",
"pretest": "npm run compile",
"package": "cd build/workspace && bestzip ../layer.zip *",
"postcompile": "npm run copy-js-files && npm run copy-esm-files && copyfiles -f 'scripts/*' build/workspace && copyfiles -f 'build/src/*.js' build/workspace && copyfiles -f 'build/src/*.mjs' build/workspace",
"pretest": "npm run compile:tsc",
"test:cjs": "mocha 'test/**/*.spec.ts' --exclude 'test/**/*.spec.mjs' --timeout 10000",
"test:esm": "mocha 'test/**/*.spec.mjs' --exclude 'test/**/*.spec.ts' --timeout 10000",
"test": "npm run test:cjs && npm run test:esm"
Expand Down Expand Up @@ -52,11 +57,11 @@
"@opentelemetry/instrumentation-mongodb": "^0.51.0",
"@opentelemetry/instrumentation-mysql": "^0.45.0",
"@opentelemetry/instrumentation-net": "^0.43.0",
"@opentelemetry/instrumentation-pg": "^0.50.0",
"@opentelemetry/instrumentation-pg": "^0.51.0",
"@opentelemetry/instrumentation-redis": "^0.46.0",
"@opentelemetry/propagator-aws-xray": "^1.26.0",
"@opentelemetry/propagator-aws-xray-lambda": "^0.53.1",
"@opentelemetry/resource-detector-aws": "^1.10.0",
"@opentelemetry/propagator-aws-xray-lambda": "^0.53.0",
"@opentelemetry/resource-detector-aws": "^1.11.0",
"@opentelemetry/resources": "^1.30.0",
"@opentelemetry/sdk-logs": "^0.57.0",
"@opentelemetry/sdk-metrics": "^1.30.0",
Expand All @@ -67,6 +72,11 @@
"@types/sinon": "^17.0.3",
"mocha": "^11.0.1",
"sinon": "^19.0.2",
"ts-node": "^10.9.2"
}
"ts-loader": "^9.5.2",
"ts-node": "^10.9.2",
"webpack": "^5.97.1",
"webpack-cli": "^6.0.1",
"webpack-node-externals": "^3.0.0"
},
"sideEffects": false
}
2 changes: 1 addition & 1 deletion nodejs/packages/layer/scripts/otel-handler
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -ef -o pipefail

export NODE_OPTIONS="${NODE_OPTIONS} --import /opt/loader.mjs --require /opt/wrapper.js"
export NODE_OPTIONS="${NODE_OPTIONS} --import /opt/init.mjs"

if [[ $OTEL_RESOURCE_ATTRIBUTES != *"service.name="* ]]; then
export OTEL_RESOURCE_ATTRIBUTES="service.name=${AWS_LAMBDA_FUNCTION_NAME},${OTEL_RESOURCE_ATTRIBUTES}"
Expand Down
9 changes: 9 additions & 0 deletions nodejs/packages/layer/src/init.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const WRAPPER_INIT_START_TIME = Date.now();
const { default: wrapper } = await import('./wrapper.js');
await wrapper.init();
await wrapper.wrap();
console.log('OpenTelemetry wrapper init completed in', Date.now() - WRAPPER_INIT_START_TIME, 'ms');

const LOADER_INIT_START_TIME = Date.now();
await import('./loader.mjs');
console.log('OpenTelemetry loader init completed in', Date.now() - LOADER_INIT_START_TIME, 'ms');
Loading