Skip to content

Commit

Permalink
feat(nodejs): bundling with webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
serkan-ozal committed Jan 25, 2025
1 parent 7f3b53e commit 3c93842
Show file tree
Hide file tree
Showing 10 changed files with 3,902 additions and 1,239 deletions.
3,610 changes: 2,456 additions & 1,154 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" )

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,339 changes: 1,299 additions & 40 deletions nodejs/packages/layer/package-lock.json

Large diffs are not rendered by default.

24 changes: 17 additions & 7 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 @@ -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
7 changes: 7 additions & 0 deletions nodejs/packages/layer/src/init.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const WRAPPER_INIT_START_TIME = Date.now();
await import('./wrapper.js');
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');
50 changes: 13 additions & 37 deletions nodejs/packages/layer/src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ import {
AwsLambdaInstrumentation,
AwsLambdaInstrumentationConfig,
} from '@opentelemetry/instrumentation-aws-lambda';
import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns';
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
import { GraphQLInstrumentation } from '@opentelemetry/instrumentation-graphql';
import { GrpcInstrumentation } from '@opentelemetry/instrumentation-grpc';
import { HapiInstrumentation } from '@opentelemetry/instrumentation-hapi';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { IORedisInstrumentation } from '@opentelemetry/instrumentation-ioredis';
import { KoaInstrumentation } from '@opentelemetry/instrumentation-koa';
import { MongoDBInstrumentation } from '@opentelemetry/instrumentation-mongodb';
import { MySQLInstrumentation } from '@opentelemetry/instrumentation-mysql';
import { NetInstrumentation } from '@opentelemetry/instrumentation-net';
import { PgInstrumentation } from '@opentelemetry/instrumentation-pg';
import { RedisInstrumentation } from '@opentelemetry/instrumentation-redis';
import { AWSXRayPropagator } from '@opentelemetry/propagator-aws-xray';
import { AWSXRayLambdaPropagator } from '@opentelemetry/propagator-aws-xray-lambda';

Expand Down Expand Up @@ -130,79 +143,42 @@ function defaultConfigureInstrumentations() {
// Use require statements for instrumentation
// to avoid having to have transitive dependencies on all the typescript definitions.
if (activeInstrumentations.has('dns')) {
const {
DnsInstrumentation,
} = require('@opentelemetry/instrumentation-dns');
instrumentations.push(new DnsInstrumentation());
}
if (activeInstrumentations.has('express')) {
const {
ExpressInstrumentation,
} = require('@opentelemetry/instrumentation-express');
instrumentations.push(new ExpressInstrumentation());
}
if (activeInstrumentations.has('graphql')) {
const {
GraphQLInstrumentation,
} = require('@opentelemetry/instrumentation-graphql');
instrumentations.push(new GraphQLInstrumentation());
}
if (activeInstrumentations.has('grpc')) {
const {
GrpcInstrumentation,
} = require('@opentelemetry/instrumentation-grpc');
instrumentations.push(new GrpcInstrumentation());
}
if (activeInstrumentations.has('hapi')) {
const {
HapiInstrumentation,
} = require('@opentelemetry/instrumentation-hapi');
instrumentations.push(new HapiInstrumentation());
}
if (activeInstrumentations.has('http')) {
const {
HttpInstrumentation,
} = require('@opentelemetry/instrumentation-http');
instrumentations.push(new HttpInstrumentation());
}
if (activeInstrumentations.has('ioredis')) {
const {
IORedisInstrumentation,
} = require('@opentelemetry/instrumentation-ioredis');
instrumentations.push(new IORedisInstrumentation());
}
if (activeInstrumentations.has('koa')) {
const {
KoaInstrumentation,
} = require('@opentelemetry/instrumentation-koa');
instrumentations.push(new KoaInstrumentation());
}
if (activeInstrumentations.has('mongodb')) {
const {
MongoDBInstrumentation,
} = require('@opentelemetry/instrumentation-mongodb');
instrumentations.push(new MongoDBInstrumentation());
}
if (activeInstrumentations.has('mysql')) {
const {
MySQLInstrumentation,
} = require('@opentelemetry/instrumentation-mysql');
instrumentations.push(new MySQLInstrumentation());
}
if (activeInstrumentations.has('net')) {
const {
NetInstrumentation,
} = require('@opentelemetry/instrumentation-net');
instrumentations.push(new NetInstrumentation());
}
if (activeInstrumentations.has('pg')) {
const { PgInstrumentation } = require('@opentelemetry/instrumentation-pg');
instrumentations.push(new PgInstrumentation());
}
if (activeInstrumentations.has('redis')) {
const {
RedisInstrumentation,
} = require('@opentelemetry/instrumentation-redis');
instrumentations.push(new RedisInstrumentation());
}
return instrumentations;
Expand Down
11 changes: 11 additions & 0 deletions nodejs/packages/layer/tsconfig.webpack.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.esm",
"compilerOptions": {
"rootDir": ".",
"outDir": "build"
},
"include": [
"src/**/*.ts",
"test/**/*.ts"
]
}
47 changes: 47 additions & 0 deletions nodejs/packages/layer/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const path = require('path');

module.exports = {
entry: './src/wrapper.ts',
target: 'node',
mode: 'production',
externalsPresets: { node: true }, // in order to ignore built-in modules like path, fs, etc.
externals: [
'import-in-the-middle',
'@aws-sdk',
],
output: {
path: path.resolve('./build/src'),
filename: 'wrapper.js',
library: {
type: 'commonjs2',
}
},
resolve: {
extensions: ['.ts', '.js', '.mjs'],
modules: [
path.resolve('./src'),
'node_modules',
],
},
module: {
rules: [
{
test: /\.ts$/,
use: [
{
loader: 'ts-loader',
options: {
configFile: "tsconfig.webpack.json"
}
}
],
exclude: /node_modules/,
}
],
},
optimization: {
minimize: true,
providedExports: true,
usedExports: true,
},
};
26 changes: 26 additions & 0 deletions nodejs/tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"moduleResolution": "node",
"module": "es2020",
"target": "es2020",
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"noEmitOnError": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"pretty": true,
"sourceMap": true,
"strict": true,
"strictNullChecks": true,
"incremental": true,
"newLine": "LF"
},
"exclude": [
"node_modules"
]
}

0 comments on commit 3c93842

Please sign in to comment.