Skip to content

Commit 3c93842

Browse files
committed
feat(nodejs): bundling with webpack
1 parent 7f3b53e commit 3c93842

10 files changed

+3902
-1239
lines changed

nodejs/package-lock.json

+2,456-1,154
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
set -euf -o pipefail
4+
5+
rm -rf ./build/workspace/node_modules
6+
7+
# Space separated list of external NPM packages
8+
EXTERNAL_PACKAGES=( "import-in-the-middle" )
9+
10+
for EXTERNAL_PACKAGE in "${EXTERNAL_PACKAGES[@]}"
11+
do
12+
echo "Installing external package $EXTERNAL_PACKAGE ..."
13+
14+
PACKAGE_VERSION=$(npm query "#$EXTERNAL_PACKAGE" \
15+
| grep version \
16+
| head -1 \
17+
| awk -F: '{ print $2 }' \
18+
| sed 's/[",]//g')
19+
20+
echo "Resolved version of the external package $EXTERNAL_PACKAGE: $PACKAGE_VERSION"
21+
22+
npm install "$EXTERNAL_PACKAGE@$PACKAGE_VERSION" --prefix ./build/workspace --production --ignore-scripts
23+
24+
echo "Installed external package $EXTERNAL_PACKAGE"
25+
done

nodejs/packages/layer/package-lock.json

+1,299-40
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nodejs/packages/layer/package.json

+17-7
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
"description": "Layer including OpenTelemetry SDK for use with AWS Lambda.",
66
"repository": "open-telemetry/opentelemetry-lambda",
77
"scripts": {
8+
"build": "npm run clean && npm run compile && npm run install-externals && npm run package",
89
"clean": "rimraf build/*",
10+
"compile:tsc": "tsc --build tsconfig.json",
11+
"compile:webpack": "webpack",
12+
"compile": "npm run compile:webpack",
13+
"copy-js-files": "copyfiles -f 'src/**/*.js' build/workspace && copyfiles 'test/**/*.js' build",
14+
"copy-esm-files": "copyfiles -f 'src/**/*.mjs' build/workspace && copyfiles 'test/**/*.mjs' build",
15+
"install-externals": "./install-externals.sh",
916
"lint": "ESLINT_USE_FLAT_CONFIG=false eslint . --ext .ts",
1017
"lint:fix": "ESLINT_USE_FLAT_CONFIG=false eslint . --ext .ts --fix",
11-
"build": "npm run clean && npm run compile && npm run postcompile",
12-
"copy-esm-files": "copyfiles 'src/**/*.mjs' build && copyfiles 'test/**/*.mjs' build",
13-
"compile": "tsc -p .",
14-
"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 *",
15-
"pretest": "npm run compile",
18+
"package": "cd build/workspace && bestzip ../layer.zip *",
19+
"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",
20+
"pretest": "npm run compile:tsc",
1621
"test:cjs": "mocha 'test/**/*.spec.ts' --exclude 'test/**/*.spec.mjs' --timeout 10000",
1722
"test:esm": "mocha 'test/**/*.spec.mjs' --exclude 'test/**/*.spec.ts' --timeout 10000",
1823
"test": "npm run test:cjs && npm run test:esm"
@@ -67,6 +72,11 @@
6772
"@types/sinon": "^17.0.3",
6873
"mocha": "^11.0.1",
6974
"sinon": "^19.0.2",
70-
"ts-node": "^10.9.2"
71-
}
75+
"ts-loader": "^9.5.2",
76+
"ts-node": "^10.9.2",
77+
"webpack": "^5.97.1",
78+
"webpack-cli": "^6.0.1",
79+
"webpack-node-externals": "^3.0.0"
80+
},
81+
"sideEffects": false
7282
}

nodejs/packages/layer/scripts/otel-handler

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -ef -o pipefail
44

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

