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: Untrace entire request #968

Merged
merged 4 commits into from
May 9, 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 @@ -52,9 +52,12 @@ class EventHandler
# @param [Rack::Response] This is nil in practice
# @return [void]
def on_start(request, _)
return if untraced_request?(request.env)
parent_context = if untraced_request?(request.env)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this relying on start_span to look for the untraced context as opposed to seething the current context to untraced without an active 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.

Yeah it's seeing that it's parent context is untraced so it will create a non recording span, and we have a lot of recording? checks in this instrumentation as is so it seemed like a simpler implementation than what I had in this commit a8548ef

Copy link
Collaborator

Choose a reason for hiding this comment

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

Understood. Thanks again for fixing this!

extract_remote_context(request, OpenTelemetry::Common::Utilities.untraced)
else
extract_remote_context(request)
end

parent_context = extract_remote_context(request)
span = create_span(parent_context, request)
span_ctx = OpenTelemetry::Trace.context_with_span(span, parent_context: parent_context)
rack_ctx = OpenTelemetry::Instrumentation::Rack.context_with_span(span, parent_context: span_ctx)
Expand Down Expand Up @@ -173,9 +176,10 @@ def create_request_span_name(request)
end
end

def extract_remote_context(request)
def extract_remote_context(request, context = Context.current)
OpenTelemetry.propagation.extract(
request.env,
context: context,
getter: OpenTelemetry::Common::Propagation.rack_env_getter
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 3.0'

spec.add_dependency 'opentelemetry-api', '~> 1.0'
spec.add_dependency 'opentelemetry-common', '~> 0.20.0'
spec.add_dependency 'opentelemetry-common', '~> 0.21.0'
spec.add_dependency 'opentelemetry-instrumentation-base', '~> 0.22.1'

spec.add_development_dependency 'appraisal', '~> 2.5'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,22 @@
end

describe 'config[:untraced_endpoints]' do
let(:service) do
lambda do |_env|
OpenTelemetry.tracer_provider.tracer('req').in_span('in_req_span') {}
[200, { 'Content-Type' => 'text/plain' }, response_body]
end
end

describe 'when an array is passed in' do
let(:uri) { '/ping' }
let(:untraced_endpoints) { ['/ping'] }

it 'does not trace paths listed in the array' do
get '/ping'

ping_span = finished_spans.find { |s| s.attributes['http.target'] == '/ping' }
_(ping_span).must_be_nil

root_span = finished_spans.find { |s| s.attributes['http.target'] == '/' }
_(root_span).wont_be_nil
_(finished_spans.size).must_equal 0
end
end

Expand All @@ -164,19 +169,24 @@
end

describe 'config[:untraced_requests]' do
let(:service) do
lambda do |_env|
OpenTelemetry.tracer_provider.tracer('req').in_span('in_req_span') {}
[200, { 'Content-Type' => 'text/plain' }, response_body]
end
end

describe 'when a callable is passed in' do
let(:uri) { '/assets' }
let(:untraced_requests) do
->(env) { env['PATH_INFO'] =~ %r{^\/assets} }
end

it 'does not trace requests in which the callable returns true' do
get '/assets'

assets_span = finished_spans.find { |s| s.attributes['http.target'] == '/assets' }
_(assets_span).must_be_nil

root_span = finished_spans.find { |s| s.attributes['http.target'] == '/' }
_(root_span).wont_be_nil
_(finished_spans.size).must_equal 0
end
end

Expand Down
Loading