diff --git a/lib/ddtrace/contrib/http/patcher.rb b/lib/ddtrace/contrib/http/patcher.rb index 0e5a54f22af..2b4c068f4b9 100644 --- a/lib/ddtrace/contrib/http/patcher.rb +++ b/lib/ddtrace/contrib/http/patcher.rb @@ -110,12 +110,19 @@ def request(req, body = nil, &block) # :yield: +response+ pin.tracer.trace(NAME) do |span| begin + + if req.respond_to?(:uri) && req.uri + host_address = req.uri.host + host_port = req.uri.port.to_s + else + host_address = @address + host_port = @port.to_s + end + span.service = pin.service span.span_type = Datadog::Ext::HTTP::TYPE - span.resource = req.method - # Using the method as a resource, as URL/path can trigger - # a possibly infinite number of resources. + span.resource = host_address span.set_tag(Datadog::Ext::HTTP::URL, req.path) span.set_tag(Datadog::Ext::HTTP::METHOD, req.method) @@ -135,13 +142,8 @@ def request(req, body = nil, &block) # :yield: +response+ response = request_without_datadog(req, body, &block) end span.set_tag(Datadog::Ext::HTTP::STATUS_CODE, response.code) - if req.respond_to?(:uri) && req.uri - span.set_tag(Datadog::Ext::NET::TARGET_HOST, req.uri.host) - span.set_tag(Datadog::Ext::NET::TARGET_PORT, req.uri.port.to_s) - else - span.set_tag(Datadog::Ext::NET::TARGET_HOST, @address) - span.set_tag(Datadog::Ext::NET::TARGET_PORT, @port.to_s) - end + span.set_tag(Datadog::Ext::NET::TARGET_HOST, host_address) + span.set_tag(Datadog::Ext::NET::TARGET_PORT, host_port) case response.code.to_i / 100 when 4 diff --git a/test/contrib/http/miniapp_test.rb b/test/contrib/http/miniapp_test.rb index 79a2901479a..a9e33d5fd2f 100644 --- a/test/contrib/http/miniapp_test.rb +++ b/test/contrib/http/miniapp_test.rb @@ -29,7 +29,7 @@ def check_span_page(span) def check_span_get(span, parent_id, trace_id) assert_equal('http.request', span.name) assert_equal('net/http', span.service) - assert_equal('GET', span.resource) + assert_equal(ELASTICSEARCH_HOST, span.resource) assert_equal('_cluster/health', span.get_tag('http.url')) assert_equal('GET', span.get_tag('http.method')) assert_equal('200', span.get_tag('http.status_code')) diff --git a/test/contrib/http/request_test.rb b/test/contrib/http/request_test.rb index 2b562a7eb7b..8845397f924 100644 --- a/test/contrib/http/request_test.rb +++ b/test/contrib/http/request_test.rb @@ -32,7 +32,7 @@ def test_get_request span = spans[0] assert_equal('http.request', span.name) assert_equal('net/http', span.service) - assert_equal('GET', span.resource) + assert_equal(ELASTICSEARCH_HOST, span.resource) assert_equal('_cluster/health', span.get_tag('http.url')) assert_equal('GET', span.get_tag('http.method')) assert_equal('200', span.get_tag('http.status_code')) @@ -48,7 +48,7 @@ def test_post_request span = spans[0] assert_equal('http.request', span.name) assert_equal('net/http', span.service) - assert_equal('POST', span.resource) + assert_equal(ELASTICSEARCH_HOST, span.resource) assert_equal('/my/thing/42', span.get_tag('http.url')) assert_equal('POST', span.get_tag('http.method')) assert_equal('127.0.0.1', span.get_tag('out.host')) @@ -64,7 +64,7 @@ def test_404 span = spans[0] assert_equal('http.request', span.name) assert_equal('net/http', span.service) - assert_equal('GET', span.resource) + assert_equal(ELASTICSEARCH_HOST, span.resource) assert_equal('/admin.php?user=admin&passwd=123456', span.get_tag('http.url')) assert_equal('GET', span.get_tag('http.method')) assert_equal('404', span.get_tag('http.status_code')) @@ -89,7 +89,7 @@ def test_pin_block_call span = spans[0] assert_equal('http.request', span.name) assert_equal('net/http', span.service) - assert_equal('GET', span.resource) + assert_equal(ELASTICSEARCH_HOST, span.resource) assert_equal('/_cluster/health', span.get_tag('http.url')) assert_equal('GET', span.get_tag('http.method')) assert_equal('200', span.get_tag('http.status_code'))