-
Notifications
You must be signed in to change notification settings - Fork 15
Improve NodeJS Lambda Layer cold start time #163
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
Changes from all commits
30497e0
7e43382
718bbec
cab9e49
279de52
244c3c7
c6b2d64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,9 @@ | |
| "repository": "aws-observability/aws-otel-js-instrumentation", | ||
| "scripts": { | ||
| "clean": "rimraf build/*", | ||
| "compile": "tsc -p .", | ||
| "compile:tsc": "tsc -p .", | ||
| "compile:webpack": "webpack", | ||
| "compile": "npm run compile:webpack", | ||
| "lint": "eslint . --ext .ts", | ||
| "lint:fix": "eslint . --ext .ts --fix", | ||
| "create-version": "node -p \"'export const LIB_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'\" > src/version.ts", | ||
|
|
@@ -93,12 +95,14 @@ | |
| "proxyquire": "^2.1.3", | ||
| "rimraf": "5.0.5", | ||
| "sinon": "15.2.0", | ||
| "ts-loader": "^9.5.2", | ||
| "ts-mocha": "10.0.0", | ||
| "typescript": "4.4.4" | ||
| "typescript": "4.4.4", | ||
| "webpack": "^5.98.0", | ||
| "webpack-cli": "^6.0.1" | ||
| }, | ||
| "dependencies": { | ||
| "@opentelemetry/api": "1.9.0", | ||
| "@opentelemetry/auto-configuration-propagators": "0.3.0", | ||
| "@opentelemetry/auto-instrumentations-node": "0.50.0", | ||
| "@opentelemetry/core": "1.26.0", | ||
| "@opentelemetry/exporter-metrics-otlp-grpc": "0.53.0", | ||
|
|
@@ -110,7 +114,8 @@ | |
| "@opentelemetry/instrumentation-aws-sdk": "0.44.0", | ||
| "@opentelemetry/otlp-transformer": "0.53.0", | ||
| "@opentelemetry/propagator-aws-xray": "1.26.0", | ||
| "@opentelemetry/resource-detector-aws": "1.6.1", | ||
| "@opentelemetry/propagator-aws-xray-lambda": "^0.53.2", | ||
| "@opentelemetry/resource-detector-aws": "1.12.0", | ||
| "@opentelemetry/resources": "1.26.0", | ||
| "@opentelemetry/sdk-metrics": "1.26.0", | ||
| "@opentelemetry/sdk-node": "0.53.0", | ||
|
|
@@ -119,8 +124,8 @@ | |
| }, | ||
| "files": [ | ||
| "build/src/**/*.js", | ||
| "build/src/**/*.js.map", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we keep the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in #171 |
||
| "build/src/**/*.d.ts", | ||
| "build/src/**/*.d.ts.map", | ||
| "build/src/**/*.json" | ||
| ] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,16 @@ | |
| // Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License. | ||
|
|
||
| import { TextMapPropagator, diag } from '@opentelemetry/api'; | ||
| import { getPropagator } from '@opentelemetry/auto-configuration-propagators'; | ||
| import { getResourceDetectors as getResourceDetectorsFromEnv } from '@opentelemetry/auto-instrumentations-node'; | ||
| import { ENVIRONMENT, TracesSamplerValues, getEnv, getEnvWithoutDefaults } from '@opentelemetry/core'; | ||
| import { | ||
| CompositePropagator, | ||
| ENVIRONMENT, | ||
| TracesSamplerValues, | ||
| getEnv, | ||
| getEnvWithoutDefaults, | ||
| W3CBaggagePropagator, | ||
| W3CTraceContextPropagator, | ||
| } from '@opentelemetry/core'; | ||
| import { OTLPMetricExporter as OTLPGrpcOTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc'; | ||
| import { | ||
| AggregationTemporalityPreference, | ||
|
|
@@ -17,6 +24,8 @@ import { OTLPTraceExporter as OTLPProtoTraceExporter } from '@opentelemetry/expo | |
| import { ZipkinExporter } from '@opentelemetry/exporter-zipkin'; | ||
| import { AWSXRayIdGenerator } from '@opentelemetry/id-generator-aws-xray'; | ||
| import { Instrumentation } from '@opentelemetry/instrumentation'; | ||
| import { AWSXRayPropagator } from '@opentelemetry/propagator-aws-xray'; | ||
| import { AWSXRayLambdaPropagator } from '@opentelemetry/propagator-aws-xray-lambda'; | ||
| import { awsEc2DetectorSync, awsEcsDetectorSync, awsEksDetectorSync } from '@opentelemetry/resource-detector-aws'; | ||
| import { | ||
| Detector, | ||
|
|
@@ -75,6 +84,13 @@ const FORMAT_OTEL_UNSAMPLED_TRACES_BINARY_PREFIX = 'T1U'; | |
| // Follow Python SDK Impl to set the max span batch size | ||
| // which will reduce the chance of UDP package size is larger than 64KB | ||
| const LAMBDA_SPAN_EXPORT_BATCH_SIZE = 10; | ||
|
|
||
| const propagatorMap = new Map<string, () => TextMapPropagator>([ | ||
| ['tracecontext', () => new W3CTraceContextPropagator()], | ||
| ['baggage', () => new W3CBaggagePropagator()], | ||
| ['xray', () => new AWSXRayPropagator()], | ||
| ['xray-lambda', () => new AWSXRayLambdaPropagator()], | ||
| ]); | ||
| /** | ||
| * Aws Application Signals Config Provider creates a configuration object that can be provided to | ||
| * the OTel NodeJS SDK for Auto Instrumentation with Application Signals Functionality. | ||
|
|
@@ -163,7 +179,7 @@ export class AwsOpentelemetryConfigurator { | |
| this.resource = autoResource; | ||
|
|
||
| this.instrumentations = instrumentations; | ||
| this.propagator = getPropagator(); | ||
| this.propagator = this.getPropagator(); | ||
|
|
||
| // TODO: Consider removing AWSXRayIdGenerator as it is not needed | ||
| // Similarly to Java, always use AWS X-Ray Id Generator | ||
|
|
@@ -190,6 +206,32 @@ export class AwsOpentelemetryConfigurator { | |
| return autoResource; | ||
| } | ||
|
|
||
| private getPropagator(): TextMapPropagator { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: This can be a regular function outside of this class.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed in #171 |
||
| if (process.env.OTEL_PROPAGATORS == null || process.env.OTEL_PROPAGATORS.trim() === '') { | ||
| return new CompositePropagator({ | ||
|
Comment on lines
+209
to
+211
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a better option to not rewrite this function by copying it from upstream? can we just patch
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #171, we revert to using the upstream dependency |
||
| propagators: [new W3CTraceContextPropagator(), new W3CBaggagePropagator()], | ||
| }); | ||
| } | ||
| const propagatorsFromEnv = Array.from( | ||
| new Set(process.env.OTEL_PROPAGATORS?.split(',').map(value => value.toLowerCase().trim())) | ||
| ); | ||
| const propagators = propagatorsFromEnv.flatMap(propagatorName => { | ||
| if (propagatorName === 'none') { | ||
| diag.info( | ||
| 'Not selecting any propagator for value "none" specified in the environment variable OTEL_PROPAGATORS' | ||
| ); | ||
| return []; | ||
| } | ||
| const propagatorFactoryFunction = propagatorMap.get(propagatorName); | ||
| if (propagatorFactoryFunction == null) { | ||
| diag.warn(`Invalid propagator "${propagatorName}" specified in the environment variable OTEL_PROPAGATORS`); | ||
| return []; | ||
| } | ||
| return propagatorFactoryFunction(); | ||
| }); | ||
| return new CompositePropagator({ propagators }); | ||
| } | ||
|
|
||
| public configure(): Partial<NodeSDKConfiguration> { | ||
| // config.autoDetectResources is set to False, as the resources are detected and added to the | ||
| // resource ahead of time. The resource is needed to be populated ahead of time instead of letting | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "extends": "./tsconfig.json", | ||
| "compilerOptions": { | ||
| "module": "es2020", | ||
| "target": "es2020", | ||
| "moduleResolution": "node", | ||
| }, | ||
|
Comment on lines
+4
to
+7
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this the same target being used by otel lambda upstream?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, see here. |
||
| "exclude": [ | ||
| "node_modules", | ||
| ] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| const path = require('path'); | ||
|
|
||
| module.exports = { | ||
| entry: './src/register.ts', | ||
| target: 'node', | ||
| mode: 'production', | ||
| externalsPresets: { node: true }, | ||
| output: { | ||
| path: path.resolve('./build/src'), | ||
| filename: 'register.js', | ||
| library: { | ||
| type: 'commonjs2', | ||
| } | ||
| }, | ||
| resolve: { | ||
| extensions: ['.ts', '.js'], | ||
| 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, | ||
| }, | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
who will run "compile:tsc" if we shift "compile" to "compile:webpack" as code compiling entry point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It probably won't be run unless someone wanted to see the previous package structure.