Skip to content

Commit b26017c

Browse files
fix: Add net.peer.name to ethon (#1004)
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.
1 parent 4357c96 commit b26017c

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

instrumentation/ethon/Appraisals

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# frozen_string_literal: true
22

3-
appraise 'ethon-0.12' do
4-
gem 'ethon', '~> 0.12.0'
3+
appraise 'ethon-0.16' do
4+
gem 'ethon', '~> 0.16.0'
55
end
66

7-
appraise 'ethon-0.11' do
8-
gem 'ethon', '~> 0.11.0'
7+
appraise 'ethon-latest' do
8+
gem 'ethon'
99
end

instrumentation/ethon/lib/opentelemetry/instrumentation/ethon/patches/easy.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@ def span_creation_attributes(method)
8787
'http.method' => method
8888
}
8989

90-
http_url = OpenTelemetry::Common::Utilities.cleanse_url(url)
91-
instrumentation_attrs['http.url'] = http_url if http_url
90+
uri = _otel_cleanse_uri(url)
91+
if uri
92+
instrumentation_attrs['http.url'] = uri.to_s
93+
instrumentation_attrs['net.peer.name'] = uri.host if uri.host
94+
end
9295

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

103+
# Returns a URL string with userinfo removed.
104+
#
105+
# @param [String] url The URL string to cleanse.
106+
#
107+
# @return [String] the cleansed URL.
108+
def _otel_cleanse_uri(url)
109+
cleansed_url = URI.parse(url)
110+
cleansed_url.password = nil
111+
cleansed_url.user = nil
112+
cleansed_url
113+
rescue URI::Error
114+
nil
115+
end
116+
100117
def tracer
101118
Ethon::Instrumentation.instance.tracer
102119
end

instrumentation/ethon/test/opentelemetry/instrumentation/ethon/instrumentation_test.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
_(span.attributes['http.method']).must_equal 'N/A'
7272
_(span.attributes['http.status_code']).must_be_nil
7373
_(span.attributes['http.url']).must_equal 'http://example.com/test'
74+
_(span.attributes['net.peer.name']).must_equal 'example.com'
7475
end
7576
end
7677
end
@@ -275,8 +276,10 @@ def stub_response(options)
275276
multi.perform
276277

277278
_(exporter.finished_spans.size).must_equal 2
278-
_(exporter.finished_spans[0].attributes['http.url']).must_equal nil
279+
_(exporter.finished_spans[0].attributes['http.url']).must_be_nil
280+
_(exporter.finished_spans[0].attributes['net.peer.name']).must_be_nil
279281
_(exporter.finished_spans[1].attributes['http.url']).must_equal 'test'
282+
_(exporter.finished_spans[1].attributes['net.peer.name']).must_be_nil
280283
end
281284
end
282285
end

0 commit comments

Comments
 (0)