Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix benchmark tf module with python function #414

Merged
merged 4 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 apmproxy/apmserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ func (c *Client) FlushAPMData(ctx context.Context) {
}

// Flush lambda data
c.logger.Debug("Flush in progress - Processing lambda data")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[For reviewers] This log line led to a lot of noise during flush. Moved it so that only single entry is logged per flush.

for {
select {
case apmData := <-c.LambdaDataChannel:
c.logger.Debug("Flush in progress - Processing lambda data")
if err := c.forwardLambdaData(ctx, apmData); err != nil {
c.logger.Errorf("Error sending to APM server, skipping: %v", err)
}
Expand Down
9 changes: 6 additions & 3 deletions testing/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ MACHINE_TYPE?=t2.medium
STACK_VERSION?=latest
LOAD_DURATION?=10
LOAD_ARRIVAL_RATE?=50
LAMBDA_RUNTIME?=python3.8
LAMBDA_RUNTIME?=python3.9
LAMBDA_TIMEOUT?=15
LAMBDA_MEMORY_SIZE?=128
CUSTOM_LAMBDA_EXTENSION_ARN?=
LAMBDA_RUNTIME_NORM=$(subst '.','_',$(LAMBDA_RUNTIME))
LAMBDA_APM_AWS_EXTENSION_PATH=$(shell echo $(wildcard ../dist/*-linux-amd64.zip) | cut -d' ' -f1)

# TODO: @lahsivjar Add automation for terraform fmt and docs

Expand All @@ -19,7 +20,7 @@ build/$(LAMBDA_RUNTIME_NORM).zip:
.PHONY: clean
clean:
@rm -rf build/
@rm -rf functions/python3.8/package/
@rm -rf functions/python3.9/package/
@rm -rf functions/go1.x/main

.PHONY: bench
Expand All @@ -46,4 +47,6 @@ terraform-init:
-var 'lambda_timeout=$(LAMBDA_TIMEOUT)' \
-var 'lambda_memory_size=$(LAMBDA_MEMORY_SIZE)' \
-var 'stack_version=$(STACK_VERSION)' \
-var 'custom_lambda_extension_arn=$(CUSTOM_LAMBDA_EXTENSION_ARN)'
-var 'custom_lambda_extension_arn=$(CUSTOM_LAMBDA_EXTENSION_ARN)' \
-var 'lambda_apm_aws_extension_path=../$(LAMBDA_APM_AWS_EXTENSION_PATH)'

4 changes: 2 additions & 2 deletions testing/benchmarking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ No resources.
| <a name="input_elasticsearch_zone_count"></a> [elasticsearch\_zone\_count](#input\_elasticsearch\_zone\_count) | Optional Elasticsearch zone count | `number` | `2` | no |
| <a name="input_ess_region"></a> [ess\_region](#input\_ess\_region) | Optional ESS region where the deployment will be created. Defaults to gcp-us-west2 | `string` | `"gcp-us-west2"` | no |
| <a name="input_lambda_memory_size"></a> [lambda\_memory\_size](#input\_lambda\_memory\_size) | Amount of memory (in MB) the lambda function can use | `number` | `128` | no |
| <a name="input_lambda_runtime"></a> [lambda\_runtime](#input\_lambda\_runtime) | The language-specific lambda runtime | `string` | `"python3.8"` | no |
| <a name="input_lambda_runtime"></a> [lambda\_runtime](#input\_lambda\_runtime) | The language-specific lambda runtime | `string` | `"python3.9"` | no |
| <a name="input_lambda_timeout"></a> [lambda\_timeout](#input\_lambda\_timeout) | Timeout of the lambda function in seconds | `number` | `15` | no |
| <a name="input_load_arrival_rate"></a> [load\_arrival\_rate](#input\_load\_arrival\_rate) | Rate(per second) at which the virtual users are generated | `number` | `50` | no |
| <a name="input_load_duration"></a> [load\_duration](#input\_load\_duration) | Duration over which to generate new virtual users | `number` | `10` | no |
Expand All @@ -46,4 +46,4 @@ No resources.
## Outputs

No outputs.
<!-- END_TF_DOCS -->
<!-- END_TF_DOCS -->
26 changes: 19 additions & 7 deletions testing/benchmarking/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
version = "~> 5.18.1"
}
null = {
source = "hashicorp/null"
Expand All @@ -21,9 +21,19 @@ locals {
name_from_runtime = replace(var.lambda_runtime, ".", "_")
lambda_function_zip = "../build/${local.name_from_runtime}.zip"
lambda_function_name = "${var.resource_prefix}_${local.name_from_runtime}_apm_aws_lambda"
runtimeToHandler = {
"python3.8" = "main.handler"
"go1.x" = "main"
runtimeVars = {
"python3.9" = {
"handler" = "main.handler"
"layers" = ["arn:aws:lambda:${var.aws_region}:267093732750:layer:elastic-apm-python-ver-6-18-0:1"]
"envvars" = {
"AWS_LAMBDA_EXEC_WRAPPER" = "/opt/python/bin/elasticapm-lambda"
}
}
"go1.x" = {
"handler" = "main"
"layers" = []
"envvars" = {}
}
}
}

Expand Down Expand Up @@ -65,14 +75,16 @@ module "lambda_deployment" {

resource_prefix = var.resource_prefix

apm_aws_extension_path = "../../bin/extension.zip"
apm_aws_extension_path = var.lambda_apm_aws_extension_path

lambda_runtime = var.lambda_runtime
lambda_memory_size = var.lambda_memory_size
lambda_function_zip = local.lambda_function_zip
lambda_function_name = local.lambda_function_name
lambda_handler = local.runtimeToHandler[var.lambda_runtime]
lambda_invoke_path = local.load_req_path
lambda_memory_size = var.lambda_memory_size
additional_lambda_layers = local.runtimeVars[var.lambda_runtime]["layers"]
lambda_handler = local.runtimeVars[var.lambda_runtime]["handler"]
environment_variables = local.runtimeVars[var.lambda_runtime]["envvars"]
custom_lambda_extension_arn = var.custom_lambda_extension_arn

apm_server_url = module.ec_deployment.apm_url
Expand Down
7 changes: 6 additions & 1 deletion testing/benchmarking/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ variable "load_arrival_rate" {
variable "lambda_runtime" {
type = string
description = "The language-specific lambda runtime"
default = "python3.8"
default = "python3.9"
}

variable "lambda_timeout" {
Expand All @@ -45,6 +45,11 @@ variable "lambda_memory_size" {
default = 128
}

variable "lambda_apm_aws_extension_path" {
type = string
description = "Extension path where apm-aws-lambda extension zip is created"
}

variable "custom_lambda_extension_arn" {
type = string
description = "Specific lambda extension to use, will use the latest build if not specified"
Expand Down
1 change: 0 additions & 1 deletion testing/functions/python3.8/requirements.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@

set -e

pip install -t ./package -r requirements.txt
(cd package && zip -r ../../../build/python3_8.zip .)
zip -g ../../build/python3_8.zip main.py
zip -g ../../build/python3_9.zip main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import json
from elasticapm import capture_serverless

coldstart = "true"
@capture_serverless()
def handler(event, context):
global coldstart
print("Example function log", context.aws_request_id)
Expand Down
2 changes: 1 addition & 1 deletion testing/tf-modules/lambda_deployment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ No modules.
| <a name="input_lambda_handler"></a> [lambda\_handler](#input\_lambda\_handler) | Entrypoint for the lambda function | `string` | `"main.handler"` | no |
| <a name="input_lambda_invoke_path"></a> [lambda\_invoke\_path](#input\_lambda\_invoke\_path) | Request path to invoke the test lambda function | `string` | `"/test"` | no |
| <a name="input_lambda_memory_size"></a> [lambda\_memory\_size](#input\_lambda\_memory\_size) | Amount of memory (in MB) the lambda function can use | `number` | `128` | no |
| <a name="input_lambda_runtime"></a> [lambda\_runtime](#input\_lambda\_runtime) | The language-specific lambda runtime | `string` | `"python3.8"` | no |
| <a name="input_lambda_runtime"></a> [lambda\_runtime](#input\_lambda\_runtime) | The language-specific lambda runtime | `string` | `"python3.9"` | no |
| <a name="input_lambda_timeout"></a> [lambda\_timeout](#input\_lambda\_timeout) | Timeout of the lambda function in seconds | `number` | `15` | no |
| <a name="input_resource_prefix"></a> [resource\_prefix](#input\_resource\_prefix) | Prefix to add to all created resource | `string` | n/a | yes |

Expand Down
21 changes: 13 additions & 8 deletions testing/tf-modules/lambda_deployment/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,26 @@ resource "aws_lambda_function" "test_fn" {
handler = var.lambda_handler
runtime = var.lambda_runtime
source_code_hash = filebase64sha256(var.lambda_function_zip)
layers = [var.custom_lambda_extension_arn == "" ? aws_lambda_layer_version.extn_layer[0].arn : var.custom_lambda_extension_arn]
timeout = var.lambda_timeout
memory_size = var.lambda_memory_size
timeout = var.lambda_timeout
memory_size = var.lambda_memory_size

depends_on = [
aws_cloudwatch_log_group.cw_log_group,
]
layers = concat(
[var.custom_lambda_extension_arn == "" ? aws_lambda_layer_version.extn_layer[0].arn : var.custom_lambda_extension_arn],
var.additional_lambda_layers,
)

environment {
variables = {
variables = merge({
ELASTIC_APM_LAMBDA_APM_SERVER = var.apm_server_url
ELASTIC_APM_SECRET_TOKEN = var.apm_secret_token
ELASTIC_APM_LAMBDA_CAPTURE_LOGS = "true"
}
ELASTIC_APM_SEND_STRATEGY = "background"
}, var.environment_variables)
}

depends_on = [
aws_cloudwatch_log_group.cw_log_group,
]
}

resource "aws_apigatewayv2_api" "trigger" {
Expand Down
14 changes: 13 additions & 1 deletion testing/tf-modules/lambda_deployment/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ variable "lambda_function_name" {
variable "lambda_runtime" {
type = string
description = "The language-specific lambda runtime"
default = "python3.8"
default = "python3.9"
}

variable "lambda_handler" {
Expand Down Expand Up @@ -54,6 +54,18 @@ variable "custom_lambda_extension_arn" {
default = ""
}

variable "additional_lambda_layers" {
type = list(string)
description = "Additional lambda layer ARNs to add to the lambda function"
default = []
}

variable "environment_variables" {
type = map(string)
description = "Additional environment variables to add to the lambda function"
default = {}
}

variable "apm_server_url" {
type = string
description = "APM Server URL for sending the generated load"
Expand Down
Loading