Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: register lambda span #1073

Merged
merged 6 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def call_wrapped(event:, context:)

original_handler_error = nil
original_response = nil
trace_token = nil
OpenTelemetry::Context.with_current(parent_context) do
span_attributes = otel_attributes(event, context)
span = tracer.start_span(
Expand All @@ -47,6 +48,9 @@ def call_wrapped(event:, context:)
kind: span_kind
)

trace_ctx = OpenTelemetry::Trace.context_with_span(span)
trace_token = OpenTelemetry::Context.attach(trace_ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xuan-cao-swi I'm curious whether you tried using tracer.in_span instead of tracer.start_span and having to explicitly manage the context.

Since you're already using OpenTelemetry::Context.with_current(parent_context) in line 43 above to propagate any incoming context into the current one, in_span should be able to continue that and as a benefit record exception too. Some examples in exising "server" or "consumer" instrumentation that deal with the same concern of propagating incoming context from external process, setting it into current process's context, and starting a span.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I agree in_span is a better choice for this instrumentation. Updated!


begin
response = call_original_handler(event: event, context: context)
status_code = response['statusCode'] || response[:statusCode] if response.is_a?(Hash)
Expand All @@ -63,6 +67,7 @@ def call_wrapped(event:, context:)
span&.record_exception(original_handler_error)
span&.status = OpenTelemetry::Trace::Status.error(original_handler_error.message)
end
OpenTelemetry::Context.detach(trace_token) if trace_token
span&.finish
OpenTelemetry.tracer_provider.force_flush(timeout: @flush_timeout)
end
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