Skip to content

Commit

Permalink
chore: added load test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
kirrg001 committed Nov 19, 2024
1 parent 68c22a3 commit 83ae371
Show file tree
Hide file tree
Showing 15 changed files with 401 additions and 688 deletions.
6 changes: 4 additions & 2 deletions packages/aws-lambda/lambdas/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Manually create the lambda function in the target region. The name needs to matc
Building The Lambda Zip Files
-----------------------------

Add `.dont-add-npm-instana` file to the target folder to avoid adding the Instana npm packages and to use the layer instead.

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.

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

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

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.
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.

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

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`.
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>`.

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`.

Expand Down
2 changes: 1 addition & 1 deletion packages/aws-lambda/lambdas/bin/create-zip-util
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function createZip {
name=${PWD##*/}
echo "creating $name.zip"
echo "create-zip-util: $name.zip"
rm -f $name.zip
mkdir -p ../zip
rm -f ../zip/$name.zip
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-lambda/lambdas/bin/create-zip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ echo
echo Building all local tar.gz files.

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

# We will want to install/uninstall local npm package tar files, which means we need to build them first. Note: Even for
Expand Down
285 changes: 172 additions & 113 deletions packages/aws-lambda/lambdas/bin/deploy-zip.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
#!/usr/bin/env bash

#######################################
# (c) Copyright IBM Corp. 2021
# (c) Copyright Instana Inc. and contributors 2019
#######################################

set -exEo pipefail

if [[ -z "${1-}" ]]; then
echo "Usage $0 <lambda-folder-name>"
echo
echo "The mandatory argument <lambda-folder-name> is missing."
exit 1
fi

cd `dirname $BASH_SOURCE`/../zip

command -v aws >/dev/null 2>&1 || {
cat <<EOF >&2
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.
Expand All @@ -41,112 +23,189 @@ EOF
exit 1
}

REGION=us-east-2
INSTANA_ENDPOINT_URL=$INSTANA_ENDPOINT_URL
INSTANA_AGENT_KEY=$INSTANA_AGENT_KEY
REGION=$REGION
TIMEOUT=$TIMEOUT
MEMORY_SIZE=$MEMORY_SIZE
HANDLER=$HANDLER
FUNCTION_NAME_PREFIX=$FUNCTION_NAME_PREFIX
lambda_zip_file=$1
ROLE_ARN=$ROLE_ARN
FUNCTION_URL=$FUNCTION_URL

if [[ -z $REGION ]]; then
REGION=us-east-1
fi

function deploy_zip {
lambda_zip_file=$1
if [[ -z $TIMEOUT ]]; then
TIMEOUT=3
fi

if [[ ! -e $lambda_zip_file ]]; then
echo "Zip file $lambda_zip_file does not exist, terminating."
exit 1
fi
if [[ -z $ROLE_ARN ]]; then
ROLE_ARN=arn:aws:iam::767398002385:role/service-role/team-nodejs-lambda-role
fi

if [[ -z $FUNCTION_NAME_PREFIX ]]; then
FUNCTION_NAME_PREFIX=teamnodejstracer-
fi

if [[ -z $HANDLER ]]; then
HANDLER=index.handler
fi

if [[ -z $FUNCTION_URL ]]; then
FUNCTION_URL=false
fi

if [[ -z $MEMORY_SIZE ]]; then
MEMORY_SIZE=256
fi

if [[ -z $LAYER_ARN ]]; then
LAYER_INFO=$( curl https://lambda-layers.instana.io/instana-nodejs?region=$REGION 2> /dev/null )
LAYER_VERSION=$(echo $LAYER_INFO | jq .version)
LAYER_ARN=$(echo $LAYER_INFO | jq .arn)
# remove surrounding quotes from ARN as it trips up the aws lambda update-function-configuration command
LAYER_ARN=${LAYER_ARN//\"/}
fi

if [[ ! -e $lambda_zip_file ]]; then
echo "Zip file $lambda_zip_file does not exist, terminating. Usage: ./deploy-zip.sh <zip-file>"
exit 1
fi

set +e
needs_layer=0
unzip -l $lambda_zip_file | grep node_modules/@instana/aws-lambda/package.json > /dev/null
needs_layer=$?
set -e

echo
echo "Found zip file: $lambda_zip_file"
if [[ $needs_layer == 1 ]]; then
HANDLER=instana-aws-lambda-auto-wrap.handler
fi

function_name=${lambda_zip_file%.zip}
echo Deploying zip $lambda_zip_file as function $function_name to region $REGION
echo
function_name=${lambda_zip_file%.zip}
function_name=${function_name##*/}
function_name="$FUNCTION_NAME_PREFIX-$function_name"

printf "\n#### Summary ####\n\n"
echo "ZIP FILE: $lambda_zip_file"
echo "FUNCTION NAME: $function_name"
echo "FUNCTION_NAME_PREFIX: $FUNCTION_NAME_PREFIX"
echo "REGION:: $REGION"
echo "FUNCTION URL: $FUNCTION_URL"
echo "HANDLER: $HANDLER"
echo "INSTANA_AGENT_KEY: $INSTANA_AGENT_KEY"
echo "INSTANA_ENDPOINT_URL: $INSTANA_ENDPOINT_URL"
echo "NEEDS LAYER: $needs_layer (yes: 1, no: 0)"
echo "LAYER_ARN: $LAYER_ARN"
echo "ROLE_ARN: $ROLE_ARN"
printf "####\n\n"

if [[ -z $NO_PROMPT ]]; then
while true; do
read -p "Do you wish to continue (yes or no)? " yn
case $yn in
[Yy]* ) echo "Let's go!"; break;;
[Nn]* ) exit 1;;
* ) echo "Please answer yes or no.";;
esac
done
fi

AWS_PAGER="" aws --region $REGION lambda update-function-code \
echo
echo "Found zip file: $lambda_zip_file"
echo Deploying zip $lambda_zip_file as function $function_name to region $REGION
echo

echo "Checking if function exists..."

if aws lambda get-function --function-name $function_name --region $REGION > /dev/null 2>&1; then
echo "Function exists. Updating code..."
AWS_PAGER="" aws lambda update-function-code \
--function-name $function_name \
--zip-file fileb://$lambda_zip_file \
--region $REGION
else
echo "Function does not exist. Creating function..."
AWS_PAGER="" aws lambda create-function \
--function-name $function_name \
--zip-file fileb://$lambda_zip_file

set +e
needs_layer=0
unzip -l $lambda_zip_file | grep .dont-add-instana > /dev/null
if [[ $? == 1 ]]; then
unzip -l $lambda_zip_file | grep node_modules/@instana/aws-lambda/package.json > /dev/null
needs_layer=$?
--runtime nodejs20.x \
--role $ROLE_ARN \
--handler $HANDLER \
--memory-size $MEMORY_SIZE \
--zip-file fileb://$lambda_zip_file \
--region $REGION \
--timeout $TIMEOUT \
--environment "Variables={INSTANA_ENDPOINT_URL=$INSTANA_ENDPOINT_URL,INSTANA_AGENT_KEY=$INSTANA_AGENT_KEY}"
fi

if [[ $FUNCTION_URL == true ]]; then
url_config=$(aws lambda get-function-url-config --function-name $function_name --region $REGION || true)

if echo "$url_config" | grep -q "FunctionUrl"; then
echo "Function URL already exists for this Lambda function. Skipping creation."
echo $url_config
else
echo "Creating Function URL for the function..."
aws lambda create-function-url-config \
--function-name $function_name \
--auth-type NONE \
--region $REGION

aws lambda add-permission \
--function-name $function_name \
--principal "*" \
--statement-id "AllowPublicInvoke" \
--action "lambda:InvokeFunctionUrl" \
--region $REGION \
--function-url-auth-type NONE

url_config=$(aws lambda get-function-url-config --function-name $function_name --region $REGION 2>&1)
echo $url_config
fi
set -e

echo
if [[ $needs_layer == 0 ]]; then
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."

current_layers=$(AWS_PAGER="" aws --region $REGION lambda get-function-configuration \
--function-name $function_name \
--output json \
| jq ".Layers")

if [[ "$current_layers" =~ ":instana-nodejs:" ]]; then
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)."
AWS_PAGER="" aws --region $REGION lambda update-function-configuration \
--function-name $function_name \
--layers [] \
--handler index.handler
else
echo This lambda function definition does not use the Instana layer, doing nothing.
fi
fi

echo
if [[ $needs_layer == 0 ]]; then
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."

current_layers=$(AWS_PAGER="" aws --region $REGION lambda get-function-configuration \
--function-name $function_name \
--output json \
| jq ".Layers")

if [[ "$current_layers" =~ ":instana-nodejs:" ]]; then
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)."
AWS_PAGER="" aws --region $REGION lambda update-function-configuration \
--function-name $function_name \
--layers [] \
--handler index.handler
else
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."

if [[ -z $LAYER_VERSION || -z $LAYER_ARN ]]; then
echo "No layer ARN and version specified, will ask for the latest Instana Node.js layer..."
LAYER_INFO=$( curl https://lambda-layers.instana.io/instana-nodejs?region=us-east-2 2> /dev/null )

echo layer info: $LAYER_INFO
LAYER_VERSION=$(echo $LAYER_INFO | jq .version)
LAYER_ARN=$(echo $LAYER_INFO | jq .arn)

# remove surrounding quotes from ARN as it trips up the aws lambda update-function-configuration command
LAYER_ARN=${LAYER_ARN//\"/}
fi

echo Using layer version: $LAYER_VERSION
echo Using layer ARN: $LAYER_ARN

if [[ -z $LAYER_VERSION || -z $LAYER_ARN || $LAYER_VERSION = null || $LAYER_ARN = null ]]; then
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."
echo
echo "For your convenience, here are the commands to figure out the latest layer:"
echo " aws --region $REGION lambda list-layer-versions --layer-name instana-nodejs"
echo "If this ^ gives you a NextToken, ask again with:"
echo " aws --region $REGION lambda list-layer-versions --layer-name instana-nodejs --starting-token"
echo "until you have seen all versions."
echo Aborting.
exit 1
fi

current_layers=$(AWS_PAGER="" aws --region $REGION lambda get-function-configuration \
--function-name $function_name \
--output json \
| jq ".Layers")

if [[ "$current_layers" =~ "instana-nodejs" ]]; then
if [[ "$current_layers" =~ ":instana-nodejs:$LAYER_VERSION" ]]; then
echo This lambda function definition already has the specified version of the Instana layer, doing nothing.
else
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."
AWS_PAGER="" aws --region $REGION lambda update-function-configuration \
--function-name $function_name \
--layers $LAYER_ARN \
--handler instana-aws-lambda-auto-wrap.handler
fi
else
echo "This lambda function definition currently has no Instana layer at all, adding it now. I'll also set the auto-wrap handler."
AWS_PAGER="" aws --region $REGION lambda update-function-configuration \
--function-name $function_name \
--layers $LAYER_ARN \
--handler instana-aws-lambda-auto-wrap.handler
fi
echo This lambda function definition does not use the Instana layer, doing nothing.
fi
}

echo "Deploying $1.zip"
deploy_zip $1.zip
else
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."

# Current layers of the target function!
current_layers=$(AWS_PAGER="" aws --region $REGION lambda get-function-configuration \
--function-name $function_name \
--output json \
| jq ".Layers")

if [[ "$current_layers" =~ $LAYER_ARN ]]; then
echo This lambda function definition already has the specified version of the Instana layer, doing nothing.
else
echo "This lambda function definition currently has no Instana layer at all, adding it now. I'll also set the auto-wrap handler."

AWS_PAGER="" aws --region $REGION lambda update-function-configuration \
--function-name $function_name \
--layers $LAYER_ARN \
--handler instana-aws-lambda-auto-wrap.handler
fi
fi

echo
echo Done.
Empty file.
4 changes: 2 additions & 2 deletions packages/aws-lambda/lambdas/many-spans/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const delay = ms => {
return new Promise(resolve => setTimeout(resolve, ms));
};

const DELAY = process.env.DELAY || 1000;
const ITERATIONS = process.env.ITERATIONS || 10;
const DELAY = process.env.DELAY || 100;
const ITERATIONS = process.env.ITERATIONS || 100;

// exports.handler = instana.wrap(async () => {
// console.log('Running handler.');
Expand Down
Loading

0 comments on commit 83ae371

Please sign in to comment.