Skip to content

Commit 83ae371

Browse files
committed
chore: added load test scripts
1 parent 68c22a3 commit 83ae371

File tree

15 files changed

+401
-688
lines changed

15 files changed

+401
-688
lines changed

packages/aws-lambda/lambdas/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Manually create the lambda function in the target region. The name needs to matc
1212
Building The Lambda Zip Files
1313
-----------------------------
1414

15+
Add `.dont-add-npm-instana` file to the target folder to avoid adding the Instana npm packages and to use the layer instead.
16+
1517
Run `bin/create-zip.sh <lambda-folder-name>` to create deployment zip files for one of Lambda functions in this folder. The resulting zip files can be found in the `zip` subfolder. They can be uploaded to AWS as Lambda functions. There is also a script to deploy the resulting zip file via the `aws` command line tool.
1618

1719
The environment variable `BUILD_LAMBDAS_WITH` controls how the Lambda zip files are being built:
@@ -26,11 +28,11 @@ Deploying Lambda Zip Files
2628

2729
Before you deploy zip files, you need to actually build them, see above.
2830

29-
Use `bin/deploy-zip.sh <lambda-folder-name>` to deploy a Lambda zip files. They will be deployed to region `us-east-2` by default. You can repeat that step as often as you like if the Lambda code has changed or you want to deploy zip files with a more recent npm package/local package/Lambda layer.
31+
Use `bin/deploy-zip.sh <zip-file>` to deploy a Lambda zip files. They will be deployed to region `us-east-2` by default. You can repeat that step as often as you like if the Lambda code has changed or you want to deploy zip files with a more recent npm package/local package/Lambda layer.
3032

3133
If you have built the zip files with `BUILD_LAMBDAS_WITH=layer`, the script will try to add the Lambda layer "instana-nodejs" to the deployed Lambda function. The script will try to figure out the latest version of the Instana Node.js Lambda layer. Alternatively, you can also use `LAYER_VERSION` and `LAYER_ARN` to specifiy which layer you want to have added. Checkout the latest layers here: https://www.ibm.com/docs/en/instana-observability/current?topic=lambda-aws-native-tracing-nodejs
3234

33-
E.g. run something like `LAYER_VERSION=167 LAYER_ARN=arn:aws:lambda:ap-southeast-1:767398002385:layer:instana-nodejs:167 bin/deploy-zip.sh`.
35+
E.g. run something like `LAYER_VERSION=167 LAYER_ARN=arn:aws:lambda:ap-southeast-1:767398002385:layer:instana-nodejs:167 bin/deploy-zip.sh <zip-file>`.
3436

3537
Note that if you have use `BUILD_LAMBDAS_WITH=npm` or `BUILD_LAMBDAS_WITH=local` and the function already has the Instana Lambda layer, the deploy script will try to remove it and revert the handler back to `index.handler`.
3638

