Skip to content

Commit 639633b

Browse files
authored
Add scripts to publish Java tracer layer. Also add code to set the correct tags in traces (#26)
* Add scripts to publish Java layer versions * Add JSON Trace Context to MDC * update getTraceContext's docstring * Set tags on the span * set version number to 9 * Delete old layer_names line * Layer signing * address PR comments
1 parent 658eb7f commit 639633b

File tree

8 files changed

+488
-48
lines changed

8 files changed

+488
-48
lines changed

build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,23 @@ dependencies {
3030
implementation 'org.apache.commons:commons-lang3:3.10'
3131
implementation 'org.slf4j:slf4j-log4j12:1.7.28'
3232
implementation 'org.jetbrains:annotations:15.0'
33+
implementation 'io.opentracing:opentracing-api:0.33.0'
34+
implementation 'io.opentracing:opentracing-util:0.33.0'
35+
implementation 'com.datadoghq:dd-trace-api:0.72.0'
3336

3437
// Use JUnit test framework
3538
testImplementation 'junit:junit:4.12'
3639
testImplementation 'org.apache.logging.log4j:log4j-api:2.13.3'
3740
testImplementation 'org.apache.logging.log4j:log4j-core:2.13.3'
3841
testImplementation 'com.github.stefanbirkner:system-rules:1.19.0'
42+
3943
}
4044

4145
sourceCompatibility = 1.8
4246
targetCompatibility = 1.8
4347

4448
group = 'com.datadoghq'
45-
version= '0.0.8'
49+
version= '0.0.9'
4650

4751
allprojects {
4852
repositories {

scripts/list_layers.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
# Unless explicitly stated otherwise all files in this repository are licensed
4+
# under the Apache License Version 2.0.
5+
# This product includes software developed at Datadog (https://www.datadoghq.com/).
6+
# Copyright 2019 Datadog, Inc.
7+
8+
# Lists most recent layers ARNs across regions to STDOUT
9+
10+
set -e
11+
12+
AVAILABLE_REGIONS=$(aws ec2 describe-regions --output json | jq -r '.[] | .[] | .RegionName')
13+
LAYERS_MISSING_REGIONS=()
14+
15+
# Check region arg
16+
if [ -z "$2" ]; then
17+
>&2 echo "Region parameter not specified, running for all available regions."
18+
REGIONS=$AVAILABLE_REGIONS
19+
else
20+
21+
>&2 echo "Region parameter specified: $2"
22+
if [[ ! "$AVAILABLE_REGIONS" == *"$2"* ]]; then
23+
>&2 echo "Could not find $2 in available regions:" $AVAILABLE_REGIONS
24+
>&2 echo ""
25+
>&2 echo "EXITING SCRIPT."
26+
exit 1
27+
fi
28+
REGIONS=($2)
29+
fi
30+
31+
32+
for region in $REGIONS
33+
do
34+
layer_name="dd-trace-java"
35+
last_layer_arn=$(aws lambda list-layer-versions --layer-name $layer_name --region $region --output json | jq -r ".LayerVersions | .[0] | .LayerVersionArn")
36+
if [ "$last_layer_arn" == "null" ]; then
37+
>&2 echo "No layer found for $region, $layer_name"
38+
if [[ ! " ${LAYERS_MISSING_REGIONS[@]} " =~ " ${region} " ]]; then
39+
LAYERS_MISSING_REGIONS+=( $region )
40+
fi
41+
else
42+
echo $last_layer_arn
43+
fi
44+
done
45+
46+
if [ ${#LAYERS_MISSING_REGIONS[@]} -gt 0 ]; then
47+
echo "WARNING: Following regions missing layers: ${LAYERS_MISSING_REGIONS[@]}"
48+
exit 1
49+
fi

scripts/publish_layers.sh

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/bin/bash
2+
3+
# Unless explicitly stated otherwise all files in this repository are licensed
4+
# under the Apache License Version 2.0.
5+
# This product includes software developed at Datadog (https://www.datadoghq.com/).
6+
# Copyright 2021 Datadog, Inc.
7+
8+
# Publish the datadog python lambda layer across regions, using the AWS CLI
9+
# Usage: publish_layer.sh [region] [layer]
10+
# Specifying the region and layer arg will publish the specified layer to the specified region
11+
set -e
12+
13+
# Makes sure any subprocesses will be terminated with this process
14+
trap "pkill -P $$; exit 1;" INT
15+
16+
LATEST_JAR_URL=$(curl -i https://repository.sonatype.org/service/local/artifact/maven/redirect\?r\=central-proxy\&g\=com.datadoghq\&a\=dd-java-agent\&v\=LATEST | grep location | cut -d " " -f 2)
17+
18+
LATEST_JAR_FILE=$(echo $LATEST_JAR_URL | rev | cut -d "/" -f 1 | rev)
19+
20+
echo ""
21+
echo "About to publish a new Lambda layer containing $LATEST_JAR_FILE"
22+
read -r -p "Continue? [y/N] " response
23+
if ! [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]
24+
then
25+
echo "OK, exiting!"
26+
exit 0
27+
fi
28+
29+
wget -O $LATEST_JAR_FILE https://dtdg.co/latest-java-tracer
30+
31+
LAYER_ZIP="tracer.zip"
32+
33+
rm -rf layer
34+
mkdir -p layer/java/lib
35+
cp "$LATEST_JAR_FILE" layer/java/lib/dd-java-agent.jar
36+
cd layer
37+
zip -r ../$LAYER_ZIP java
38+
cd ..
39+
rm -rf layer
40+
41+
# sign the layer zip
42+
43+
echo
44+
echo "Signing layers..."
45+
./scripts/sign_layers.sh prod $LAYER_ZIP
46+
47+
48+
AVAILABLE_REGIONS=$(aws ec2 describe-regions --output json | jq -r '.[] | .[] | .RegionName')
49+
50+
51+
# Check region arg
52+
if [ -z "$1" ]; then
53+
echo "Region parameter not specified, running for all available regions."
54+
REGIONS=$AVAILABLE_REGIONS
55+
else
56+
echo "Region parameter specified: $1"
57+
if [[ ! "$AVAILABLE_REGIONS" == *"$1"* ]]; then
58+
echo "Could not find $1 in available regions: $AVAILABLE_REGIONS"
59+
echo ""
60+
echo "EXITING SCRIPT."
61+
exit 1
62+
fi
63+
REGIONS=($1)
64+
fi
65+
66+
echo "Starting publishing layers for regions: $REGIONS"
67+
68+
69+
publish_layer() {
70+
region=$1
71+
layer_name="dd-trace-java"
72+
aws_version_key=$3
73+
layer_path=$4
74+
version_nbr=$(aws lambda publish-layer-version --layer-name $layer_name \
75+
--description "Datadog Tracer Lambda Layer for Java" \
76+
--zip-file "fileb://tracer.zip" \
77+
--region $region \
78+
--output json \
79+
| jq -r '.Version')
80+
81+
aws lambda add-layer-version-permission --layer-name $layer_name \
82+
--version-number $version_nbr \
83+
--statement-id "release-$version_nbr" \
84+
--action lambda:GetLayerVersion --principal "*" \
85+
--region $region\
86+
--output json
87+
88+
echo "Published layer for region $region, layer_name $layer_name, layer_version $version_nbr"
89+
}
90+
91+
for region in $REGIONS
92+
do
93+
echo "Starting publishing layer for region $region..."
94+
95+
publish_layer $region $layer_name $aws_version_key
96+
done
97+
98+
99+
echo "Done !"

scripts/publish_test_layer.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
3+
# Unless explicitly stated otherwise all files in this repository are licensed
4+
# under the Apache License Version 2.0.
5+
# This product includes software developed at Datadog (https://www.datadoghq.com/).
6+
# Copyright 2021 Datadog, Inc.
7+
8+
# Publish the datadog python lambda layer across regions, using the AWS CLI
9+
# Usage: publish_layer.sh [region] [layer]
10+
# Specifying the region and layer arg will publish the specified layer to the specified region
11+
set -e
12+
13+
# Makes sure any subprocesses will be terminated with this process
14+
trap "pkill -P $$; exit 1;" INT
15+
16+
if [ -z "$1" ]
17+
then
18+
echo "Usage: publish_test_layer path/to/layerFile.jar [region]"
19+
exit 0
20+
fi
21+
22+
23+
LAYER_FILE=$1
24+
REGION=$2
25+
LAYER_ZIP="tracer.zip"
26+
27+
if [ -z "$REGION" ]
28+
then
29+
REGION="sa-east-1"
30+
fi
31+
32+
echo ""
33+
echo "About to publish a TEST Lambda layer containing $LAYER_FILE to $REGION"
34+
read -r -p "Continue? [y/N] " response
35+
if ! [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]
36+
then
37+
echo "OK, exiting!"
38+
exit 0
39+
fi
40+
41+
rm -rf layer
42+
mkdir -p layer/java/lib
43+
cp "$LAYER_FILE" layer/java/lib/dd-java-agent.jar
44+
cd layer
45+
zip -r ../$LAYER_ZIP java
46+
cd ..
47+
rm -rf layer
48+
49+
echo
50+
echo "Signing layer..."
51+
./scripts/sign_layers.sh sandbox $LAYER_ZIP
52+
53+
AVAILABLE_REGIONS=$(aws ec2 describe-regions --output json | jq -r '.[] | .[] | .RegionName')
54+
55+
echo "Region parameter specified: $REGION"
56+
if [[ ! "$AVAILABLE_REGIONS" == *"$REGION"* ]]; then
57+
echo "Could not find $REGION in available regions: $AVAILABLE_REGIONS"
58+
echo ""
59+
echo "EXITING SCRIPT."
60+
exit 1
61+
fi
62+
63+
publish_layer() {
64+
region=$1
65+
layer_name="dd-trace-java-test"
66+
aws_version_key=$2
67+
version_nbr=$(aws lambda publish-layer-version --layer-name $layer_name \
68+
--description "Datadog Tracer Lambda Layer for Java" \
69+
--zip-file "fileb://$LAYER_ZIP" \
70+
--region "$region" \
71+
--output json \
72+
| jq -r '.Version')
73+
74+
aws lambda add-layer-version-permission --layer-name $layer_name \
75+
--version-number $version_nbr \
76+
--statement-id "release-$version_nbr" \
77+
--action lambda:GetLayerVersion --principal "*" \
78+
--region $region\
79+
--output json
80+
81+
echo "Published layer for region $region, layer_name $layer_name, layer_version $version_nbr"
82+
}
83+
84+
echo "Starting publishing layer for region $REGION..."
85+
86+
publish_layer $REGION "$aws_version_key"
87+
88+
echo "Done !"

scripts/sign_layers.sh

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/bin/bash
2+
3+
# Unless explicitly stated otherwise all files in this repository are licensed
4+
# under the Apache License Version 2.0.
5+
# This product includes software developed at Datadog (https://www.datadoghq.com/).
6+
# Copyright 2019 Datadog, Inc.
7+
8+
set -e
9+
10+
SIGNING_PROFILE_NAME="DatadogLambdaSigningProfile"
11+
12+
# Check account parameter
13+
VALID_ACCOUNTS=("sandbox" "prod")
14+
if [ -z "$1" ]; then
15+
echo "ERROR: You must pass an account parameter to sign the layers"
16+
exit 1
17+
fi
18+
if [[ ! "${VALID_ACCOUNTS[@]}" =~ $1 ]]; then
19+
echo "ERROR: The account parameter was invalid. Please choose sandbox or prod."
20+
exit 1
21+
fi
22+
if [ "$1" = "sandbox" ]; then
23+
REGION="sa-east-1"
24+
S3_BUCKET_NAME="dd-lambda-signing-bucket-sandbox"
25+
fi
26+
if [ "$1" = "prod" ]; then
27+
REGION="us-east-1"
28+
S3_BUCKET_NAME="dd-lambda-signing-bucket"
29+
fi
30+
31+
if [ -z "$2" ]
32+
then
33+
echo "Usage: ./sign_layers (sandbox|prod) layer_file.zip"
34+
exit 1
35+
fi
36+
37+
LAYER_FILE=$2
38+
39+
echo
40+
echo "${LAYER_FILE}"
41+
echo "-------------------------"
42+
43+
# Upload the layer to S3 for signing
44+
echo "Uploading layer to S3 for signing..."
45+
UUID=$(uuidgen)
46+
S3_UNSIGNED_ZIP_KEY="${UUID}.zip"
47+
S3_UNSIGNED_ZIP_URI="s3://${S3_BUCKET_NAME}/${S3_UNSIGNED_ZIP_KEY}"
48+
aws s3 cp $LAYER_FILE $S3_UNSIGNED_ZIP_URI
49+
50+
# Start a signing job
51+
echo "Starting the signing job..."
52+
SIGNING_JOB_ID=$(aws signer start-signing-job \
53+
--source "s3={bucketName=${S3_BUCKET_NAME},key=${S3_UNSIGNED_ZIP_KEY},version=null}" \
54+
--destination "s3={bucketName=${S3_BUCKET_NAME}}" \
55+
--profile-name $SIGNING_PROFILE_NAME \
56+
--region $REGION \
57+
--output json \
58+
| jq -r '.jobId'\
59+
)
60+
61+
# Wait for the signing job to complete
62+
echo "Waiting for the signing job to complete..."
63+
SECONDS_WAITED_SO_FAR=0
64+
while :
65+
do
66+
sleep 3
67+
SECONDS_WAITED_SO_FAR=$((SECONDS_WAITED_SO_FAR + 3))
68+
69+
SIGNING_JOB_DESCRIPTION=$(aws signer describe-signing-job \
70+
--job-id $SIGNING_JOB_ID \
71+
--region $REGION \
72+
--output json
73+
)
74+
SIGNING_JOB_STATUS=$(echo $SIGNING_JOB_DESCRIPTION | jq -r '.status')
75+
SIGNING_JOB_STATUS_REASON=$(echo $SIGNING_JOB_DESCRIPTION | jq -r '.statusReason')
76+
77+
if [ $SIGNING_JOB_STATUS = "Succeeded" ]; then
78+
echo "Signing job succeeded!"
79+
break
80+
fi
81+
82+
if [ $SIGNING_JOB_STATUS = "Failed" ]; then
83+
echo "ERROR: Signing job failed"
84+
echo $SIGNING_JOB_STATUS_REASON
85+
exit 1
86+
fi
87+
88+
if [ $SECONDS_WAITED_SO_FAR -ge 60 ]; then
89+
echo "ERROR: Timed out waiting for the signing job to complete"
90+
exit 1
91+
fi
92+
93+
echo "Signing job still in progress..."
94+
done
95+
96+
# Download the signed ZIP, overwriting the original ZIP
97+
echo "Replacing the local layer with the signed layer from S3..."
98+
S3_SIGNED_ZIP_KEY="${SIGNING_JOB_ID}.zip"
99+
S3_SIGNED_ZIP_URI="s3://${S3_BUCKET_NAME}/${S3_SIGNED_ZIP_KEY}"
100+
aws s3 cp $S3_SIGNED_ZIP_URI $LAYER_FILE
101+
102+
# Delete the signed and unsigned ZIPs in S3
103+
echo "Cleaning up the S3 bucket..."
104+
aws s3api delete-object --bucket $S3_BUCKET_NAME --key $S3_UNSIGNED_ZIP_KEY
105+
aws s3api delete-object --bucket $S3_BUCKET_NAME --key $S3_SIGNED_ZIP_KEY
106+
107+
echo
108+
echo "Successfully signed the layer!"

0 commit comments

Comments
 (0)