Skip to content
Closed
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
2 changes: 1 addition & 1 deletion lambda-layer/build-layer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ popd

## Copy ADOT Java Agent downloaded using Gradle task and bundle it with the Lambda handler script
cp "$SOURCEDIR"/build/javaagent/aws-opentelemetry-agent*.jar ./opentelemetry-javaagent.jar
zip -qr opentelemetry-javaagent-layer.zip opentelemetry-javaagent.jar otel-handler
zip -qr opentelemetry-javaagent-layer.zip opentelemetry-javaagent.jar otel-instrument
24 changes: 0 additions & 24 deletions lambda-layer/otel-handler

This file was deleted.

38 changes: 38 additions & 0 deletions lambda-layer/otel-instrument
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

export OTEL_INSTRUMENTATION_AWS_SDK_EXPERIMENTAL_SPAN_ATTRIBUTES=true

export OTEL_PROPAGATORS="${OTEL_PROPAGATORS:-xray,tracecontext,b3,b3multi}"

export OTEL_SERVICE_NAME=${OTEL_SERVICE_NAME:-${AWS_LAMBDA_FUNCTION_NAME}}

export JAVA_TOOL_OPTIONS="-javaagent:/opt/opentelemetry-javaagent.jar ${JAVA_TOOL_OPTIONS}"

if [[ $OTEL_RESOURCE_ATTRIBUTES != *"service.name="* ]]; then
export OTEL_RESOURCE_ATTRIBUTES="service.name=${AWS_LAMBDA_FUNCTION_NAME},${OTEL_RESOURCE_ATTRIBUTES}"
fi

export OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT=10000

# Disable the Application Signals runtime metrics since we are on Lambda
export OTEL_AWS_APPLICATION_SIGNALS_RUNTIME_ENABLED=false

# Use OTLP traces exporter if not specified
export OTEL_TRACES_EXPORTER=${OTEL_TRACES_EXPORTER:-"otlp"}

# Disable metrics and logs export by default if not specified
export OTEL_METRICS_EXPORTER=${OTEL_METRICS_EXPORTER:-"none"}
export OTEL_LOGS_EXPORTER=${OTEL_LOGS_EXPORTER:-"none"}

# Enable Application Signals by default if not specified
export OTEL_AWS_APPLICATION_SIGNALS_ENABLED=${OTEL_AWS_APPLICATION_SIGNALS_ENABLED:-"true"}

# Append Lambda Resource Attributes to OTel Resource Attribute List
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";
if [ -z "${OTEL_RESOURCE_ATTRIBUTES}" ]; then
export OTEL_RESOURCE_ATTRIBUTES=$LAMBDA_RESOURCE_ATTRIBUTES;
else
export OTEL_RESOURCE_ATTRIBUTES="$LAMBDA_RESOURCE_ATTRIBUTES,$OTEL_RESOURCE_ATTRIBUTES";
fi

exec "$@"
38 changes: 38 additions & 0 deletions sample-apps/apigateway-lambda/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
plugins {
java
application
}

application {
mainClass.set("com.amazon.sampleapp.LambdaHandler")
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}

dependencies {
implementation("com.amazonaws:aws-lambda-java-core:1.2.2")
implementation("com.squareup.okhttp3:okhttp:4.11.0")
implementation("software.amazon.awssdk:s3:2.20.0")
testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
}

tasks.jar {
manifest {
attributes["Main-Class"] = "com.amazon.sampleapp.LambdaHandler"
}
}

tasks.register<Zip>("createLambdaZip") {
dependsOn("build")
from(tasks.compileJava.get())
from(tasks.processResources.get())
into("lib") {
from(configurations.runtimeClasspath.get())
}
archiveFileName.set("lambda-function.zip")
destinationDirectory.set(layout.buildDirectory.dir("distributions"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.amazon.sampleapp;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ListBucketsResponse;

public class LambdaHandler implements RequestHandler<Object, String> {
private final OkHttpClient httpClient;
private final S3Client s3Client;

public LambdaHandler() {
this.httpClient = new OkHttpClient();
this.s3Client = S3Client.create();
}

@Override
public String handleRequest(Object o, Context context) {
makeRemoteCall();
listS3Buckets();

// Get the _X_AMZN_TRACE_ID environment variable
String traceId = System.getenv("_X_AMZN_TRACE_ID");

// Construct the response string
return "Trace ID: " + traceId;
}

private void makeRemoteCall() {
try {
Request request = new Request.Builder().url("https://aws.amazon.com/").build();
Response response = httpClient.newCall(request).execute();
response.close();
} catch (Exception e) {
e.printStackTrace();
}
}

private void listS3Buckets() {
ListBucketsResponse response = s3Client.listBuckets();
int bucketCount = response.buckets().size();

// Print bucket count
System.out.println("Number of S3 buckets: " + bucketCount);
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ include(":smoke-tests:spring-boot")
include(":sample-apps:springboot")
include(":sample-apps:spark")
include(":sample-apps:spark-awssdkv1")
include(":sample-apps:apigateway-lambda")

// Used for contract tests
include("appsignals-tests:images:mock-collector")
Expand Down
Loading