Skip to content

Commit

Permalink
Fix forceFlush issue for Lambda Instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mxiamxia committed Sep 20, 2024
1 parent e794e75 commit 88ab828
Show file tree
Hide file tree
Showing 12 changed files with 3,537 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const AWS_ATTRIBUTE_KEYS: { [key: string]: string } = {
AWS_IS_LOCAL_ROOT: 'aws.is.local.root',

// Trace Span Unsampled flag
AWS_TRACE_FLAG_UNSAMPLED: 'aws.trace.flag.unsampled',
AWS_TRACE_FLAG_SAMPLED: 'aws.trace.flag.sampled',

// AWS_#_NAME attributes are not supported in JavaScript as they are not part of the Semantic Conventions.
// TODO:Move to Semantic Conventions when these attributes are added.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { BatchSpanProcessorBase } from '@opentelemetry/sdk-trace-base/build/src/
* In particular, the following methods are modified:
*
* 1. `onStart`: This method is modified to detect unsampled spans and add an
* AWS-specific attribute (`AWS_TRACE_FLAG_UNSAMPLED`) to denote that the span
* AWS-specific attribute (`AWS_TRACE_FLAG_SAMPLED`) to denote that the span
* is unsampled. This is done by checking the `traceFlags` of the span.
*
* 2. `onEnd`: The logic here is changed to handle unsampled spans. While the
Expand All @@ -37,7 +37,7 @@ import { BatchSpanProcessorBase } from '@opentelemetry/sdk-trace-base/build/src/
export class AwsBatchUnsampledSpanProcessor extends BatchSpanProcessorBase<BufferConfig> {
override onStart(span: Span, _parentContext: Context): void {
if ((span.spanContext().traceFlags & TraceFlags.SAMPLED) === 0) {
span.setAttribute(AWS_ATTRIBUTE_KEYS.AWS_TRACE_FLAG_UNSAMPLED, true);
span.setAttribute(AWS_ATTRIBUTE_KEYS.AWS_TRACE_FLAG_SAMPLED, false);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License.

import { diag, DiagConsoleLogger } from '@opentelemetry/api';
import { DiagConsoleLogger, diag, trace } from '@opentelemetry/api';
import { getNodeAutoInstrumentations, InstrumentationConfigMap } from '@opentelemetry/auto-instrumentations-node';
import { Instrumentation } from '@opentelemetry/instrumentation';
import * as opentelemetry from '@opentelemetry/sdk-node';
Expand Down Expand Up @@ -67,6 +67,11 @@ const sdk: opentelemetry.NodeSDK = new opentelemetry.NodeSDK(configuration);
// we wish to make contributions to upstream to improve customizability of the Node auto-instrumentation.
try {
sdk.start();
for (const instrumentation of instrumentations) {
diag.info('Set TraceProvider for instrumentations at the end of initialization');
instrumentation.setTracerProvider(trace.getTracerProvider());
}

diag.info('AWS Distro of OpenTelemetry automatic instrumentation started successfully');
diag.debug(`Environment variable OTEL_PROPAGATORS is set to '${process.env.OTEL_PROPAGATORS}'`);
diag.debug(`Environment variable OTEL_EXPORTER_OTLP_PROTOCOL is set to '${process.env.OTEL_EXPORTER_OTLP_PROTOCOL}'`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { Resource, ResourceAttributes } from '@opentelemetry/resources';
import { AwsBatchUnsampledSpanProcessor } from '../src/aws-batch-unsampled-span-processor';
import { AlwaysRecordSampler } from '../src/always-record-sampler';
import { AWS_ATTRIBUTE_KEYS } from '../src/aws-attribute-keys';

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

processor.onStart(span, ROOT_CONTEXT);
processor.onEnd(span);
assert.strictEqual(span.attributes[AWS_ATTRIBUTE_KEYS.AWS_TRACE_FLAG_SAMPLED], false);

await processor.forceFlush();
// _finishedSpans should be empty after forceFlush
Expand Down
10 changes: 9 additions & 1 deletion lambda-layer/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# AWS Distro Lambda Layer for NodeJS

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

This package includes the following three components,

1. AWS Lambda Layer implementation for supporting Application Signals in AWS Lambda.
2. Lambda Sample App with AWS SDK to demonstrate Application Signals telemetry data generated by Lambda Layer.
Expand Down
15 changes: 14 additions & 1 deletion lambda-layer/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,28 @@ cd "${SOURCEDIR}/.."

# Install dependencies and compile all projects in the repository
echo "Installing dependencies and compiling projects..."
rm -rf node_modules
rm -rf ./aws-distro-opentelemetry-node-autoinstrumentation/build
rm -rf ./aws-distro-opentelemetry-node-autoinstrumentation/node_modules
npm install
npm run compile
npm run compile || exit

# Build aws distro tar file
cd aws-distro-opentelemetry-node-autoinstrumentation || exit
rm aws-aws-distro-opentelemetry-node-autoinstrumentation-*.tgz
npm pack || exit

# Install Lambda Layer Build Tool
cd ${SOURCEDIR}/packages || exit
rm -rf build
rm -rf node_modules
npm install || exit

# Build Lambda SDK layer
cd ${SOURCEDIR}/packages/layer || exit
rm -rf build
rm -rf node_modules
rm -r package-lock.json
npm install || exit

# Build sample apps
Expand Down
36 changes: 36 additions & 0 deletions lambda-layer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@aws/aws-distro-opentelemetry-node-autoinstrumentatio-sdk-layer",
"version": "0.0.1",
"description": "Lambda Layer including AWS Distro OpenTelemetry SDK for supporting Amazon Application Signals",
"repository": "aws-observability/aws-otel-js-instrumentation",
"author": {
"name": "Amazon Web Services",
"url": "http://aws.amazon.com"
},
"homepage": "https://github.com/aws-observability/aws-otel-js-instrumentation/tree/main/aws-distro-opentelemetry-node-autoinstrumentation#readme",
"license": "Apache-2.0",
"engines": {
"node": ">=16"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"clean": "rimraf build/*",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix"
},
"keywords": [
"awsdistroopentelemetry",
"opentelemetry",
"awslambda",
"nodejs",
"tracing",
"profiling",
"instrumentation"
],
"devDependencies": {
"copyfiles": "^2.4.1",
"bestzip": "2.2.0"
}
}
Loading

0 comments on commit 88ab828

Please sign in to comment.