diff --git a/aws-distro-opentelemetry-node-autoinstrumentation/src/aws-attribute-keys.ts b/aws-distro-opentelemetry-node-autoinstrumentation/src/aws-attribute-keys.ts index 4afc6e0e..53ee92a7 100644 --- a/aws-distro-opentelemetry-node-autoinstrumentation/src/aws-attribute-keys.ts +++ b/aws-distro-opentelemetry-node-autoinstrumentation/src/aws-attribute-keys.ts @@ -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. diff --git a/aws-distro-opentelemetry-node-autoinstrumentation/src/aws-batch-unsampled-span-processor.ts b/aws-distro-opentelemetry-node-autoinstrumentation/src/aws-batch-unsampled-span-processor.ts index 3d6b89a9..6891aa3c 100644 --- a/aws-distro-opentelemetry-node-autoinstrumentation/src/aws-batch-unsampled-span-processor.ts +++ b/aws-distro-opentelemetry-node-autoinstrumentation/src/aws-batch-unsampled-span-processor.ts @@ -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 @@ -37,7 +37,7 @@ import { BatchSpanProcessorBase } from '@opentelemetry/sdk-trace-base/build/src/ export class AwsBatchUnsampledSpanProcessor extends BatchSpanProcessorBase { 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; } } diff --git a/aws-distro-opentelemetry-node-autoinstrumentation/test/aws-batch-unsampled-span-processor.test.ts b/aws-distro-opentelemetry-node-autoinstrumentation/test/aws-batch-unsampled-span-processor.test.ts index dfb0fc1a..add7ddea 100644 --- a/aws-distro-opentelemetry-node-autoinstrumentation/test/aws-batch-unsampled-span-processor.test.ts +++ b/aws-distro-opentelemetry-node-autoinstrumentation/test/aws-batch-unsampled-span-processor.test.ts @@ -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`. @@ -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 diff --git a/lambda-layer/README.md b/lambda-layer/README.md index c75fee8d..250d52cb 100644 --- a/lambda-layer/README.md +++ b/lambda-layer/README.md @@ -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. @@ -35,5 +43,4 @@ The lambda function(`aws-opentelemetry-distro-nodejs`) will be initialized and t will be displayed at the end. Send a request to the URL in a browser or using curl to execute the function. Then, navigate to Lambda function's Monitoring page. You will see a log stream with an event time and Traces data corresponding to when you issued the request, -open it and you can find -information about the exported spans in the log stream. +open it and you can find information about the exported spans in the log stream. diff --git a/lambda-layer/build.sh b/lambda-layer/build.sh index 7024dd9f..a6b1ce92 100755 --- a/lambda-layer/build.sh +++ b/lambda-layer/build.sh @@ -10,10 +10,22 @@ 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 diff --git a/lambda-layer/package.json b/lambda-layer/package.json new file mode 100644 index 00000000..9e314114 --- /dev/null +++ b/lambda-layer/package.json @@ -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" + } +} diff --git a/lambda-layer/packages/layer/package.json b/lambda-layer/packages/layer/package.json index b2a71d70..5d1a4820 100644 --- a/lambda-layer/packages/layer/package.json +++ b/lambda-layer/packages/layer/package.json @@ -33,21 +33,24 @@ "instrumentation" ], "dependencies": { - "@aws/aws-distro-opentelemetry-node-autoinstrumentation": "file:../../../aws-distro-opentelemetry-node-autoinstrumentation", - "@opentelemetry/api": "1.9.0", - "@opentelemetry/auto-instrumentations-node": "0.48.0", - "@opentelemetry/auto-configuration-propagators": "0.2.0", - "@opentelemetry/exporter-metrics-otlp-grpc": "0.52.1", - "@opentelemetry/exporter-metrics-otlp-http": "0.52.1", - "@opentelemetry/instrumentation": "0.52.1", - "@opentelemetry/id-generator-aws-xray": "1.2.2", - "@opentelemetry/propagator-aws-xray": "1.25.1", - "@opentelemetry/core": "1.25.1", - "@opentelemetry/sdk-trace-base": "1.25.1", - "@opentelemetry/semantic-conventions": "1.25.1", - "@opentelemetry/resources": "1.25.1", - "@opentelemetry/exporter-trace-otlp-proto": "0.52.1", - "@opentelemetry/exporter-zipkin": "1.25.1", - "@opentelemetry/sdk-metrics": "1.25.1" + "@aws/aws-distro-opentelemetry-node-autoinstrumentation": "file:../../../aws-distro-opentelemetry-node-autoinstrumentation/aws-aws-distro-opentelemetry-node-autoinstrumentation-0.1.0-dev0.tgz", + "@opentelemetry/propagator-b3": "1.25.1", + "@opentelemetry/propagator-jaeger": "1.25.1", + "@opentelemetry/resource-detector-azure": "0.2.10", + "@opentelemetry/resource-detector-gcp": "0.29.10", + "@opentelemetry/semantic-conventions": "1.25.1" + }, + "overrides": { + "@aws/aws-distro-opentelemetry-node-autoinstrumentation": { + "@opentelemetry/auto-configuration-propagators": { + "@opentelemetry/propagators-b3": "1.25.1" + }, + "@opentelemetry/resource-detector-azure": { + "@opentelemetry/semantic-conventions": "1.25.1" + }, + "@opentelemetry/resource-detector-gcp": { + "@opentelemetry/semantic-conventions": "1.25.1" + } + } } } diff --git a/lambda-layer/packages/layer/scripts/otel-instrument b/lambda-layer/packages/layer/scripts/otel-instrument index dd317a32..fc347316 100644 --- a/lambda-layer/packages/layer/scripts/otel-instrument +++ b/lambda-layer/packages/layer/scripts/otel-instrument @@ -1,13 +1,18 @@ #!/bin/bash export NODE_OPTIONS="${NODE_OPTIONS} --require /opt/wrapper.js" export LAMBDA_RESOURCE_ATTRIBUTES="cloud.region=$AWS_REGION,cloud.provider=aws,faas.name=$AWS_LAMBDA_FUNCTION_NAME,faas.version=$AWS_LAMBDA_FUNCTION_VERSION,faas.instance=$AWS_LAMBDA_LOG_STREAM_NAME,aws.log.group.names=$AWS_LAMBDA_LOG_GROUP_NAME"; -export OTEL_NODE_ENABLED_INSTRUMENTATIONS="aws-lambda,aws-sdk" + # - If OTEL_EXPORTER_OTLP_PROTOCOL is not set by user, the default exporting protocol is http/protobuf. if [ -z "${OTEL_EXPORTER_OTLP_PROTOCOL}" ]; then export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf fi +# - If OTEL_NODE_ENABLED_INSTRUMENTATIONS is not set by user, use default instrumentation +if [ -z "${OTEL_NODE_ENABLED_INSTRUMENTATIONS}" ]; then + export OTEL_NODE_ENABLED_INSTRUMENTATIONS="aws-lambda,aws-sdk" +fi + # - Set the service name if [ -z "${OTEL_SERVICE_NAME}" ]; then export OTEL_SERVICE_NAME=$AWS_LAMBDA_FUNCTION_NAME;