Skip to content

Commit

Permalink
Set host or address as resource for Net::HTTP
Browse files Browse the repository at this point in the history
Tracking external requests by host is more useful for understanding performance issues than tracking by HTTP method. By using the host we should have a smaller potential list than if we were tracking by URL/endpoint.

We don't always have a hostname, so lets use logic that exists for falling back to address.
  • Loading branch information
buddhistpirate committed Dec 14, 2017
1 parent f2aad98 commit c3d3492
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
22 changes: 12 additions & 10 deletions lib/ddtrace/contrib/http/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/contrib/http/miniapp_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
8 changes: 4 additions & 4 deletions test/contrib/http/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand All @@ -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'))
Expand All @@ -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'))
Expand All @@ -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'))
Expand Down

0 comments on commit c3d3492

Please sign in to comment.