Skip to content

Instantly share code, notes, and snippets.

@xuan-cao-swi
Created December 13, 2023 17:09
Show Gist options
  • Save xuan-cao-swi/151a8348504b4c4fcd4019a5aa05d82a to your computer and use it in GitHub Desktop.
Save xuan-cao-swi/151a8348504b4c4fcd4019a5aa05d82a to your computer and use it in GitHub Desktop.
Testing lambda instrumentation with lambda layer

Lambda Instrumentation and Layer Test Environment Setup

This guide outlines detailed steps for setting up a test environment to evaluate both Lambda instrumentation (PR) and the Lambda layer (PR) for OpenTelemetry.

Step 1: Creating the Lambda Layer

  1. Clone the repository using the ruby-layer-wrapper branch from this location. This branch contains the wrapper file absent in the original PR.
  2. Navigate to ruby/src/ and execute sam build -u -t template.yml. This action generates the .aws-sam folder. Finally, run the script ./zip_ruby_layer.sh -n <your_layer_name> to create the zip file suitable for upload to AWS Lambda via the AWS console.
  3. If using a different tracing platform, ensure its installation before creating the layers.
  4. The gems listed in ruby/layer/Gemfile are the ones to be installed and uploaded.
  5. For custom OpenTelemetry initialization (e.g., requiring other vendor-modified agents), edit the ruby/src/wrapper.rb file accordingly.

Step 2: Uploading the Zip File to Create a Layer

Refer to: AWS Lambda Packaging Guide

  1. Create a new layer and upload the previously generated zip file.
  2. Associate this layer with a function by attaching it to the function configuration.

Step 3: Initializing a Traced Application with OpenTelemetry and Calling the Lambda Function

Assuming the lambda function is triggered (learn more about invoking functions) with the AWS_LAMBDA_URL and AWS_LAMBDA_KEY set:

Example of these two environment variables AWS_LAMBDA_URL=https://abcdefg.execute-api.location-1.amazonaws.com/default/your_lambda_function_name AWS_LAMBDA_KEY=0yN7bRPfu9g***********************0yvXsO7bRtpfu

Example Sinatra app code:

get '/call-with-lambda-aws' do
  url = URI.parse(ENV['AWS_LAMBDA_URL'])
  req = Net::HTTP::Get.new(url.to_s)
  req['x-api-key'] = "#{ENV['AWS_LAMBDA_KEY']}"

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = (url.scheme == 'https')

  res = http.request(req)

  content_type :json
  { :Output => "lambda: #{res.body}\n"}.to_json
end

Step 4: Checking the Trace Information

Upon execution, expect to observe a single span with the span scope name as OpenTelemetry::Instrumentation::AwsLambda, accompanied by comprehensive details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment