From 40453763d3d17fe0b1897e76cb250c4fffe8febf Mon Sep 17 00:00:00 2001 From: Emily Stolfo Date: Mon, 25 Oct 2021 12:25:05 +0200 Subject: [PATCH 1/3] Skeleton sam setup for ruby function --- .../tests/functions/ruby/Gemfile | 3 ++ .../functions/ruby/ruby_function_error.rb | 14 ++++++++ .../tests/templates/template.yml | 34 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 apm-lambda-extension/tests/functions/ruby/Gemfile create mode 100644 apm-lambda-extension/tests/functions/ruby/ruby_function_error.rb create mode 100644 apm-lambda-extension/tests/templates/template.yml diff --git a/apm-lambda-extension/tests/functions/ruby/Gemfile b/apm-lambda-extension/tests/functions/ruby/Gemfile new file mode 100644 index 00000000..1e933df9 --- /dev/null +++ b/apm-lambda-extension/tests/functions/ruby/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gem 'elastic-apm' diff --git a/apm-lambda-extension/tests/functions/ruby/ruby_function_error.rb b/apm-lambda-extension/tests/functions/ruby/ruby_function_error.rb new file mode 100644 index 00000000..8948ba99 --- /dev/null +++ b/apm-lambda-extension/tests/functions/ruby/ruby_function_error.rb @@ -0,0 +1,14 @@ +load_paths = Dir["vendor/bundle/ruby/2.7.0/bundler/gems/**/lib"] +load_paths += Dir["vendor/bundle/ruby/2.7.0/gems/**/lib"] +load_paths += Dir["vendor/bundle/ruby/2.7.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby"] + +$LOAD_PATH.unshift(*load_paths) + +require 'json' +require 'elastic-apm' + +ElasticAPM.start(pool_size: 2) + +def lambda_handler(event:, context:) + raise Exception +end diff --git a/apm-lambda-extension/tests/templates/template.yml b/apm-lambda-extension/tests/templates/template.yml new file mode 100644 index 00000000..b3ddd6e8 --- /dev/null +++ b/apm-lambda-extension/tests/templates/template.yml @@ -0,0 +1,34 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: > + sam-testing + + +# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst +Globals: + Function: + Timeout: 3 + +Resources: + TestFunction: + Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction + Properties: + CodeUri: ../functions/ruby + Handler: ruby_function_error.lambda_handler + Runtime: ruby2.7 + PackageType: Zip + Layers: + - ${LAYER_ARN} + Architectures: + - x86_64 + Events: + Test: + Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api + Properties: + Path: / + Method: get + Environment: + Variables: + ELASTIC_APM_LAMBDA_APM_SERVER: ${ELASTIC_APM_LAMBDA_APM_SERVER} + ELASTIC_APM_SECRET_TOKEN: ${ELASTIC_APM_SECRET_TOKEN} + GEM_PATH: vendor/bundle/ruby/2.7.0 From 1ef76fe5f7de64972dfce4c41f289898a130ecdc Mon Sep 17 00:00:00 2001 From: Emily Stolfo Date: Tue, 26 Oct 2021 15:03:06 +0200 Subject: [PATCH 2/3] Change directory name to testing --- .../{tests => testing}/functions/ruby/Gemfile | 0 .../functions/ruby/ruby_function_error.rb | 6 +++++- .../{tests => testing}/templates/template.yml | 4 +--- 3 files changed, 6 insertions(+), 4 deletions(-) rename apm-lambda-extension/{tests => testing}/functions/ruby/Gemfile (100%) rename apm-lambda-extension/{tests => testing}/functions/ruby/ruby_function_error.rb (65%) rename apm-lambda-extension/{tests => testing}/templates/template.yml (95%) diff --git a/apm-lambda-extension/tests/functions/ruby/Gemfile b/apm-lambda-extension/testing/functions/ruby/Gemfile similarity index 100% rename from apm-lambda-extension/tests/functions/ruby/Gemfile rename to apm-lambda-extension/testing/functions/ruby/Gemfile diff --git a/apm-lambda-extension/tests/functions/ruby/ruby_function_error.rb b/apm-lambda-extension/testing/functions/ruby/ruby_function_error.rb similarity index 65% rename from apm-lambda-extension/tests/functions/ruby/ruby_function_error.rb rename to apm-lambda-extension/testing/functions/ruby/ruby_function_error.rb index 8948ba99..5eaa3f84 100644 --- a/apm-lambda-extension/tests/functions/ruby/ruby_function_error.rb +++ b/apm-lambda-extension/testing/functions/ruby/ruby_function_error.rb @@ -1,5 +1,6 @@ load_paths = Dir["vendor/bundle/ruby/2.7.0/bundler/gems/**/lib"] load_paths += Dir["vendor/bundle/ruby/2.7.0/gems/**/lib"] +load_paths += Dir["vendor/bundle/ruby/2.7.0/extensions/x86_64-linux/2.7.0/*"] load_paths += Dir["vendor/bundle/ruby/2.7.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby"] $LOAD_PATH.unshift(*load_paths) @@ -7,8 +8,11 @@ require 'json' require 'elastic-apm' -ElasticAPM.start(pool_size: 2) +ElasticAPM.start(pool_size: 2, service_name: 'LocalLambdaTesting') def lambda_handler(event:, context:) raise Exception +rescue => e + ElasticAPM.flush + raise e end diff --git a/apm-lambda-extension/tests/templates/template.yml b/apm-lambda-extension/testing/templates/template.yml similarity index 95% rename from apm-lambda-extension/tests/templates/template.yml rename to apm-lambda-extension/testing/templates/template.yml index b3ddd6e8..fcf35a5b 100644 --- a/apm-lambda-extension/tests/templates/template.yml +++ b/apm-lambda-extension/testing/templates/template.yml @@ -7,7 +7,7 @@ Description: > # More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst Globals: Function: - Timeout: 3 + Timeout: 20 Resources: TestFunction: @@ -19,8 +19,6 @@ Resources: PackageType: Zip Layers: - ${LAYER_ARN} - Architectures: - - x86_64 Events: Test: Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api From 8fcf4952397016a6cc908c60e73073fcfe2c03f3 Mon Sep 17 00:00:00 2001 From: Emily Stolfo Date: Tue, 7 Dec 2021 12:50:44 +0100 Subject: [PATCH 3/3] Use parameters in sam cli template and send flushed message from function --- .../functions/ruby/ruby_function_error.rb | 7 +++---- .../testing/templates/template.yml | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/apm-lambda-extension/testing/functions/ruby/ruby_function_error.rb b/apm-lambda-extension/testing/functions/ruby/ruby_function_error.rb index 5eaa3f84..2c8bb5b5 100644 --- a/apm-lambda-extension/testing/functions/ruby/ruby_function_error.rb +++ b/apm-lambda-extension/testing/functions/ruby/ruby_function_error.rb @@ -8,11 +8,10 @@ require 'json' require 'elastic-apm' -ElasticAPM.start(pool_size: 2, service_name: 'LocalLambdaTesting') +ElasticAPM.start(service_name: 'LocalLambdaTesting') def lambda_handler(event:, context:) raise Exception -rescue => e - ElasticAPM.flush - raise e +ensure + HTTP.post("http://localhost:8200/intake/v2/events", :params => {:flushed => "true"}) end diff --git a/apm-lambda-extension/testing/templates/template.yml b/apm-lambda-extension/testing/templates/template.yml index fcf35a5b..70f06f8d 100644 --- a/apm-lambda-extension/testing/templates/template.yml +++ b/apm-lambda-extension/testing/templates/template.yml @@ -9,6 +9,17 @@ Globals: Function: Timeout: 20 +Parameters: + LayerArn: + Type: String + Description: Function Layer arn + ApmServerURL: + Type: String + Description: APM server URL + ApmSecretToken: + Type: String + Description: APM server secret token + Resources: TestFunction: Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction @@ -18,7 +29,7 @@ Resources: Runtime: ruby2.7 PackageType: Zip Layers: - - ${LAYER_ARN} + - !Ref LayerArn Events: Test: Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api @@ -27,6 +38,6 @@ Resources: Method: get Environment: Variables: - ELASTIC_APM_LAMBDA_APM_SERVER: ${ELASTIC_APM_LAMBDA_APM_SERVER} - ELASTIC_APM_SECRET_TOKEN: ${ELASTIC_APM_SECRET_TOKEN} + ELASTIC_APM_LAMBDA_APM_SERVER: !Ref ApmServerURL + ELASTIC_APM_SECRET_TOKEN: !Ref ApmSecretToken GEM_PATH: vendor/bundle/ruby/2.7.0