Skip to content

Commit de000d1

Browse files
authored
Fix forceFlush issue for Lambda Instrumentation and optimizations (#73)
*Description of changes:* 1. Replicate Python SDK changes aws-observability/aws-otel-python-instrumentation#260 to rename Unsampled span attribute flag name. 2. Optimize Lambda Layer Deps package config to reduce layer size. 3. Fixed the forceFlush issue which is to set TraceProvider for all instrumentations at the end of init configuration 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 e794e75 commit de000d1

File tree

12 files changed

+3537
-28
lines changed

12 files changed

+3537
-28
lines changed

aws-distro-opentelemetry-node-autoinstrumentation/src/aws-attribute-keys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const AWS_ATTRIBUTE_KEYS: { [key: string]: string } = {
2222
AWS_IS_LOCAL_ROOT: 'aws.is.local.root',
2323

2424
// Trace Span Unsampled flag
25-
AWS_TRACE_FLAG_UNSAMPLED: 'aws.trace.flag.unsampled',
25+
AWS_TRACE_FLAG_SAMPLED: 'aws.trace.flag.sampled',
2626

2727
// AWS_#_NAME attributes are not supported in JavaScript as they are not part of the Semantic Conventions.
2828
// TODO:Move to Semantic Conventions when these attributes are added.

aws-distro-opentelemetry-node-autoinstrumentation/src/aws-batch-unsampled-span-processor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { BatchSpanProcessorBase } from '@opentelemetry/sdk-trace-base/build/src/
2020
* In particular, the following methods are modified:
2121
*
2222
* 1. `onStart`: This method is modified to detect unsampled spans and add an
23-
* AWS-specific attribute (`AWS_TRACE_FLAG_UNSAMPLED`) to denote that the span
23+
* AWS-specific attribute (`AWS_TRACE_FLAG_SAMPLED`) to denote that the span
2424
* is unsampled. This is done by checking the `traceFlags` of the span.
2525
*
2626
* 2. `onEnd`: The logic here is changed to handle unsampled spans. While the
@@ -37,7 +37,7 @@ import { BatchSpanProcessorBase } from '@opentelemetry/sdk-trace-base/build/src/
3737
export class AwsBatchUnsampledSpanProcessor extends BatchSpanProcessorBase<BufferConfig> {
3838
override onStart(span: Span, _parentContext: Context): void {
3939
if ((span.spanContext().traceFlags & TraceFlags.SAMPLED) === 0) {
40-
span.setAttribute(AWS_ATTRIBUTE_KEYS.AWS_TRACE_FLAG_UNSAMPLED, true);
40+
span.setAttribute(AWS_ATTRIBUTE_KEYS.AWS_TRACE_FLAG_SAMPLED, false);
4141
return;
4242
}
4343
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License.
44

5-
import { diag, DiagConsoleLogger } from '@opentelemetry/api';
5+
import { DiagConsoleLogger, diag, trace } from '@opentelemetry/api';
66
import { getNodeAutoInstrumentations, InstrumentationConfigMap } from '@opentelemetry/auto-instrumentations-node';
77
import { Instrumentation } from '@opentelemetry/instrumentation';
88
import * as opentelemetry from '@opentelemetry/sdk-node';
@@ -67,6 +67,11 @@ const sdk: opentelemetry.NodeSDK = new opentelemetry.NodeSDK(configuration);
6767
// we wish to make contributions to upstream to improve customizability of the Node auto-instrumentation.
6868
try {
6969
sdk.start();
70+
for (const instrumentation of instrumentations) {
71+
diag.info('Set TraceProvider for instrumentations at the end of initialization');
72+
instrumentation.setTracerProvider(trace.getTracerProvider());
73+
}
74+
7075
diag.info('AWS Distro of OpenTelemetry automatic instrumentation started successfully');
7176
diag.debug(`Environment variable OTEL_PROPAGATORS is set to '${process.env.OTEL_PROPAGATORS}'`);
7277
diag.debug(`Environment variable OTEL_EXPORTER_OTLP_PROTOCOL is set to '${process.env.OTEL_EXPORTER_OTLP_PROTOCOL}'`);

aws-distro-opentelemetry-node-autoinstrumentation/test/aws-batch-unsampled-span-processor.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import { Resource, ResourceAttributes } from '@opentelemetry/resources';
1919
import { AwsBatchUnsampledSpanProcessor } from '../src/aws-batch-unsampled-span-processor';
2020
import { AlwaysRecordSampler } from '../src/always-record-sampler';
21+
import { AWS_ATTRIBUTE_KEYS } from '../src/aws-attribute-keys';
2122

2223
/**
2324
* This test file is a modified version of `BatchSpanProcessorBase.test.ts`.
@@ -131,6 +132,7 @@ describe('AwsBatchUnsampledSpanProcessor', () => {
131132

132133
processor.onStart(span, ROOT_CONTEXT);
133134
processor.onEnd(span);
135+
assert.strictEqual(span.attributes[AWS_ATTRIBUTE_KEYS.AWS_TRACE_FLAG_SAMPLED], false);
134136

135137
await processor.forceFlush();
136138
// _finishedSpans should be empty after forceFlush

lambda-layer/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# AWS Distro Lambda Layer for NodeJS
22

3-
This package includes the following three components
3+
By default, the layer enables aws-sdk and aws-lambda instrumentation libraries only for better Lambda cold start
4+
performance.
5+
You can use the environment variable `OTEL_NODE_ENABLED_INSTRUMENTATIONS` to enable only certain
6+
instrumentations, OR the environment variable `OTEL_NODE_DISABLED_INSTRUMENTATIONS` to disable only
7+
certain instrumentations, by providing a comma-separated list of the instrumentation
8+
package names without the @opentelemetry/instrumentation- prefix.
9+
Eg, OTEL_NODE_ENABLED_INSTRUMENTATIONS="aws-lambda,aws-sdk,http"
10+
11+
This package includes the following three components,
412

513
1. AWS Lambda Layer implementation for supporting Application Signals in AWS Lambda.
614
2. Lambda Sample App with AWS SDK to demonstrate Application Signals telemetry data generated by Lambda Layer.

lambda-layer/build.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,28 @@ cd "${SOURCEDIR}/.."
1010

1111
# Install dependencies and compile all projects in the repository
1212
echo "Installing dependencies and compiling projects..."
13+
rm -rf node_modules
1314
rm -rf ./aws-distro-opentelemetry-node-autoinstrumentation/build
1415
rm -rf ./aws-distro-opentelemetry-node-autoinstrumentation/node_modules
1516
npm install
16-
npm run compile
17+
npm run compile || exit
18+
19+
# Build aws distro tar file
20+
cd aws-distro-opentelemetry-node-autoinstrumentation || exit
21+
rm aws-aws-distro-opentelemetry-node-autoinstrumentation-*.tgz
22+
npm pack || exit
23+
24+
# Install Lambda Layer Build Tool
25+
cd ${SOURCEDIR}/packages || exit
26+
rm -rf build
27+
rm -rf node_modules
28+
npm install || exit
1729

1830
# Build Lambda SDK layer
1931
cd ${SOURCEDIR}/packages/layer || exit
2032
rm -rf build
2133
rm -rf node_modules
34+
rm -r package-lock.json
2235
npm install || exit
2336

2437
# Build sample apps

lambda-layer/package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "@aws/aws-distro-opentelemetry-node-autoinstrumentatio-sdk-layer",
3+
"version": "0.0.1",
4+
"description": "Lambda Layer including AWS Distro OpenTelemetry SDK for supporting Amazon Application Signals",
5+
"repository": "aws-observability/aws-otel-js-instrumentation",
6+
"author": {
7+
"name": "Amazon Web Services",
8+
"url": "http://aws.amazon.com"
9+
},
10+
"homepage": "https://github.com/aws-observability/aws-otel-js-instrumentation/tree/main/aws-distro-opentelemetry-node-autoinstrumentation#readme",
11+
"license": "Apache-2.0",
12+
"engines": {
13+
"node": ">=16"
14+
},
15+
"publishConfig": {
16+
"access": "public"
17+
},
18+
"scripts": {
19+
"clean": "rimraf build/*",
20+
"lint": "eslint . --ext .ts",
21+
"lint:fix": "eslint . --ext .ts --fix"
22+
},
23+
"keywords": [
24+
"awsdistroopentelemetry",
25+
"opentelemetry",
26+
"awslambda",
27+
"nodejs",
28+
"tracing",
29+
"profiling",
30+
"instrumentation"
31+
],
32+
"devDependencies": {
33+
"copyfiles": "^2.4.1",
34+
"bestzip": "2.2.0"
35+
}
36+
}

0 commit comments

Comments
 (0)