Skip to content

Commit

Permalink
feat: add lambda ruby wrapper for layer (#1323)
Browse files Browse the repository at this point in the history
* feat: add lambda ruby wrapper for layer

* double quote on env var

* feat: revision

* feat: change to dev

* feat: ensure version control
  • Loading branch information
xuan-cao-swi authored May 22, 2024
1 parent 4955e0c commit 4e2e142
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-layer-ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ jobs:
layer-name: opentelemetry-ruby
component-version: ${{needs.build-layer.outputs.RUBY_SDK_VERSION}}
runtimes: ruby3.2
release-group: prod
release-group: dev
aws_region: ${{ matrix.aws_region }}
secrets: inherit
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
6 changes: 3 additions & 3 deletions ruby/src/layer/Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source 'https://rubygems.org'

gem 'opentelemetry-sdk'
gem 'opentelemetry-exporter-otlp'
gem 'opentelemetry-instrumentation-all'
gem 'opentelemetry-sdk', '~> 1.4.1'
gem 'opentelemetry-exporter-otlp', '~> 0.26.3'
gem 'opentelemetry-instrumentation-aws_lambda', '~> 0.1.0'
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 "$@"
12 changes: 12 additions & 0 deletions ruby/src/layer/wrapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp'
require 'opentelemetry/instrumentation/aws_lambda'

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

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 "$@"

0 comments on commit 4e2e142

Please sign in to comment.