Skip to content

Commit

Permalink
fix: register lambda span (#1073)
Browse files Browse the repository at this point in the history
* fix: register lambda span

* avoid use instance variable

* revision
  • Loading branch information
xuan-cao-swi committed Jul 23, 2024
1 parent bfe1e8c commit e7377e0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
1 change: 1 addition & 0 deletions instrumentation/aws_lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion instrumentation/aws_lambda/example/trace_demonstration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e7377e0

Please sign in to comment.