Skip to content

Commit 82e563c

Browse files
authored
Improve Lambda Layer cold start time #2 (#171)
*Description of changes:* This PR is a rehash of #163 with some minor changes reverted after several autoinstrumentation package dependencies were upgraded in #168. The changes suggested by the unaddressed comments in the previous PR are addressed here. The primary change made to reduce the cold start time was to bundle the ADOT autoinstrumentation package with webpack instead of using the TS compiler. Using the same build targets as the upstream (see [this PR](https://github.com/open-telemetry/opentelemetry-lambda/pull/1679/files)), the cold start performance of the layer was improved by about 50%, from ~765ms to ~385ms. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 43069b1 commit 82e563c

File tree

10 files changed

+940
-49
lines changed

10 files changed

+940
-49
lines changed

aws-distro-opentelemetry-node-autoinstrumentation/package.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
"repository": "aws-observability/aws-otel-js-instrumentation",
2323
"scripts": {
2424
"clean": "rimraf build/*",
25-
"compile": "tsc -p .",
25+
"compile:tsc": "tsc -p .",
26+
"compile:webpack": "webpack",
27+
"compile": "npm run compile:webpack",
2628
"lint": "eslint . --ext .ts",
2729
"lint:fix": "eslint . --ext .ts --fix",
2830
"create-version": "node -p \"'export const LIB_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'\" > src/version.ts",
@@ -93,8 +95,11 @@
9395
"proxyquire": "^2.1.3",
9496
"rimraf": "5.0.5",
9597
"sinon": "15.2.0",
98+
"ts-loader": "^9.5.2",
9699
"ts-mocha": "10.0.0",
97-
"typescript": "4.4.4"
100+
"typescript": "4.4.4",
101+
"webpack": "^5.98.0",
102+
"webpack-cli": "^6.0.1"
98103
},
99104
"dependencies": {
100105
"@opentelemetry/api": "1.9.0",
@@ -110,6 +115,7 @@
110115
"@opentelemetry/instrumentation-aws-sdk": "0.49.0",
111116
"@opentelemetry/otlp-transformer": "0.57.1",
112117
"@opentelemetry/propagator-aws-xray": "1.26.2",
118+
"@opentelemetry/propagator-aws-xray-lambda": "^0.54.0",
113119
"@opentelemetry/resource-detector-aws": "1.12.0",
114120
"@opentelemetry/resources": "1.30.1",
115121
"@opentelemetry/sdk-metrics": "1.30.1",
@@ -121,6 +127,7 @@
121127
"build/src/**/*.js",
122128
"build/src/**/*.js.map",
123129
"build/src/**/*.d.ts",
130+
"build/src/**/*.d.ts.map",
124131
"build/src/**/*.json"
125132
]
126133
}

