From 759030f60d9101f3372533887e4dc80bf066fd24 Mon Sep 17 00:00:00 2001 From: James Bowman Date: Mon, 18 Jun 2018 17:26:42 -0700 Subject: [PATCH] Excluding query string in segment.http.request.url generated by Rack middleware. --- aws-xray-sdk.gemspec | 1 + lib/aws-xray-sdk/facets/rack.rb | 5 ++- test/aws-xray-sdk/tc_facet_rack.rb | 65 ++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 test/aws-xray-sdk/tc_facet_rack.rb diff --git a/aws-xray-sdk.gemspec b/aws-xray-sdk.gemspec index 788a7e6..072bc28 100644 --- a/aws-xray-sdk.gemspec +++ b/aws-xray-sdk.gemspec @@ -27,6 +27,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'aws-sdk-s3', '~> 1' spec.add_development_dependency 'bundler', '~> 1.0' spec.add_development_dependency 'minitest', '~> 5.0' + spec.add_development_dependency 'rack', '~> 2.0' spec.add_development_dependency 'rake', '~> 12.0' spec.add_development_dependency 'simplecov', '~> 0.15' spec.add_development_dependency 'webmock', '~> 3.0' diff --git a/lib/aws-xray-sdk/facets/rack.rb b/lib/aws-xray-sdk/facets/rack.rb index c3f8307..6b74cd6 100644 --- a/lib/aws-xray-sdk/facets/rack.rb +++ b/lib/aws-xray-sdk/facets/rack.rb @@ -8,6 +8,7 @@ module Rack class Middleware include XRay::Facets::Helper X_FORWARD = 'HTTP_X_FORWARDED_FOR'.freeze + SCHEME_SEPARATOR = "://".freeze def initialize(app, recorder: nil) @app = app @@ -63,7 +64,9 @@ def call(env) def extract_request_meta(req) req_meta = {} - req_meta[:url] = req.url if req.url + req_meta[:url] = req.scheme + SCHEME_SEPARATOR if req.scheme + req_meta[:url] += req.host_with_port if req.host_with_port + req_meta[:url] += req.path if req.path req_meta[:user_agent] = req.user_agent if req.user_agent req_meta[:method] = req.request_method if req.request_method if req.has_header?(X_FORWARD) diff --git a/test/aws-xray-sdk/tc_facet_rack.rb b/test/aws-xray-sdk/tc_facet_rack.rb new file mode 100644 index 0000000..524c8df --- /dev/null +++ b/test/aws-xray-sdk/tc_facet_rack.rb @@ -0,0 +1,65 @@ +require_relative '../test_helper' +require 'aws-xray-sdk/facets/rack' +require 'rack' + +class TestFacetRack < Minitest::Test + + ENV_WITH_QUERY_STRING = { + "GATEWAY_INTERFACE" => "CGI/1.1", + "PATH_INFO" => "/index.html", + "QUERY_STRING" => "", + "REMOTE_ADDR" => "::1", + "REMOTE_HOST" => "localhost", + "REQUEST_METHOD" => "GET", + "REQUEST_URI" => "http://localhost:3000/index.html?foo=bar", + "SCRIPT_NAME" => "", + "SERVER_NAME" => "localhost", + "SERVER_PORT" => "3000", + "SERVER_PROTOCOL" => "HTTP/1.1", + "SERVER_SOFTWARE" => "WEBrick/1.3.1 (Ruby/2.0.0/2013-11-22)", + "HTTP_HOST" => "localhost:3000", + "HTTP_USER_AGENT" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:26.0) Gecko/20100101 Firefox/26.0", + "HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", + "HTTP_ACCEPT_LANGUAGE" => "zh-tw,zh;q=0.8,en-us;q=0.5,en;q=0.3", + "HTTP_ACCEPT_ENCODING" => "gzip, deflate", + "HTTP_COOKIE" => "jsonrpc.session=3iqp3ydRwFyqjcfO0GT2bzUh.bacc2786c7a81df0d0e950bec8fa1a9b1ba0bb61", + "HTTP_CONNECTION" => "keep-alive", + "HTTP_CACHE_CONTROL" => "max-age=0", + "rack.version" => [1, 2], + "rack.multithread" => true, + "rack.multiprocess" => false, + "rack.run_once" => false, + "rack.url_scheme" => "http", + "HTTP_VERSION" => "HTTP/1.1", + "REQUEST_PATH" => "/index.html" + }.freeze + + @@recorder = XRay::Recorder.new + config = { + name: "rack_test", + emitter: XRay::TestHelper::StubbedEmitter.new, + } + @@recorder.configure(config) + + @@app = Minitest::Mock.new + def @@app.call(env) + return 200, {}, "" + end + + def setup + @@recorder.context.clear! + @@recorder.emitter.clear + end + + def teardown + @@recorder.context.clear! + @@recorder.emitter.clear + end + + def test_rack_http_url_excludes_query_string + middleware = XRay::Rack::Middleware.new(@@app, :recorder => @@recorder) + middleware.call ENV_WITH_QUERY_STRING + segment = @@recorder.emitter.entities[0] + assert_equal "http://localhost:3000/index.html", segment.http_request[:url] + end +end