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

feat: add lambda ruby wrapper for layer #1323

Merged
merged 7 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 0 additions & 1 deletion ruby/sample-apps/function/lambda_function.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'json'
require 'opentelemetry-sdk'

def lambda_handler(event:, context:)
if defined?(::OpenTelemetry::SDK)
Expand Down
1 change: 1 addition & 0 deletions ruby/sample-apps/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Resources:
RestApiId: !Ref api
Path: /
Method: GET
Timeout: 60
Environment:
Variables:
AWS_LAMBDA_EXEC_WRAPPER: /opt/otel-handler
2 changes: 1 addition & 1 deletion ruby/src/layer/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ source 'https://rubygems.org'

gem 'opentelemetry-sdk'
gem 'opentelemetry-exporter-otlp'
gem 'opentelemetry-instrumentation-all'
gem 'opentelemetry-instrumentation-aws_lambda'
6 changes: 4 additions & 2 deletions ruby/src/layer/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

build-OTelLayer:
mkdir -p ruby
mkdir -p $(ARTIFACTS_DIR)/ruby/gems/3.2.0
bundler install --path ruby
bundle config set --global silence_root_warning 1
bundle config set --local path 'ruby'
bundle install
cp -r ruby/ruby/3.2.0/* $(ARTIFACTS_DIR)/ruby/gems/3.2.0
cp otel-handler $(ARTIFACTS_DIR)/otel-handler
cp wrapper.rb $(ARTIFACTS_DIR)/wrapper.rb
rm -rf $(ARTIFACTS_DIR)/ruby/gems/3.2.0/cache
10 changes: 9 additions & 1 deletion ruby/src/layer/otel-handler
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
#!/bin/sh

export ORIG_HANDLER="$_HANDLER";
export _HANDLER="/opt/wrapper.otel_wrapper";

if [ -z "${OTEL_SERVICE_NAME}" ]; then
export OTEL_SERVICE_NAME="$AWS_LAMBDA_FUNCTION_NAME";
fi

# disable HTTP NET by default - otherwise, lambda runtime gets instrumented yielding noise
if [ -z "${OTEL_RUBY_INSTRUMENTATION_NET_HTTP_ENABLED}" ]; then
export OTEL_RUBY_INSTRUMENTATION_NET_HTTP_ENABLED=false;
fi

export LAMBDA_RESOURCE_ATTRIBUTES="cloud.region=$AWS_REGION,cloud.provider=aws,faas.name=$AWS_LAMBDA_FUNCTION_NAME,faas.version=$AWS_LAMBDA_FUNCTION_VERSION";
if [ -z "${OTEL_RESOURCE_ATTRIBUTES}" ]; then
export OTEL_RESOURCE_ATTRIBUTES="$LAMBDA_RESOURCE_ATTRIBUTES";
else
export OTEL_RESOURCE_ATTRIBUTES="$LAMBDA_RESOURCE_ATTRIBUTES,$OTEL_RESOURCE_ATTRIBUTES";
fi

exec "$@"
exec "$@"
15 changes: 15 additions & 0 deletions ruby/src/layer/wrapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp'
require 'opentelemetry/instrumentation/aws_lambda'

OpenTelemetry::SDK.configure do |c|
c.use 'OpenTelemetry::Instrumentation::AwsLambda'
end

::OpenTelemetry::Instrumentation.registry.register('OpenTelemetry::Instrumentation::AwsLambda')
::OpenTelemetry::Instrumentation.registry.install(['OpenTelemetry::Instrumentation::AwsLambda'])
Copy link
Member

Choose a reason for hiding this comment

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

I'm curious about these two lines. Doesn't Instrumentation::Base do this automatically?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, yes, it should do it automatically, I will remove the registry part.


def otel_wrapper(event:, context:)
otel_wrapper = OpenTelemetry::Instrumentation::AwsLambda::Handler.new()
otel_wrapper.call_wrapped(event: event, context: context)
end
1 change: 1 addition & 0 deletions ruby/src/zip_ruby_layer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ main() {
cd .aws-sam/build/OTelLayer/
zip -qr ../../../"$layerName".zip ruby/
cd -
echo "Finished"
}

main "$@"
Loading