Skip to content

Commit e7377e0

Browse files
authored
fix: register lambda span (#1073)
* fix: register lambda span * avoid use instance variable * revision
1 parent bfe1e8c commit e7377e0

File tree

4 files changed

+39
-25
lines changed

4 files changed

+39
-25
lines changed

instrumentation/aws_lambda/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ To run the example:
3636
* `bundle install`
3737
2. Run the sample client script
3838
* `ruby trace_demonstration.rb`
39+
* or `bundle exec ruby trace_demonstration.rb`
3940

4041
This will run SNS publish command, printing OpenTelemetry traces to the console as it goes.
4142

instrumentation/aws_lambda/example/trace_demonstration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,6 @@ def otel_wrapper(event:, context:)
9797
"version" => "1.0"
9898
}
9999

100-
context = MockLambdaContext.new(aws_request_id: "aws_request_id",invoked_function_arn: "invoked_function_arn",function_name: "function")
100+
context = MockLambdaContext.new(aws_request_id: "aws_request_id",invoked_function_arn: "arn:aws:lambda:us-west-2:123456789012:function:HelloWorld",function_name: "function")
101101

102102
otel_wrapper(event: event, context: context) # you should see Success before the trace

instrumentation/aws_lambda/lib/opentelemetry/instrumentation/aws_lambda/handler.rb

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,25 @@ def call_wrapped(event:, context:)
4040
original_handler_error = nil
4141
original_response = nil
4242
OpenTelemetry::Context.with_current(parent_context) do
43-
span_attributes = otel_attributes(event, context)
44-
span = tracer.start_span(
45-
@original_handler,
46-
attributes: span_attributes,
47-
kind: span_kind
48-
)
49-
50-
begin
51-
response = call_original_handler(event: event, context: context)
52-
status_code = response['statusCode'] || response[:statusCode] if response.is_a?(Hash)
53-
span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, status_code) if status_code
54-
rescue StandardError => e
55-
original_handler_error = e
56-
ensure
57-
original_response = response
43+
tracer.in_span(@original_handler, attributes: otel_attributes(event, context), kind: span_kind) do |span|
44+
begin
45+
response = call_original_handler(event: event, context: context)
46+
status_code = response['statusCode'] || response[:statusCode] if response.is_a?(Hash)
47+
span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, status_code) if status_code
48+
rescue StandardError => e
49+
original_handler_error = e
50+
ensure
51+
original_response = response
52+
end
53+
if original_handler_error
54+
span.record_exception(original_handler_error)
55+
span.status = OpenTelemetry::Trace::Status.error(original_handler_error.message)
56+
end
5857
end
59-
rescue StandardError => e
60-
OpenTelemetry.logger.error("aws-lambda instrumentation #{e.class}: #{e.message}")
61-
ensure
62-
if original_handler_error
63-
span&.record_exception(original_handler_error)
64-
span&.status = OpenTelemetry::Trace::Status.error(original_handler_error.message)
65-
end
66-
span&.finish
67-
OpenTelemetry.tracer_provider.force_flush(timeout: @flush_timeout)
6858
end
6959

60+
OpenTelemetry.tracer_provider.force_flush(timeout: @flush_timeout)
61+
7062
raise original_handler_error if original_handler_error
7163

7264
original_response

instrumentation/aws_lambda/test/opentelemetry/instrumentation_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,25 @@
214214
end
215215
end
216216
end
217+
218+
describe 'validate_if_span_is_registered' do
219+
it 'add_span_attributes_to_lambda_span' do
220+
stub = proc do
221+
span = OpenTelemetry::Trace.current_span
222+
span.set_attribute('test.attribute', 320)
223+
end
224+
225+
otel_wrapper = OpenTelemetry::Instrumentation::AwsLambda::Handler.new
226+
otel_wrapper.stub(:call_original_handler, stub) do
227+
otel_wrapper.call_wrapped(event: sqs_record, context: context)
228+
229+
_(last_span.name).must_equal 'sample.test'
230+
_(last_span.kind).must_equal :consumer
231+
_(last_span.status.code).must_equal 1
232+
_(last_span.hex_parent_span_id).must_equal '0000000000000000'
233+
234+
_(last_span.attributes['test.attribute']).must_equal 320
235+
end
236+
end
237+
end
217238
end

0 commit comments

Comments
 (0)