Skip to content

Commit

Permalink
fix: Add net.peer.name to ethon (#1004)
Browse files Browse the repository at this point in the history
The ethon instrumentation did not include the `net.peer.name` attribute like other HTTP instrumentations and was not ported from ddtrace-rb: <https://github.com/DataDog/dd-trace-rb/blob/master/lib/datadog/tracing/contrib/ethon/easy_patch.rb#L159>

This attribute is important to GitHub because we base the `peer.service` extracting the subdomain and host information from `net.peer.name`.

In order to avoid parsing the URL multiple times, I inlined the common gem `cleanse_url` function and plan to open a separate PR adding a feature to support returing URI objects instead of Strings.
  • Loading branch information
arielvalentin authored Jun 11, 2024
1 parent 4357c96 commit b26017c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
8 changes: 4 additions & 4 deletions instrumentation/ethon/Appraisals
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

appraise 'ethon-0.12' do
gem 'ethon', '~> 0.12.0'
appraise 'ethon-0.16' do
gem 'ethon', '~> 0.16.0'
end

appraise 'ethon-0.11' do
gem 'ethon', '~> 0.11.0'
appraise 'ethon-latest' do
gem 'ethon'
end
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ def span_creation_attributes(method)
'http.method' => method
}

http_url = OpenTelemetry::Common::Utilities.cleanse_url(url)
instrumentation_attrs['http.url'] = http_url if http_url
uri = _otel_cleanse_uri(url)
if uri
instrumentation_attrs['http.url'] = uri.to_s
instrumentation_attrs['net.peer.name'] = uri.host if uri.host
end

config = Ethon::Instrumentation.instance.config
instrumentation_attrs['peer.service'] = config[:peer_service] if config[:peer_service]
Expand All @@ -97,6 +100,20 @@ def span_creation_attributes(method)
)
end

# Returns a URL string with userinfo removed.
#
# @param [String] url The URL string to cleanse.
#
# @return [String] the cleansed URL.
def _otel_cleanse_uri(url)
cleansed_url = URI.parse(url)
cleansed_url.password = nil
cleansed_url.user = nil
cleansed_url
rescue URI::Error
nil
end

def tracer
Ethon::Instrumentation.instance.tracer
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
_(span.attributes['http.method']).must_equal 'N/A'
_(span.attributes['http.status_code']).must_be_nil
_(span.attributes['http.url']).must_equal 'http://example.com/test'
_(span.attributes['net.peer.name']).must_equal 'example.com'
end
end
end
Expand Down Expand Up @@ -275,8 +276,10 @@ def stub_response(options)
multi.perform

_(exporter.finished_spans.size).must_equal 2
_(exporter.finished_spans[0].attributes['http.url']).must_equal nil
_(exporter.finished_spans[0].attributes['http.url']).must_be_nil
_(exporter.finished_spans[0].attributes['net.peer.name']).must_be_nil
_(exporter.finished_spans[1].attributes['http.url']).must_equal 'test'
_(exporter.finished_spans[1].attributes['net.peer.name']).must_be_nil
end
end
end
Expand Down

0 comments on commit b26017c

Please sign in to comment.