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 2 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 @@ -43,6 +43,7 @@ class EventHandler
include ::Rack::Events::Abstract

OTEL_TOKEN_AND_SPAN = 'otel.rack.token_and_span'
UNTRACED_CTX_TOKEN = 'otel.rack.untraced_ctx_token'
GOOD_HTTP_STATUSES = (100..499)

# Creates a server span for this current request using the incoming parent context
Expand All @@ -52,7 +53,11 @@ class EventHandler
# @param [Rack::Response] This is nil in practice
# @return [void]
def on_start(request, _)
return if untraced_request?(request.env)
if untraced_request?(request.env)
ctx = OpenTelemetry::Common::Utilities.untraced
request.env[UNTRACED_CTX_TOKEN] = OpenTelemetry::Context.attach(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need another context (attached) and another token for this? We should add a context = OpenTelemetry::Context.current parameter to extract_remote_context and pass in the untraced context if necessary. I.e.:

parent_context = if untraced_request?(request.env)
    extract_remote_context(request, OpenTelemetry::Common::Utilities.untraced)
  else
    extract_remote_context(request)
  end

return
end

parent_context = extract_remote_context(request)
span = create_span(parent_context, request)
Expand Down Expand Up @@ -194,7 +199,10 @@ def request_span_attributes(env)
end

def detach_context(request)
return nil unless request.env[OTEL_TOKEN_AND_SPAN]
if (untracted_ctx_token = request.env[UNTRACED_CTX_TOKEN])
OpenTelemetry::Context.detach(untracted_ctx_token)
return
end

token, span = request.env[OTEL_TOKEN_AND_SPAN]
span.finish
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