diff --git a/instrumentation/aws_lambda/README.md b/instrumentation/aws_lambda/README.md index 90dc918c9..eea0d446a 100644 --- a/instrumentation/aws_lambda/README.md +++ b/instrumentation/aws_lambda/README.md @@ -36,6 +36,7 @@ To run the example: * `bundle install` 2. Run the sample client script * `ruby trace_demonstration.rb` + * or `bundle exec ruby trace_demonstration.rb` This will run SNS publish command, printing OpenTelemetry traces to the console as it goes. diff --git a/instrumentation/aws_lambda/example/trace_demonstration.rb b/instrumentation/aws_lambda/example/trace_demonstration.rb index b135bad5b..7af664018 100755 --- a/instrumentation/aws_lambda/example/trace_demonstration.rb +++ b/instrumentation/aws_lambda/example/trace_demonstration.rb @@ -97,6 +97,6 @@ def otel_wrapper(event:, context:) "version" => "1.0" } -context = MockLambdaContext.new(aws_request_id: "aws_request_id",invoked_function_arn: "invoked_function_arn",function_name: "function") +context = MockLambdaContext.new(aws_request_id: "aws_request_id",invoked_function_arn: "arn:aws:lambda:us-west-2:123456789012:function:HelloWorld",function_name: "function") otel_wrapper(event: event, context: context) # you should see Success before the trace diff --git a/instrumentation/aws_lambda/lib/opentelemetry/instrumentation/aws_lambda/handler.rb b/instrumentation/aws_lambda/lib/opentelemetry/instrumentation/aws_lambda/handler.rb index 7c9a32c18..69c050be5 100644 --- a/instrumentation/aws_lambda/lib/opentelemetry/instrumentation/aws_lambda/handler.rb +++ b/instrumentation/aws_lambda/lib/opentelemetry/instrumentation/aws_lambda/handler.rb @@ -40,33 +40,25 @@ def call_wrapped(event:, context:) original_handler_error = nil original_response = nil OpenTelemetry::Context.with_current(parent_context) do - span_attributes = otel_attributes(event, context) - span = tracer.start_span( - @original_handler, - attributes: span_attributes, - kind: span_kind - ) - - begin - response = call_original_handler(event: event, context: context) - status_code = response['statusCode'] || response[:statusCode] if response.is_a?(Hash) - span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, status_code) if status_code - rescue StandardError => e - original_handler_error = e - ensure - original_response = response + tracer.in_span(@original_handler, attributes: otel_attributes(event, context), kind: span_kind) do |span| + begin + response = call_original_handler(event: event, context: context) + status_code = response['statusCode'] || response[:statusCode] if response.is_a?(Hash) + span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, status_code) if status_code + rescue StandardError => e + original_handler_error = e + ensure + original_response = response + end + if original_handler_error + span.record_exception(original_handler_error) + span.status = OpenTelemetry::Trace::Status.error(original_handler_error.message) + end end - rescue StandardError => e - OpenTelemetry.logger.error("aws-lambda instrumentation #{e.class}: #{e.message}") - ensure - if original_handler_error - span&.record_exception(original_handler_error) - span&.status = OpenTelemetry::Trace::Status.error(original_handler_error.message) - end - span&.finish - OpenTelemetry.tracer_provider.force_flush(timeout: @flush_timeout) end + OpenTelemetry.tracer_provider.force_flush(timeout: @flush_timeout) + raise original_handler_error if original_handler_error original_response diff --git a/instrumentation/aws_lambda/test/opentelemetry/instrumentation_test.rb b/instrumentation/aws_lambda/test/opentelemetry/instrumentation_test.rb index e737544f6..855b0bc6e 100644 --- a/instrumentation/aws_lambda/test/opentelemetry/instrumentation_test.rb +++ b/instrumentation/aws_lambda/test/opentelemetry/instrumentation_test.rb @@ -214,4 +214,25 @@ end end end + + describe 'validate_if_span_is_registered' do + it 'add_span_attributes_to_lambda_span' do + stub = proc do + span = OpenTelemetry::Trace.current_span + span.set_attribute('test.attribute', 320) + end + + otel_wrapper = OpenTelemetry::Instrumentation::AwsLambda::Handler.new + otel_wrapper.stub(:call_original_handler, stub) do + otel_wrapper.call_wrapped(event: sqs_record, context: context) + + _(last_span.name).must_equal 'sample.test' + _(last_span.kind).must_equal :consumer + _(last_span.status.code).must_equal 1 + _(last_span.hex_parent_span_id).must_equal '0000000000000000' + + _(last_span.attributes['test.attribute']).must_equal 320 + end + end + end end