From d75917e23cac28004fcf7ca22d40bdcb9dda8117 Mon Sep 17 00:00:00 2001 From: Ariel Valentin Date: Mon, 10 Jun 2024 23:24:15 -0500 Subject: [PATCH] fix: Add net.peer.name to ethon The ethon instrumentation did not include the `net.peer.name` attribute like other HTTP instrumentations and was not ported from ddtrace-rb: 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. --- instrumentation/ethon/Appraisals | 8 +++---- .../instrumentation/ethon/patches/easy.rb | 21 +++++++++++++++++-- .../ethon/instrumentation_test.rb | 5 ++++- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/instrumentation/ethon/Appraisals b/instrumentation/ethon/Appraisals index 137aed805..61520f97d 100644 --- a/instrumentation/ethon/Appraisals +++ b/instrumentation/ethon/Appraisals @@ -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 diff --git a/instrumentation/ethon/lib/opentelemetry/instrumentation/ethon/patches/easy.rb b/instrumentation/ethon/lib/opentelemetry/instrumentation/ethon/patches/easy.rb index 18b11dc51..74791818e 100644 --- a/instrumentation/ethon/lib/opentelemetry/instrumentation/ethon/patches/easy.rb +++ b/instrumentation/ethon/lib/opentelemetry/instrumentation/ethon/patches/easy.rb @@ -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] @@ -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 diff --git a/instrumentation/ethon/test/opentelemetry/instrumentation/ethon/instrumentation_test.rb b/instrumentation/ethon/test/opentelemetry/instrumentation/ethon/instrumentation_test.rb index 1c3a24035..e39779bf0 100644 --- a/instrumentation/ethon/test/opentelemetry/instrumentation/ethon/instrumentation_test.rb +++ b/instrumentation/ethon/test/opentelemetry/instrumentation/ethon/instrumentation_test.rb @@ -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 @@ -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