aws-distro-opentelemetry-node-autoinstrumentation/src/aws-opentelemetry-configurator.ts

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const FORMAT_OTEL_UNSAMPLED_TRACES_BINARY_PREFIX = 'T1U';
7575
// Follow Python SDK Impl to set the max span batch size
7676
// which will reduce the chance of UDP package size is larger than 64KB
7777
const LAMBDA_SPAN_EXPORT_BATCH_SIZE = 10;
78+
7879
/**
7980
* Aws Application Signals Config Provider creates a configuration object that can be provided to
8081
* the OTel NodeJS SDK for Auto Instrumentation with Application Signals Functionality.

aws-distro-opentelemetry-node-autoinstrumentation/src/register.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function setAwsDefaultEnvironmentVariables(): void {
4141
process.env.OTEL_EXPORTER_OTLP_PROTOCOL = 'http/protobuf';
4242
}
4343
if (!process.env.OTEL_PROPAGATORS) {
44-
process.env.OTEL_PROPAGATORS = 'xray,tracecontext,b3,b3multi';
44+
process.env.OTEL_PROPAGATORS = 'xray,tracecontext';
4545
}
4646
// Disable the following instrumentations by default
4747
// This auto-instrumentation for the `fs` module generates many low-value spans. `dns` is similar.

aws-distro-opentelemetry-node-autoinstrumentation/test/aws-opentelemetry-configurator.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ describe('AwsOpenTelemetryConfiguratorTest', () => {
506506
function validateConfiguratorEnviron() {
507507
// Set by register.ts
508508
expect('http/protobuf').toEqual(process.env.OTEL_EXPORTER_OTLP_PROTOCOL);
509-
expect('xray,tracecontext,b3,b3multi').toEqual(process.env.OTEL_PROPAGATORS);
509+
expect('xray,tracecontext').toEqual(process.env.OTEL_PROPAGATORS);
510510

511511
// Not set
512512
expect(undefined).toEqual(process.env.OTEL_TRACES_SAMPLER);

aws-distro-opentelemetry-node-autoinstrumentation/test/patches/instrumentation-patch.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { AWSXRAY_TRACE_ID_HEADER, AWSXRayPropagator } from '@opentelemetry/propa
2525
import { Context } from 'aws-lambda';
2626
import { SinonStub } from 'sinon';
2727
import { Lambda } from '@aws-sdk/client-lambda';
28-
import nock = require('nock');
28+
import * as nock from 'nock';
2929
import { ReadableSpan } from '@opentelemetry/sdk-trace-base';
3030
import { getTestSpans } from '@opentelemetry/contrib-test-utils';
3131
import { instrumentationConfigs } from '../../src/register';

aws-distro-opentelemetry-node-autoinstrumentation/test/register.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('Register', function () {
3333
it('sets AWS Default Environment Variables', () => {
3434
setAwsDefaultEnvironmentVariables();
3535
expect(process.env.OTEL_EXPORTER_OTLP_PROTOCOL).toEqual('http/protobuf');
36-
expect(process.env.OTEL_PROPAGATORS).toEqual('xray,tracecontext,b3,b3multi');
36+
expect(process.env.OTEL_PROPAGATORS).toEqual('xray,tracecontext');
3737
expect(process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS).toEqual('fs');
3838
});
3939

@@ -79,7 +79,7 @@ describe('Register', function () {
7979

8080
assert.ok(proc.stdout.includes('AWS Distro of OpenTelemetry automatic instrumentation started successfully'));
8181
assert.ok(proc.stdout.includes("Environment variable OTEL_EXPORTER_OTLP_PROTOCOL is set to 'http/protobuf'"));
82-
assert.ok(proc.stdout.includes("Environment variable OTEL_PROPAGATORS is set to 'xray,tracecontext,b3,b3multi'"));
82+
assert.ok(proc.stdout.includes("Environment variable OTEL_PROPAGATORS is set to 'xray,tracecontext'"));
8383

8484
// Check a span has been generated for the GET request done in app.js
8585
assert.ok(proc.stdout.includes("name: 'GET'"), 'console span output in stdout - validate Span Name');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"module": "es2020",
5+
"target": "es2020",
6+
"moduleResolution": "node",
7+
},
8+
"exclude": [
9+
"node_modules",
10+
]
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
entry: './src/register.ts',
5+
target: 'node',
6+
mode: 'production',
7+
externalsPresets: { node: true },
8+
output: {
9+
path: path.resolve('./build/src'),
10+
filename: 'register.js',
11+
library: {
12+
type: 'commonjs2',
13+
}
14+
},
15+
resolve: {
16+
extensions: ['.ts', '.js'],
17+
modules: [
18+
path.resolve('./src'),
19+
'node_modules',
20+
],
21+
},
22+
module: {
23+
rules: [
24+
{
25+
test: /\.ts$/,
26+
use: [
27+
{
28+
loader: 'ts-loader',
29+
options: {
30+
configFile: 'tsconfig.webpack.json'
31+
},
32+
}
33+
],
34+
exclude: /node_modules/,
35+
},
36+
],
37+
},
38+
optimization: {
39+
minimize: true,
40+
providedExports: true,
41+
usedExports: true,
42+
},
43+
};

lambda-layer/packages/layer/scripts/otel-instrument

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fi
6868

6969
# - Set the propagators
7070
if [[ -z "$OTEL_PROPAGATORS" ]]; then
71-
export OTEL_PROPAGATORS="xray,tracecontext,baggage,b3,b3multi"
71+
export OTEL_PROPAGATORS="xray,tracecontext,baggage"
7272
fi
7373

7474
# - Set Application Signals configuration

0 commit comments

Comments
 (0)