diff --git a/lib/elastic_apm/context_builder.rb b/lib/elastic_apm/context_builder.rb index f8445c8d8..534e46000 100644 --- a/lib/elastic_apm/context_builder.rb +++ b/lib/elastic_apm/context_builder.rb @@ -51,12 +51,14 @@ def apply_to_request(context, rack_env:, for_type:) request.body = should_capture_body?(for_type) ? get_body(req) : SKIPPED headers, env = get_headers_and_env(rack_env) - request.headers = headers if config.capture_headers? request.env = env if config.capture_env? - request.cookies = req.cookies.dup - unless request.cookies.empty? - request.headers['Cookie'] = SKIPPED if request.headers.has_key?('Cookie') + + if config.capture_headers? + request.headers = headers + unless request.cookies.empty? + request.headers['Cookie'] = SKIPPED if request.headers.has_key?('Cookie') + end end context diff --git a/spec/elastic_apm/context_builder_spec.rb b/spec/elastic_apm/context_builder_spec.rb index 228817105..e68a37de0 100644 --- a/spec/elastic_apm/context_builder_spec.rb +++ b/spec/elastic_apm/context_builder_spec.rb @@ -115,6 +115,21 @@ module ElasticAPM expect(result.request.body.valid_encoding?).to be true end end + + context 'with capture_headers false' do + let(:config) { Config.new capture_headers: false } + + it 'does not break on the cookies' do + env = Rack::MockRequest.env_for( + '/somewhere/in/there?q=yes', + method: 'POST', + 'HTTP_COOKIE' => 'things=1' + ) + env['HTTP_CONTENT_TYPE'] = 'application/json' + + expect { subject.build(rack_env: env, for_type: :transaction) }.not_to raise_error + end + end end end end