Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6,124 changes: 3,332 additions & 2,792 deletions nodejs/package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions nodejs/sample-apps/aws-sdk/cdk.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"app": "npm run build"
}
"app": "npx ts-node --prefer-ts-exts --project ./tsconfig.json bin/app.ts"
}
47 changes: 0 additions & 47 deletions nodejs/sample-apps/aws-sdk/deploy/wrapper/main.tf

This file was deleted.

7 changes: 0 additions & 7 deletions nodejs/sample-apps/aws-sdk/deploy/wrapper/outputs.tf

This file was deleted.

41 changes: 0 additions & 41 deletions nodejs/sample-apps/aws-sdk/deploy/wrapper/variables.tf

This file was deleted.

6 changes: 4 additions & 2 deletions nodejs/sample-apps/aws-sdk/lambda/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { STSClient, GetCallerIdentityCommand } from '@aws-sdk/client-sts';

const sts = new STSClient({});

export const handler = async (
export async function handler(
event: APIGatewayProxyEvent,
_context: Context,
): Promise<APIGatewayProxyResult> => {
): Promise<APIGatewayProxyResult> {
console.log('Received event:', JSON.stringify(event, null, 2));
console.log('Received context:', JSON.stringify(_context, null, 2));

Expand All @@ -38,3 +38,5 @@ export const handler = async (
};
}
};

module.exports = { handler };
29 changes: 29 additions & 0 deletions nodejs/sample-apps/aws-sdk/lambda/lambda-wrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { DiagConsoleLogger, DiagLogLevel, diag } from '@opentelemetry/api';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { AwsLambdaInstrumentation } from '@opentelemetry/instrumentation-aws-lambda';
import { AwsInstrumentation } from '@opentelemetry/instrumentation-aws-sdk';
import { resourceFromAttributes } from '@opentelemetry/resources';
import { AlwaysOnSampler, BatchSpanProcessor, ConsoleSpanExporter} from '@opentelemetry/sdk-trace-base';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';

diag.setLogger(new DiagConsoleLogger(), {
logLevel: DiagLogLevel.DEBUG,
});

const provider = new NodeTracerProvider({
spanProcessors: [new BatchSpanProcessor(new ConsoleSpanExporter()) ],
sampler: new AlwaysOnSampler(),
resource: resourceFromAttributes({
[ATTR_SERVICE_NAME]: process.env.AWS_LAMBDA_FUNCTION_NAME
}),
});

provider.register();

registerInstrumentations({
instrumentations: [
new AwsInstrumentation({ suppressInternalInstrumentation: true }),
new AwsLambdaInstrumentation({}),
],
});
45 changes: 23 additions & 22 deletions nodejs/sample-apps/aws-sdk/lib/otel-sample-lambda-stack.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as path from 'path';

const AWS_ACCOUNT_ID = '184161586896'; // Replace with your AWS account ID if you want to use a specific layer
const NODEJS_LAYER_VERSION = '0_14_0'; // Update with the latest version if needed
const COLLECTOR_LAYER_VERSION = '0_15_0'; // Update with the latest version if needed
import {NodejsFunction} from 'aws-cdk-lib/aws-lambda-nodejs';

export class OtelSampleLambdaStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const region = cdk.Stack.of(this).region;
const architecture = lambda.Architecture.ARM_64;
const collectorLayerArn = `arn:aws:lambda:${region}:${AWS_ACCOUNT_ID}:layer:opentelemetry-collector-${architecture}-${COLLECTOR_LAYER_VERSION}:1`;
const nodejsInstrumentationLayerArn = `arn:aws:lambda:${region}:${AWS_ACCOUNT_ID}:layer:opentelemetry-nodejs-${NODEJS_LAYER_VERSION}:1`;
const target = 'node24';
new NodejsFunction(this, 'MyLambdaFunction', {
functionName: 'instrumentation-test-handler',

new lambda.Function(this, 'MyLambdaFunction', {
runtime: lambda.Runtime.NODEJS_22_X,
runtime: lambda.Runtime.NODEJS_24_X,
handler: 'index.handler',
code: lambda.Code.fromAsset(path.join(__dirname, '../build/lambda')),
layers: [
lambda.LayerVersion.fromLayerVersionArn(this, 'OtelCollectorLayer', collectorLayerArn),
lambda.LayerVersion.fromLayerVersionArn(this, 'NodeJsInstrumentationLayer', nodejsInstrumentationLayerArn)
],
entry: './lambda/index.ts',
bundling: {
forceDockerBundling: false,
minify: true,
sourceMap: true,
target,
tsconfig: 'tsconfig.json',
commandHooks: {
beforeBundling: (_: string, outputDir: string) => [
// the lambda wrapper which is registered via NODE_OPTIONS for OpenTelemetry instrumentation needs to find its way into the bundle
`npx esbuild --bundle lambda/lambda-wrapper.ts --outfile=${outputDir}/lambda-wrapper.js --minify --platform=node --tsconfig='tsconfig.json' --target=${target}`,
],
beforeInstall: () => [],
afterBundling: () => [],
}
},
timeout: cdk.Duration.seconds(30),
memorySize: 256,
architecture,
environment: {
OTEL_EXPORTER_OTLP_ENDPOINT: 'http://localhost:4318/',
OTEL_TRACES_EXPORTER: 'console',
OTEL_METRICS_EXPORTER: 'console',
OTEL_LOG_LEVEL: 'DEBUG',
OTEL_TRACES_SAMPLER: 'always_on',
AWS_LAMBDA_EXEC_WRAPPER: '/opt/otel-handler',
NODE_OPTIONS: '--enable-source-maps --require lambda-wrapper',
},
});
}
}
}
22 changes: 14 additions & 8 deletions nodejs/sample-apps/aws-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
"types": "build/src/index.d.ts",
"repository": "open-telemetry/opentelemetry-lambda",
"scripts": {
"clean": "rimraf build/*",
"lint": "ESLINT_USE_FLAT_CONFIG=false eslint . --ext .ts",
"lint:fix": "ESLINT_USE_FLAT_CONFIG=false eslint . --ext .ts --fix",
"build": "npm run clean && npm run compile && npm run postcompile",
"compile": "tsc -p .",
"postcompile": "copyfiles 'package*.json' build/lambda/ && npm install --production --ignore-scripts --prefix build/lambda/ && cd build/lambda && bestzip ../function.zip *"
"esbuild": "esbuild"
},
"keywords": [
"opentelemetry",
Expand All @@ -37,11 +34,20 @@
"devDependencies": {
"@aws-sdk/client-sts": "^3.826.0",
"@types/node": "24.10.1",
"aws-cdk-lib": "^2.201.0",
"bestzip": "2.2.1",
"copyfiles": "2.4.1",
"rimraf": "6.1.2",
"aws-cdk-lib": "^2.232.2",
"esbuild": "^0.27.1",
"ts-node": "10.9.2",
"typescript": "5.9.3"
},
"dependencies": {
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/core": "^2.2.0",
"@opentelemetry/instrumentation": "^0.208.0",
"@opentelemetry/instrumentation-aws-lambda": "^0.61.1",
"@opentelemetry/instrumentation-aws-sdk": "^0.64.1",
"@opentelemetry/resources": "^2.2.0",
"@opentelemetry/sdk-trace-base": "^2.2.0",
"@opentelemetry/sdk-trace-node": "^2.2.0",
"aws-cdk": "^2.1100.1"
}
}