Skip to content

Commit 015c6db

Browse files
authored
Utility for building a lambda layer artifact (#953)
1 parent 8469983 commit 015c6db

11 files changed

+1141
-0
lines changed

lambda-layer/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore Gradle project-specific cache directory
2+
.gradle
3+
4+
# Ignore Gradle build output directory
5+
build

lambda-layer/build-layer.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
3+
SOURCEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
4+
5+
6+
## Get OTel version
7+
file="$SOURCEDIR/../.github/patches/versions"
8+
version=$(awk -F'=v' '/OTEL_JAVA_INSTRUMENTATION_VERSION/ {print $2}' "$file")
9+
echo "Found OTEL Version: ${version}"
10+
# Exit if the version is empty or null
11+
if [[ -z "$version" ]]; then
12+
echo "Error: Version could not be found in ${file}."
13+
exit 1
14+
fi
15+
16+
17+
## Clone and Patch the OpenTelemetry Java Instrumentation Repository
18+
git clone https://github.com/open-telemetry/opentelemetry-java-instrumentation.git
19+
pushd opentelemetry-java-instrumentation
20+
git checkout v${version} -b tag-v${version}
21+
22+
# This patch is for Lambda related context propagation
23+
patch -p1 < "$SOURCEDIR"/patches/opentelemetry-java-instrumentation.patch
24+
25+
# There is another patch in the .github/patches directory for other changes. We should apply them too for consistency.
26+
patch -p1 < "$SOURCEDIR"/../.github/patches/opentelemetry-java-instrumentation.patch
27+
28+
git add -A
29+
git commit -m "Create patch version"
30+
./gradlew publishToMavenLocal
31+
popd
32+
rm -rf opentelemetry-java-instrumentation
33+
34+
35+
## Build the ADOT Java from current source
36+
pushd "$SOURCEDIR"/..
37+
patch -p1 < "${SOURCEDIR}"/patches/aws-otel-java-instrumentation.patch
38+
CI=false ./gradlew publishToMavenLocal -Prelease.version=${version}-adot-lambda1
39+
popd
40+
41+
42+
## Build ADOT Lambda Java SDK Layer Code
43+
./gradlew build
44+
45+
46+
## Copy ADOT Java Agent downloaded using Gradle task and bundle it with the Lambda handler script
47+
cp "$SOURCEDIR"/build/javaagent/aws-opentelemetry-agent*.jar ./opentelemetry-javaagent.jar
48+
zip -qr opentelemetry-javaagent-layer.zip opentelemetry-javaagent.jar otel-handler

lambda-layer/build.gradle.kts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
plugins {
2+
java
3+
4+
id("com.diffplug.spotless")
5+
}
6+
7+
base.archivesBaseName = "aws-otel-lambda-java-extensions"
8+
group = "software.amazon.opentelemetry.lambda"
9+
10+
repositories {
11+
mavenCentral()
12+
mavenLocal()
13+
}
14+
15+
java {
16+
sourceCompatibility = JavaVersion.VERSION_1_8
17+
targetCompatibility = JavaVersion.VERSION_1_8
18+
}
19+
20+
spotless {
21+
java {
22+
googleJavaFormat("1.15.0")
23+
}
24+
}
25+
26+
val javaagentDependency by configurations.creating {
27+
extendsFrom()
28+
}
29+
30+
dependencies {
31+
compileOnly(platform("io.opentelemetry:opentelemetry-bom:1.32.1"))
32+
compileOnly(platform("io.opentelemetry:opentelemetry-bom-alpha:1.32.1-alpha"))
33+
// Already included in wrapper so compileOnly
34+
compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi")
35+
compileOnly("io.opentelemetry:opentelemetry-sdk-extension-aws")
36+
javaagentDependency("software.amazon.opentelemetry:aws-opentelemetry-agent:1.32.1-adot-lambda1")
37+
}
38+
39+
tasks.register<Copy>("download") {
40+
from(javaagentDependency)
41+
into("$buildDir/javaagent")
42+
}
43+
44+
tasks.named("build") {
45+
dependsOn("download")
46+
}
60.1 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
4+
networkTimeout=10000
5+
zipStoreBase=GRADLE_USER_HOME
6+
zipStorePath=wrapper/dists

lambda-layer/gradlew

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
2+
#!/bin/sh
3+
4+
#
5+
# Copyright © 2015-2021 the original authors.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# https://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
##############################################################################
21+
#
22+
# Gradle start up script for POSIX generated by Gradle.
23+
#
24+
# Important for running:
25+
#
26+
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
27+
# noncompliant, but you have some other compliant shell such as ksh or
28+
# bash, then to run this script, type that shell name before the whole
29+
# command line, like:
30+
#
31+
# ksh Gradle
32+
#
33+
# Busybox and similar reduced shells will NOT work, because this script
34+
# requires all of these POSIX shell features:
35+
# * functions;
36+
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
37+
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
38+
# * compound commands having a testable exit status, especially «case»;
39+
# * various built-in commands including «command», «set», and «ulimit».
40+
#
41+
# Important for patching:
42+
#
43+
# (2) This script targets any POSIX shell, so it avoids extensions provided
44+
# by Bash, Ksh, etc; in particular arrays are avoided.
45+
#
46+
# The "traditional" practice of packing multiple parameters into a
47+
# space-separated string is a well documented source of bugs and security
48+
# problems, so this is (mostly) avoided, by progressively accumulating
49+
# options in "$@", and eventually passing that to Java.
50+
#
51+
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
52+
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
53+
# see the in-line comments for details.
54+
#
55+
# There are tweaks for specific operating systems such as AIX, CygWin,
56+
# Darwin, MinGW, and NonStop.
57+
#
58+
# (3) This script is generated from the Groovy template
59+
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
60+
# within the Gradle project.
61+
#
62+
# You can find Gradle at https://github.com/gradle/gradle/.
63+
#
64+
##############################################################################
65+
66+
# Attempt to set APP_HOME
67+
68+
# Resolve links: $0 may be a link
69+
app_path=$0
70+
71+
# Need this for daisy-chained symlinks.
72+
while
73+
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
74+
[ -h "$app_path" ]
75+
do
76+
ls=$( ls -ld "$app_path" )
77+
link=${ls#*' -> '}
78+
case $link in #(
79+
/*) app_path=$link ;; #(
80+
*) app_path=$APP_HOME$link ;;
81+
esac
82+
done
83+
84+
# This is normally unused
85+
# shellcheck disable=SC2034
86+
APP_BASE_NAME=${0##*/}
87+
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
88+
89+
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
90+
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
91+
92+
# Use the maximum available, or set MAX_FD != -1 to use that value.
93+
MAX_FD=maximum
94+
95+
warn () {
96+
echo "$*"
97+
} >&2
98+
99+
die () {
100+
echo
101+
echo "$*"
102+
echo
103+
exit 1
104+
} >&2
105+
106+
# OS specific support (must be 'true' or 'false').
107+
cygwin=false
108+
msys=false
109+
darwin=false
110+
nonstop=false
111+
case "$( uname )" in #(
112+
CYGWIN* ) cygwin=true ;; #(
113+
Darwin* ) darwin=true ;; #(
114+
MSYS* | MINGW* ) msys=true ;; #(
115+
NONSTOP* ) nonstop=true ;;
116+
esac
117+
118+
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
119+
120+
121+
# Determine the Java command to use to start the JVM.
122+
if [ -n "$JAVA_HOME" ] ; then
123+
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
124+
# IBM's JDK on AIX uses strange locations for the executables
125+
JAVACMD=$JAVA_HOME/jre/sh/java
126+
else
127+
JAVACMD=$JAVA_HOME/bin/java
128+
fi
129+
if [ ! -x "$JAVACMD" ] ; then
130+
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
131+
132+
Please set the JAVA_HOME variable in your environment to match the
133+
location of your Java installation."
134+
fi
135+
else
136+
JAVACMD=java
137+
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
138+
139+
Please set the JAVA_HOME variable in your environment to match the
140+
location of your Java installation."
141+
fi
142+
143+
# Increase the maximum file descriptors if we can.
144+
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
145+
case $MAX_FD in #(
146+
max*)
147+
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
148+
# shellcheck disable=SC3045
149+
MAX_FD=$( ulimit -H -n ) ||
150+
warn "Could not query maximum file descriptor limit"
151+
esac
152+
case $MAX_FD in #(
153+
'' | soft) :;; #(
154+
*)
155+
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
156+
# shellcheck disable=SC3045
157+
ulimit -n "$MAX_FD" ||
158+
warn "Could not set maximum file descriptor limit to $MAX_FD"
159+
esac
160+
fi
161+
162+
# Collect all arguments for the java command, stacking in reverse order:
163+
# * args from the command line
164+
# * the main class name
165+
# * -classpath
166+
# * -D...appname settings
167+
# * --module-path (only if needed)
168+
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
169+
170+
# For Cygwin or MSYS, switch paths to Windows format before running java
171+
if "$cygwin" || "$msys" ; then
172+
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
173+
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
174+
175+
JAVACMD=$( cygpath --unix "$JAVACMD" )
176+
177+
# Now convert the arguments - kludge to limit ourselves to /bin/sh
178+
for arg do
179+
if
180+
case $arg in #(
181+
-*) false ;; # don't mess with options #(
182+
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
183+
[ -e "$t" ] ;; #(
184+
*) false ;;
185+
esac
186+
then
187+
arg=$( cygpath --path --ignore --mixed "$arg" )
188+
fi
189+
# Roll the args list around exactly as many times as the number of
190+
# args, so each arg winds up back in the position where it started, but
191+
# possibly modified.
192+
#
193+
# NB: a `for` loop captures its iteration list before it begins, so
194+
# changing the positional parameters here affects neither the number of
195+
# iterations, nor the values presented in `arg`.
196+
shift # remove old arg
197+
set -- "$@" "$arg" # push replacement arg
198+
done
199+
fi
200+
201+
# Collect all arguments for the java command;
202+
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
203+
# shell script including quotes and variable substitutions, so put them in
204+
# double quotes to make sure that they get re-expanded; and
205+
# * put everything else in single quotes, so that it's not re-expanded.
206+
207+
set -- \
208+
"-Dorg.gradle.appname=$APP_BASE_NAME" \
209+
-classpath "$CLASSPATH" \
210+
org.gradle.wrapper.GradleWrapperMain \
211+
"$@"
212+
213+
# Stop when "xargs" is not available.
214+
if ! command -v xargs >/dev/null 2>&1
215+
then
216+
die "xargs is not available"
217+
fi
218+
219+
# Use "xargs" to parse quoted args.
220+
#
221+
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
222+
#
223+
# In Bash we could simply go:
224+
#
225+
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
226+
# set -- "${ARGS[@]}" "$@"
227+
#
228+
# but POSIX shell has neither arrays nor command substitution, so instead we
229+
# post-process each arg (as a line of input to sed) to backslash-escape any
230+
# character that might be a shell metacharacter, then use eval to reverse
231+
# that process (while maintaining the separation between arguments), and wrap
232+
# the whole thing up as a single "set" statement.
233+
#
234+
# This will of course break if any of these variables contains a newline or
235+
# an unmatched quote.
236+
#
237+
238+
eval "set -- $(
239+
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
240+
xargs -n1 |
241+
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
242+
tr '\n' ' '
243+
)" '"$@"'
244+
245+
exec "$JAVACMD" "$@"

0 commit comments

Comments
 (0)