From 432670a3230fbe3ceabcd32f4432a061db1aab87 Mon Sep 17 00:00:00 2001 From: xuan-cao-swi Date: Tue, 12 Dec 2023 12:23:14 -0500 Subject: [PATCH] feat: revision on fix sample test, update readme and add rubocop-performance --- instrumentation/aws_lambda/.rubocop.yml | 1 + instrumentation/aws_lambda/README.md | 3 +-- .../aws_lambda/example/trace_demonstration.rb | 26 +++++++++++++++++++ ...lemetry-instrumentation-aws_lambda.gemspec | 1 + 4 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 instrumentation/aws_lambda/.rubocop.yml diff --git a/instrumentation/aws_lambda/.rubocop.yml b/instrumentation/aws_lambda/.rubocop.yml new file mode 100644 index 000000000..1248a2f82 --- /dev/null +++ b/instrumentation/aws_lambda/.rubocop.yml @@ -0,0 +1 @@ +inherit_from: ../../.rubocop.yml diff --git a/instrumentation/aws_lambda/README.md b/instrumentation/aws_lambda/README.md index e94e61e6e..04f57fe54 100644 --- a/instrumentation/aws_lambda/README.md +++ b/instrumentation/aws_lambda/README.md @@ -17,10 +17,9 @@ Or, if you use [bundler][bundler-home], include `opentelemetry-instrumentation-a From the Lambda Layer side, create the wrapper. More information can be found at https://github.com/open-telemetry/opentelemetry-lambda ```ruby require 'opentelemetry/sdk' -require 'opentelemetry/instrumentation/all' +require 'opentelemetry/instrumentation/aws_lambda' OpenTelemetry::SDK.configure do |c| c.service_name = '' - c.use_all() end def otel_wrapper(event:, context:) diff --git a/instrumentation/aws_lambda/example/trace_demonstration.rb b/instrumentation/aws_lambda/example/trace_demonstration.rb index f7eb1e301..44efc5fb9 100755 --- a/instrumentation/aws_lambda/example/trace_demonstration.rb +++ b/instrumentation/aws_lambda/example/trace_demonstration.rb @@ -27,6 +27,32 @@ def initialize(aws_request_id:, invoked_function_arn:) end end +# To accommendate the test case, handler class doesn't need to require the sample file if it's required here +# In lambda environment, the env will find the handler file. +module OpenTelemetry + module Instrumentation + module AwsLambda + class Handler + def resolve_original_handler + original_handler = ENV['ORIG_HANDLER'] || ENV['_HANDLER'] || '' + original_handler_parts = original_handler.split('.') + if original_handler_parts.size == 2 + handler_file, @handler_method = original_handler_parts + elsif original_handler_parts.size == 3 + handler_file, @handler_class, @handler_method = original_handler_parts + else + OpenTelemetry.logger.warn("aws-lambda instrumentation: Invalid handler #{original_handler}, must be of form FILENAME.METHOD or FILENAME.CLASS.METHOD.") + end + + # require handler_file #-> don't require file for this sample test + + original_handler + end + end + end + end +end + def otel_wrapper(event:, context:) otel_wrapper = OpenTelemetry::Instrumentation::AwsLambda::Handler.new() otel_wrapper.call_wrapped(event: event, context: context) diff --git a/instrumentation/aws_lambda/opentelemetry-instrumentation-aws_lambda.gemspec b/instrumentation/aws_lambda/opentelemetry-instrumentation-aws_lambda.gemspec index 5567e5081..972f1ee70 100644 --- a/instrumentation/aws_lambda/opentelemetry-instrumentation-aws_lambda.gemspec +++ b/instrumentation/aws_lambda/opentelemetry-instrumentation-aws_lambda.gemspec @@ -37,6 +37,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'pry-byebug' unless RUBY_ENGINE == 'jruby' spec.add_development_dependency 'rspec-mocks' spec.add_development_dependency 'rubocop', '~> 1.56.1' + spec.add_development_dependency 'rubocop-performance', '~> 1.19.1' spec.add_development_dependency 'simplecov', '~> 0.17.1' spec.add_development_dependency 'webmock', '~> 3.19' spec.add_development_dependency 'yard', '~> 0.9'