77
if [[ $OTEL_RESOURCE_ATTRIBUTES != *"service.name="* ]]; then
88
export OTEL_RESOURCE_ATTRIBUTES="service.name=${AWS_LAMBDA_FUNCTION_NAME},${OTEL_RESOURCE_ATTRIBUTES}"

nodejs/packages/layer/src/init.mjs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const WRAPPER_INIT_START_TIME = Date.now();
2+
await import('./wrapper.js');
3+
console.log('OpenTelemetry wrapper init completed in', Date.now() - WRAPPER_INIT_START_TIME, 'ms');
4+
5+
const LOADER_INIT_START_TIME = Date.now();
6+
await import('./loader.mjs');
7+
console.log('OpenTelemetry loader init completed in', Date.now() - LOADER_INIT_START_TIME, 'ms');

nodejs/packages/layer/src/wrapper.ts

+13-37
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ import {
5555
AwsLambdaInstrumentation,
5656
AwsLambdaInstrumentationConfig,
5757
} from '@opentelemetry/instrumentation-aws-lambda';
58+
import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns';
59+
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
60+
import { GraphQLInstrumentation } from '@opentelemetry/instrumentation-graphql';
61+
import { GrpcInstrumentation } from '@opentelemetry/instrumentation-grpc';
62+
import { HapiInstrumentation } from '@opentelemetry/instrumentation-hapi';
63+
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
64+
import { IORedisInstrumentation } from '@opentelemetry/instrumentation-ioredis';
65+
import { KoaInstrumentation } from '@opentelemetry/instrumentation-koa';
66+
import { MongoDBInstrumentation } from '@opentelemetry/instrumentation-mongodb';
67+
import { MySQLInstrumentation } from '@opentelemetry/instrumentation-mysql';
68+
import { NetInstrumentation } from '@opentelemetry/instrumentation-net';
69+
import { PgInstrumentation } from '@opentelemetry/instrumentation-pg';
70+
import { RedisInstrumentation } from '@opentelemetry/instrumentation-redis';
5871
import { AWSXRayPropagator } from '@opentelemetry/propagator-aws-xray';
5972
import { AWSXRayLambdaPropagator } from '@opentelemetry/propagator-aws-xray-lambda';
6073