packages/aws-lambda/lambdas/bin/create-zip-util

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function createZip {
22
name=${PWD##*/}
3-
echo "creating $name.zip"
3+
echo "create-zip-util: $name.zip"
44
rm -f $name.zip
55
mkdir -p ../zip
66
rm -f ../zip/$name.zip

packages/aws-lambda/lambdas/bin/create-zip.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ echo
3737
echo Building all local tar.gz files.
3838

3939
if [[ -z "${BUILD_LAMBDAS_WITH-}" ]]; then
40-
echo "Environmant variable BUILD_LAMBDAS_WITH has not been provided, assuming \"npm\" (build with latest npm package)."
40+
echo "Environment variable BUILD_LAMBDAS_WITH has not been provided, assuming \"npm\" (build with latest npm package)."
4141
fi
4242

4343
# We will want to install/uninstall local npm package tar files, which means we need to build them first. Note: Even for
Lines changed: 172 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
#!/usr/bin/env bash
2-
3-
#######################################
4-
# (c) Copyright IBM Corp. 2021
5-
# (c) Copyright Instana Inc. and contributors 2019
6-
#######################################
7-
8-
set -exEo pipefail
9-
10-
if [[ -z "${1-}" ]]; then
11-
echo "Usage $0 <lambda-folder-name>"
12-
echo
13-
echo "The mandatory argument <lambda-folder-name> is missing."
14-
exit 1
15-
fi
16-
17-
cd `dirname $BASH_SOURCE`/../zip
18-
191
command -v aws >/dev/null 2>&1 || {
202
cat <<EOF >&2
213
The AWS command line tool needs to be installed but it isn't. See https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html or https://docs.aws.amazon.com/cli/latest/userguide/install-macos.html etc. for instructions.
@@ -41,112 +23,189 @@ EOF
4123
exit 1
4224
}
4325

44-
REGION=us-east-2
26+
INSTANA_ENDPOINT_URL=$INSTANA_ENDPOINT_URL
27+
INSTANA_AGENT_KEY=$INSTANA_AGENT_KEY
28+
REGION=$REGION
29+
TIMEOUT=$TIMEOUT
30+
MEMORY_SIZE=$MEMORY_SIZE
31+
HANDLER=$HANDLER
32+
FUNCTION_NAME_PREFIX=$FUNCTION_NAME_PREFIX
33+
lambda_zip_file=$1
34+
ROLE_ARN=$ROLE_ARN
35+
FUNCTION_URL=$FUNCTION_URL
36+
37+
if [[ -z $REGION ]]; then
38+
REGION=us-east-1
39+
fi
4540

46-
function deploy_zip {
47-
lambda_zip_file=$1
41+
if [[ -z $TIMEOUT ]]; then
42+
TIMEOUT=3
43+
fi
4844

49-
if [[ ! -e $lambda_zip_file ]]; then
50-
echo "Zip file $lambda_zip_file does not exist, terminating."
51-
exit 1
52-
fi
45+
if [[ -z $ROLE_ARN ]]; then
46+
ROLE_ARN=arn:aws:iam::767398002385:role/service-role/team-nodejs-lambda-role
47+
fi
48+
49+
if [[ -z $FUNCTION_NAME_PREFIX ]]; then
50+
FUNCTION_NAME_PREFIX=teamnodejstracer-
51+
fi
52+
53+
if [[ -z $HANDLER ]]; then
54+
HANDLER=index.handler
55+
fi
56+
57+
if [[ -z $FUNCTION_URL ]]; then
58+
FUNCTION_URL=false
59+
fi
60+
61+
if [[ -z $MEMORY_SIZE ]]; then
62+
MEMORY_SIZE=256
63+
fi
64+
65+
if [[ -z $LAYER_ARN ]]; then
66+
LAYER_INFO=$( curl https://lambda-layers.instana.io/instana-nodejs?region=$REGION 2> /dev/null )
67+
LAYER_VERSION=$(echo $LAYER_INFO | jq .version)
68+
LAYER_ARN=$(echo $LAYER_INFO | jq .arn)
69+
# remove surrounding quotes from ARN as it trips up the aws lambda update-function-configuration command
70+
LAYER_ARN=${LAYER_ARN//\"/}
71+
fi
72+
73+
if [[ ! -e $lambda_zip_file ]]; then
74+
echo "Zip file $lambda_zip_file does not exist, terminating. Usage: ./deploy-zip.sh <zip-file>"
75+
exit 1
76+
fi
77+
78+
set +e
79+
needs_layer=0
80+
unzip -l $lambda_zip_file | grep node_modules/@instana/aws-lambda/package.json > /dev/null
81+
needs_layer=$?
82+
set -e
5383

54-
echo
55-
echo "Found zip file: $lambda_zip_file"
84+
if [[ $needs_layer == 1 ]]; then
85+
HANDLER=instana-aws-lambda-auto-wrap.handler
86+
fi
5687

57-
function_name=${lambda_zip_file%.zip}
58-
echo Deploying zip $lambda_zip_file as function $function_name to region $REGION
59-
echo
88+
function_name=${lambda_zip_file%.zip}
89+
function_name=${function_name##*/}
90+
function_name="$FUNCTION_NAME_PREFIX-$function_name"
91+
92+
printf "\n#### Summary ####\n\n"
93+
echo "ZIP FILE: $lambda_zip_file"
94+
echo "FUNCTION NAME: $function_name"
95+
echo "FUNCTION_NAME_PREFIX: $FUNCTION_NAME_PREFIX"
96+
echo "REGION:: $REGION"
97+
echo "FUNCTION URL: $FUNCTION_URL"
98+
echo "HANDLER: $HANDLER"
99+
echo "INSTANA_AGENT_KEY: $INSTANA_AGENT_KEY"
100+
echo "INSTANA_ENDPOINT_URL: $INSTANA_ENDPOINT_URL"
101+
echo "NEEDS LAYER: $needs_layer (yes: 1, no: 0)"
102+
echo "LAYER_ARN: $LAYER_ARN"
103+
echo "ROLE_ARN: $ROLE_ARN"
104+
printf "####\n\n"
105+
106+
if [[ -z $NO_PROMPT ]]; then
107+
while true; do
108+
read -p "Do you wish to continue (yes or no)? " yn
109+
case $yn in
110+
[Yy]* ) echo "Let's go!"; break;;
111+
[Nn]* ) exit 1;;
112+
* ) echo "Please answer yes or no.";;
113+
esac
114+
done
115+
fi
60116

61-
AWS_PAGER="" aws --region $REGION lambda update-function-code \
117+
echo
118+
echo "Found zip file: $lambda_zip_file"
119+
echo Deploying zip $lambda_zip_file as function $function_name to region $REGION
120+
echo
121+
122+
echo "Checking if function exists..."
123+
124+
if aws lambda get-function --function-name $function_name --region $REGION > /dev/null 2>&1; then
125+
echo "Function exists. Updating code..."
126+
AWS_PAGER="" aws lambda update-function-code \
127+
--function-name $function_name \
128+
--zip-file fileb://$lambda_zip_file \
129+
--region $REGION
130+
else
131+
echo "Function does not exist. Creating function..."
132+
AWS_PAGER="" aws lambda create-function \
62133
--function-name $function_name \
63-
--zip-file fileb://$lambda_zip_file
64-
65-
set +e
66-
needs_layer=0
67-
unzip -l $lambda_zip_file | grep .dont-add-instana > /dev/null
68-
if [[ $? == 1 ]]; then
69-
unzip -l $lambda_zip_file | grep node_modules/@instana/aws-lambda/package.json > /dev/null
70-
needs_layer=$?
134+
--runtime nodejs20.x \
135+
--role $ROLE_ARN \
136+
--handler $HANDLER \
137+
--memory-size $MEMORY_SIZE \
138+
--zip-file fileb://$lambda_zip_file \
139+
--region $REGION \
140+
--timeout $TIMEOUT \
141+
--environment "Variables={INSTANA_ENDPOINT_URL=$INSTANA_ENDPOINT_URL,INSTANA_AGENT_KEY=$INSTANA_AGENT_KEY}"
142+
fi
143+
144+
if [[ $FUNCTION_URL == true ]]; then
145+
url_config=$(aws lambda get-function-url-config --function-name $function_name --region $REGION || true)
146+
147+
if echo "$url_config" | grep -q "FunctionUrl"; then
148+
echo "Function URL already exists for this Lambda function. Skipping creation."
149+
echo $url_config
150+
else
151+
echo "Creating Function URL for the function..."
152+
aws lambda create-function-url-config \
153+
--function-name $function_name \
154+
--auth-type NONE \
155+
--region $REGION
156+
157+
aws lambda add-permission \
158+
--function-name $function_name \
159+
--principal "*" \
160+
--statement-id "AllowPublicInvoke" \
161+
--action "lambda:InvokeFunctionUrl" \
162+
--region $REGION \
163+
--function-url-auth-type NONE
164+
165+
url_config=$(aws lambda get-function-url-config --function-name $function_name --region $REGION 2>&1)
166+
echo $url_config
71167
fi
72-
set -e
73-
74-
echo
75-
if [[ $needs_layer == 0 ]]; then
76-
echo "The zip file $lambda_zip_file seems to contain the package @instana/aws-lambda (or a .dont-add-instana marker file), so I won't add the Lambda layer to it. I'll check if it currently has a layer that needs to be removed."
77-
78-
current_layers=$(AWS_PAGER="" aws --region $REGION lambda get-function-configuration \
79-
--function-name $function_name \
80-
--output json \
81-
| jq ".Layers")
82-
83-
if [[ "$current_layers" =~ ":instana-nodejs:" ]]; then
84-
echo "This lambda function definition currently has the Instana layer configured, removing it now. I'll also set the standard handler index.handler (just in in case the auto-wrap handler had been configured previously)."
85-
AWS_PAGER="" aws --region $REGION lambda update-function-configuration \
86-
--function-name $function_name \
87-
--layers [] \
88-
--handler index.handler
89-
else
90-
echo This lambda function definition does not use the Instana layer, doing nothing.
91-
fi
168+
fi
92169

170+
echo
171+
if [[ $needs_layer == 0 ]]; then
172+
echo "The zip file $lambda_zip_file seems to contain the package @instana/aws-lambda, so I won't add the Lambda layer to it. I'll check if it currently has a layer that needs to be removed."
173+
174+
current_layers=$(AWS_PAGER="" aws --region $REGION lambda get-function-configuration \
175+
--function-name $function_name \
176+
--output json \
177+
| jq ".Layers")
178+
179+
if [[ "$current_layers" =~ ":instana-nodejs:" ]]; then
180+
echo "This lambda function definition currently has the Instana layer configured, removing it now. I'll also set the standard handler index.handler (just in in case the auto-wrap handler had been configured previously)."
181+
AWS_PAGER="" aws --region $REGION lambda update-function-configuration \
182+
--function-name $function_name \
183+
--layers [] \
184+
--handler index.handler
93185
else
94-
echo "It appears $lambda_zip_file does not contain package @instana/aws-lambda, so I'll add the \"instana-nodejs\" Lambda layer to the function."
95-
96-
if [[ -z $LAYER_VERSION || -z $LAYER_ARN ]]; then
97-
echo "No layer ARN and version specified, will ask for the latest Instana Node.js layer..."
98-
LAYER_INFO=$( curl https://lambda-layers.instana.io/instana-nodejs?region=us-east-2 2> /dev/null )
99-
100-
echo layer info: $LAYER_INFO
101-
LAYER_VERSION=$(echo $LAYER_INFO | jq .version)
102-
LAYER_ARN=$(echo $LAYER_INFO | jq .arn)
103-
104-
# remove surrounding quotes from ARN as it trips up the aws lambda update-function-configuration command
105-
LAYER_ARN=${LAYER_ARN//\"/}
106-
fi
107-
108-
echo Using layer version: $LAYER_VERSION
109-
echo Using layer ARN: $LAYER_ARN
110-
111-
if [[ -z $LAYER_VERSION || -z $LAYER_ARN || $LAYER_VERSION = null || $LAYER_ARN = null ]]; then
112-
echo "I just found out that I'm supposed to add the Instana layer to the function I have just deployed. But I could not find out which LAYER_VERSION or LAYER_ARN to use, so I do not know which layer version I should deploy or what the ARN of that layer version is. The lambda zip file I have just deployed will probably be in a broken state now. Please fix this manually."
113-
echo
114-
echo "For your convenience, here are the commands to figure out the latest layer:"
115-
echo " aws --region $REGION lambda list-layer-versions --layer-name instana-nodejs"
116-
echo "If this ^ gives you a NextToken, ask again with:"
117-
echo " aws --region $REGION lambda list-layer-versions --layer-name instana-nodejs --starting-token"
118-
echo "until you have seen all versions."
119-
echo Aborting.
120-
exit 1
121-
fi
122-
123-
current_layers=$(AWS_PAGER="" aws --region $REGION lambda get-function-configuration \
124-
--function-name $function_name \
125-
--output json \
126-
| jq ".Layers")
127-
128-
if [[ "$current_layers" =~ "instana-nodejs" ]]; then
129-
if [[ "$current_layers" =~ ":instana-nodejs:$LAYER_VERSION" ]]; then
130-
echo This lambda function definition already has the specified version of the Instana layer, doing nothing.
131-
else
132-
echo "This lambda function definition already has the Instana layer configured but uses a version ($current_layers) that is different from the one you specified ($LAYER_VERSION). I'll try to replace this with the specified layer. I'll also set the auto-wrap handler."
133-
AWS_PAGER="" aws --region $REGION lambda update-function-configuration \
134-
--function-name $function_name \
135-
--layers $LAYER_ARN \
136-
--handler instana-aws-lambda-auto-wrap.handler
137-
fi
138-
else
139-
echo "This lambda function definition currently has no Instana layer at all, adding it now. I'll also set the auto-wrap handler."
140-
AWS_PAGER="" aws --region $REGION lambda update-function-configuration \
141-
--function-name $function_name \
142-
--layers $LAYER_ARN \
143-
--handler instana-aws-lambda-auto-wrap.handler
144-
fi
186+
echo This lambda function definition does not use the Instana layer, doing nothing.
145187
fi
146-
}
147188

148-
echo "Deploying $1.zip"
149-
deploy_zip $1.zip
189+
else
190+
echo "It appears $lambda_zip_file does not contain package @instana/aws-lambda, so I'll add the \"instana-nodejs\" Lambda layer to the function."
191+
192+
# Current layers of the target function!
193+
current_layers=$(AWS_PAGER="" aws --region $REGION lambda get-function-configuration \
194+
--function-name $function_name \
195+
--output json \
196+
| jq ".Layers")
197+
198+
if [[ "$current_layers" =~ $LAYER_ARN ]]; then
199+
echo This lambda function definition already has the specified version of the Instana layer, doing nothing.
200+
else
201+
echo "This lambda function definition currently has no Instana layer at all, adding it now. I'll also set the auto-wrap handler."
202+
203+
AWS_PAGER="" aws --region $REGION lambda update-function-configuration \
204+
--function-name $function_name \
205+
--layers $LAYER_ARN \
206+
--handler instana-aws-lambda-auto-wrap.handler
207+
fi
208+
fi
150209

151210
echo
152211
echo Done.

packages/aws-lambda/lambdas/many-spans/.dont-add-instana

Whitespace-only changes.

packages/aws-lambda/lambdas/many-spans/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const delay = ms => {
1313
return new Promise(resolve => setTimeout(resolve, ms));
1414
};
1515

16-
const DELAY = process.env.DELAY || 1000;
17-
const ITERATIONS = process.env.ITERATIONS || 10;
16+
const DELAY = process.env.DELAY || 100;
17+
const ITERATIONS = process.env.ITERATIONS || 100;
1818

1919
// exports.handler = instana.wrap(async () => {
2020
// console.log('Running handler.');

0 commit comments

Comments
 (0)