Skip to content

Commit de44cb8

Browse files
authored
Fix benchmark tf module with python function (#414)
1 parent 421496b commit de44cb8

File tree

18 files changed

+893
-65
lines changed

18 files changed

+893
-65
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ dist/
3232
*arn-file.md
3333

3434
tf/*.zip
35+
36+
build

apmproxy/apmserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ func (c *Client) FlushAPMData(ctx context.Context) {
9696
}
9797

9898
// Flush lambda data
99+
c.logger.Debug("Flush in progress - Processing lambda data")
99100
for {
100101
select {
101102
case apmData := <-c.LambdaDataChannel:
102-
c.logger.Debug("Flush in progress - Processing lambda data")
103103
if err := c.forwardLambdaData(ctx, apmData); err != nil {
104104
c.logger.Errorf("Error sending to APM server, skipping: %v", err)
105105
}

testing/Makefile

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ MACHINE_TYPE?=t2.medium
44
STACK_VERSION?=latest
55
LOAD_DURATION?=10
66
LOAD_ARRIVAL_RATE?=50
7-
LAMBDA_RUNTIME?=python3.8
7+
LAMBDA_RUNTIME?=python3.9
88
LAMBDA_TIMEOUT?=15
99
LAMBDA_MEMORY_SIZE?=128
1010
CUSTOM_LAMBDA_EXTENSION_ARN?=
1111
LAMBDA_RUNTIME_NORM=$(subst '.','_',$(LAMBDA_RUNTIME))
12-
13-
# TODO: @lahsivjar Add automation for terraform fmt and docs
12+
LAMBDA_APM_AWS_EXTENSION_PATH=$(shell echo $(wildcard ../dist/*-linux-amd64.zip) | cut -d' ' -f1)
13+
TERRAFORM_DOCS=../build/terraform-docs
14+
TERRAFORM_DIRS=$(shell find tf-modules -type d -mindepth 1 -maxdepth 1)
1415

1516
build/$(LAMBDA_RUNTIME_NORM).zip:
1617
@mkdir -p build
@@ -19,7 +20,7 @@ build/$(LAMBDA_RUNTIME_NORM).zip:
1920
.PHONY: clean
2021
clean:
2122
@rm -rf build/
22-
@rm -rf functions/python3.8/package/
23+
@rm -rf functions/python3.9/package/
2324
@rm -rf functions/go1.x/main
2425

2526
.PHONY: bench
@@ -30,10 +31,29 @@ bench: build/$(LAMBDA_RUNTIME_NORM).zip --run-tf
3031
destroy: TF_CMD=destroy
3132
destroy: --run-tf
3233

34+
.PHONY: fmt
35+
fmt:
36+
@terraform fmt -write $(TERRAFORM_DIRS)
37+
38+
.PHONY: lint
39+
lint:
40+
@terraform fmt -write=false $(TERRAFORM_DIRS)
41+
3342
.PHONY: terraform-init
3443
terraform-init:
3544
@cd benchmarking && terraform init
3645

46+
$(TERRAFORM_DOCS): ../tools/go.mod
47+
@go build -o $@ -modfile=$< github.com/terraform-docs/terraform-docs
48+
49+
.PHONY: docs
50+
docs: $(TERRAFORM_DOCS) $(addsuffix /README.md,$(TERRAFORM_DIRS))
51+
52+
%/README.md: force
53+
$(TERRAFORM_DOCS) markdown --hide-empty --header-from header.md --output-file=README.md --output-mode replace $(subst README.md,,$@)
54+
55+
force: ;
56+
3757
.PHONY: --run-tf
3858
--run-tf:
3959
@cd benchmarking && terraform $(TF_CMD) -auto-approve \
@@ -46,4 +66,6 @@ terraform-init:
4666
-var 'lambda_timeout=$(LAMBDA_TIMEOUT)' \
4767
-var 'lambda_memory_size=$(LAMBDA_MEMORY_SIZE)' \
4868
-var 'stack_version=$(STACK_VERSION)' \
49-
-var 'custom_lambda_extension_arn=$(CUSTOM_LAMBDA_EXTENSION_ARN)'
69+
-var 'custom_lambda_extension_arn=$(CUSTOM_LAMBDA_EXTENSION_ARN)' \
70+
-var 'lambda_apm_aws_extension_path=../$(LAMBDA_APM_AWS_EXTENSION_PATH)'
71+

testing/benchmarking/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ No resources.
3535
| <a name="input_elasticsearch_zone_count"></a> [elasticsearch\_zone\_count](#input\_elasticsearch\_zone\_count) | Optional Elasticsearch zone count | `number` | `2` | no |
3636
| <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 |
3737
| <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 |
38-
| <a name="input_lambda_runtime"></a> [lambda\_runtime](#input\_lambda\_runtime) | The language-specific lambda runtime | `string` | `"python3.8"` | no |
38+
| <a name="input_lambda_runtime"></a> [lambda\_runtime](#input\_lambda\_runtime) | The language-specific lambda runtime | `string` | `"python3.9"` | no |
3939
| <a name="input_lambda_timeout"></a> [lambda\_timeout](#input\_lambda\_timeout) | Timeout of the lambda function in seconds | `number` | `15` | no |
4040
| <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 |
4141
| <a name="input_load_duration"></a> [load\_duration](#input\_load\_duration) | Duration over which to generate new virtual users | `number` | `10` | no |
@@ -46,4 +46,4 @@ No resources.
4646
## Outputs
4747

4848
No outputs.
49-
<!-- END_TF_DOCS -->
49+
<!-- END_TF_DOCS -->

testing/benchmarking/main.tf

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ terraform {
33
required_providers {
44
aws = {
55
source = "hashicorp/aws"
6-
version = "~> 4.0"
6+
version = "~> 5.18.1"
77
}
88
null = {
99
source = "hashicorp/null"
@@ -21,9 +21,19 @@ locals {
2121
name_from_runtime = replace(var.lambda_runtime, ".", "_")
2222
lambda_function_zip = "../build/${local.name_from_runtime}.zip"
2323
lambda_function_name = "${var.resource_prefix}_${local.name_from_runtime}_apm_aws_lambda"
24-
runtimeToHandler = {
25-
"python3.8" = "main.handler"
26-
"go1.x" = "main"
24+
runtimeVars = {
25+
"python3.9" = {
26+
"handler" = "main.handler"
27+
"layers" = ["arn:aws:lambda:${var.aws_region}:267093732750:layer:elastic-apm-python-ver-6-18-0:1"]
28+
"envvars" = {
29+
"AWS_LAMBDA_EXEC_WRAPPER" = "/opt/python/bin/elasticapm-lambda"
30+
}
31+
}
32+
"go1.x" = {
33+
"handler" = "main"
34+
"layers" = []
35+
"envvars" = {}
36+
}
2737
}
2838
}
2939

@@ -65,14 +75,16 @@ module "lambda_deployment" {
6575

6676
resource_prefix = var.resource_prefix
6777

68-
apm_aws_extension_path = "../../bin/extension.zip"
78+
apm_aws_extension_path = var.lambda_apm_aws_extension_path
6979

7080
lambda_runtime = var.lambda_runtime
81+
lambda_memory_size = var.lambda_memory_size
7182
lambda_function_zip = local.lambda_function_zip
7283
lambda_function_name = local.lambda_function_name
73-
lambda_handler = local.runtimeToHandler[var.lambda_runtime]
7484
lambda_invoke_path = local.load_req_path
75-
lambda_memory_size = var.lambda_memory_size
85+
additional_lambda_layers = local.runtimeVars[var.lambda_runtime]["layers"]
86+
lambda_handler = local.runtimeVars[var.lambda_runtime]["handler"]
87+
environment_variables = local.runtimeVars[var.lambda_runtime]["envvars"]
7688
custom_lambda_extension_arn = var.custom_lambda_extension_arn
7789

7890
apm_server_url = module.ec_deployment.apm_url

testing/benchmarking/variables.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ variable "load_arrival_rate" {
3030
variable "lambda_runtime" {
3131
type = string
3232
description = "The language-specific lambda runtime"
33-
default = "python3.8"
33+
default = "python3.9"
3434
}
3535

3636
variable "lambda_timeout" {
@@ -45,6 +45,11 @@ variable "lambda_memory_size" {
4545
default = 128
4646
}
4747

48+
variable "lambda_apm_aws_extension_path" {
49+
type = string
50+
description = "Extension path where apm-aws-lambda extension zip is created"
51+
}
52+
4853
variable "custom_lambda_extension_arn" {
4954
type = string
5055
description = "Specific lambda extension to use, will use the latest build if not specified"

testing/functions/python3.8/requirements.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

testing/functions/python3.8/build.sh renamed to testing/functions/python3.9/build.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@
55

66
set -e
77

8-
pip install -t ./package -r requirements.txt
9-
(cd package && zip -r ../../../build/python3_8.zip .)
10-
zip -g ../../build/python3_8.zip main.py
8+
zip -g ../../build/python3_9.zip main.py

testing/functions/python3.8/main.py renamed to testing/functions/python3.9/main.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import json
2-
from elasticapm import capture_serverless
32

43
coldstart = "true"
5-
@capture_serverless()
64
def handler(event, context):
75
global coldstart
86
print("Example function log", context.aws_request_id)
Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1+
<!-- BEGIN_TF_DOCS -->
12
## Terraform module for deploying artillery
23

34
The module is used to deploy and run [artillery](https://www.artillery.io/) in
45
cloud for benchmarking lambda functions.
56

6-
<!-- BEGIN_TF_DOCS -->
7-
## Requirements
8-
9-
| Name | Version |
10-
|------|---------|
11-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 4.0 |
12-
| <a name="requirement_null"></a> [null](#requirement\_null) | >=3.1.1 |
13-
147
## Providers
158

169
| Name | Version |
1710
|------|---------|
18-
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.37.0 |
19-
| <a name="provider_null"></a> [null](#provider\_null) | 3.2.0 |
20-
| <a name="provider_tls"></a> [tls](#provider\_tls) | 4.0.4 |
21-
22-
## Modules
23-
24-
No modules.
11+
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
12+
| <a name="provider_null"></a> [null](#provider\_null) | n/a |
13+
| <a name="provider_tls"></a> [tls](#provider\_tls) | n/a |
2514

2615
## Resources
2716

@@ -43,15 +32,10 @@ No modules.
4332

4433
| Name | Description | Type | Default | Required |
4534
|------|-------------|------|---------|:--------:|
46-
| <a name="input_aws_region"></a> [aws\_region](#input\_aws\_region) | AWS region to deploy lambda function | `string` | `"us-west-2"` | no |
4735
| <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 |
4836
| <a name="input_load_base_url"></a> [load\_base\_url](#input\_load\_base\_url) | Base URL for load generation | `string` | n/a | yes |
4937
| <a name="input_load_duration"></a> [load\_duration](#input\_load\_duration) | Duration over which to generate new virtual users | `number` | `10` | no |
5038
| <a name="input_load_req_path"></a> [load\_req\_path](#input\_load\_req\_path) | Request path for load generation | `string` | n/a | yes |
5139
| <a name="input_machine_type"></a> [machine\_type](#input\_machine\_type) | Machine type for artillery nodes | `string` | `"t2.medium"` | no |
5240
| <a name="input_resource_prefix"></a> [resource\_prefix](#input\_resource\_prefix) | Prefix to add to all created resource | `string` | n/a | yes |
53-
54-
## Outputs
55-
56-
No outputs.
57-
<!-- END_TF_DOCS -->
41+
<!-- END_TF_DOCS -->

0 commit comments

Comments
 (0)