@@ -130,79 +143,42 @@ function defaultConfigureInstrumentations() {
130143
// Use require statements for instrumentation
131144
// to avoid having to have transitive dependencies on all the typescript definitions.
132145
if (activeInstrumentations.has('dns')) {
133-
const {
134-
DnsInstrumentation,
135-
} = require('@opentelemetry/instrumentation-dns');
136146
instrumentations.push(new DnsInstrumentation());
137147
}
138148
if (activeInstrumentations.has('express')) {
139-
const {
140-
ExpressInstrumentation,
141-
} = require('@opentelemetry/instrumentation-express');
142149
instrumentations.push(new ExpressInstrumentation());
143150
}
144151
if (activeInstrumentations.has('graphql')) {
145-
const {
146-
GraphQLInstrumentation,
147-
} = require('@opentelemetry/instrumentation-graphql');
148152
instrumentations.push(new GraphQLInstrumentation());
149153
}
150154
if (activeInstrumentations.has('grpc')) {
151-
const {
152-
GrpcInstrumentation,
153-
} = require('@opentelemetry/instrumentation-grpc');
154155
instrumentations.push(new GrpcInstrumentation());
155156
}
156157
if (activeInstrumentations.has('hapi')) {
157-
const {
158-
HapiInstrumentation,
159-
} = require('@opentelemetry/instrumentation-hapi');
160158
instrumentations.push(new HapiInstrumentation());
161159
}
162160
if (activeInstrumentations.has('http')) {
163-
const {
164-
HttpInstrumentation,
165-
} = require('@opentelemetry/instrumentation-http');
166161
instrumentations.push(new HttpInstrumentation());
167162
}
168163
if (activeInstrumentations.has('ioredis')) {
169-
const {
170-
IORedisInstrumentation,
171-
} = require('@opentelemetry/instrumentation-ioredis');
172164
instrumentations.push(new IORedisInstrumentation());
173165
}
174166
if (activeInstrumentations.has('koa')) {
175-
const {
176-
KoaInstrumentation,
177-
} = require('@opentelemetry/instrumentation-koa');
178167
instrumentations.push(new KoaInstrumentation());
179168
}
180169
if (activeInstrumentations.has('mongodb')) {
181-
const {
182-
MongoDBInstrumentation,
183-
} = require('@opentelemetry/instrumentation-mongodb');
184170
instrumentations.push(new MongoDBInstrumentation());
185171
}
186172
if (activeInstrumentations.has('mysql')) {
187-
const {
188-
MySQLInstrumentation,
189-
} = require('@opentelemetry/instrumentation-mysql');
190173
instrumentations.push(new MySQLInstrumentation());
191174
}
192175
if (activeInstrumentations.has('net')) {
193-
const {
194-
NetInstrumentation,
195-
} = require('@opentelemetry/instrumentation-net');
196176
instrumentations.push(new NetInstrumentation());
197177
}
198178
if (activeInstrumentations.has('pg')) {
199-
const { PgInstrumentation } = require('@opentelemetry/instrumentation-pg');
200179
instrumentations.push(new PgInstrumentation());
201180
}
202181
if (activeInstrumentations.has('redis')) {
203-
const {
204-
RedisInstrumentation,
205-
} = require('@opentelemetry/instrumentation-redis');
206182
instrumentations.push(new RedisInstrumentation());
207183
}
208184
return instrumentations;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "../../tsconfig.esm",
3+
"compilerOptions": {
4+
"rootDir": ".",
5+
"outDir": "build"
6+
},
7+
"include": [
8+
"src/**/*.ts",
9+
"test/**/*.ts"
10+
]
11+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
entry: './src/wrapper.ts',
5+
target: 'node',
6+
mode: 'production',
7+
externalsPresets: { node: true }, // in order to ignore built-in modules like path, fs, etc.
8+
externals: [
9+
'import-in-the-middle',
10+
'@aws-sdk',
11+
],
12+
output: {
13+
path: path.resolve('./build/src'),
14+
filename: 'wrapper.js',
15+
library: {
16+
type: 'commonjs2',
17+
}
18+
},
19+
resolve: {
20+
extensions: ['.ts', '.js', '.mjs'],
21+
modules: [
22+
path.resolve('./src'),
23+
'node_modules',
24+
],
25+
},
26+
module: {
27+
rules: [
28+
{
29+
test: /\.ts$/,
30+
use: [
31+
{
32+
loader: 'ts-loader',
33+
options: {
34+
configFile: "tsconfig.webpack.json"
35+
}
36+
}
37+
],
38+
exclude: /node_modules/,
39+
}
40+
],
41+
},
42+
optimization: {
43+
minimize: true,
44+
providedExports: true,
45+
usedExports: true,
46+
},
47+
};

nodejs/tsconfig.esm.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"compilerOptions": {
3+
"moduleResolution": "node",
4+
"module": "es2020",
5+
"target": "es2020",
6+
"allowUnreachableCode": false,
7+
"allowUnusedLabels": false,
8+
"declaration": true,
9+
"declarationMap": true,
10+
"esModuleInterop": true,
11+
"forceConsistentCasingInFileNames": true,
12+
"noEmitOnError": true,
13+
"noFallthroughCasesInSwitch": true,
14+
"noImplicitReturns": true,
15+
"noUnusedLocals": true,
16+
"pretty": true,
17+
"sourceMap": true,
18+
"strict": true,
19+
"strictNullChecks": true,
20+
"incremental": true,
21+
"newLine": "LF"
22+
},
23+
"exclude": [
24+
"node_modules"
25+
]
26+
}

0 commit comments

Comments
 (0)