Skip to content

Commit 2fe3930

Browse files
ttarielvalentin
andauthored
fix: Add http.url to Excon instrumentation (#1020)
* Sort attributes alphabetically * Verify port equivalence * Add `http.url` to Excon instrumentation --------- Co-authored-by: Ariel Valentin <[email protected]>
1 parent c97fc56 commit 2fe3930

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

instrumentation/excon/lib/opentelemetry/instrumentation/excon/middlewares/tracer_middleware.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ def request_call(datum)
2727
http_method = HTTP_METHODS_TO_UPPERCASE[datum[:method]]
2828

2929
attributes = {
30+
OpenTelemetry::SemanticConventions::Trace::HTTP_HOST => datum[:host],
3031
OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => http_method,
3132
OpenTelemetry::SemanticConventions::Trace::HTTP_SCHEME => datum[:scheme],
3233
OpenTelemetry::SemanticConventions::Trace::HTTP_TARGET => datum[:path],
33-
OpenTelemetry::SemanticConventions::Trace::HTTP_HOST => datum[:host],
34+
OpenTelemetry::SemanticConventions::Trace::HTTP_URL => OpenTelemetry::Common::Utilities.cleanse_url(::Excon::Utils.request_uri(datum)),
3435
OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => datum[:hostname],
3536
OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => datum[:port]
3637
}

instrumentation/excon/test/opentelemetry/instrumentation/excon/instrumentation_test.rb

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@
4848

4949
_(exporter.finished_spans.size).must_equal 1
5050
_(span.name).must_equal 'HTTP GET'
51+
_(span.attributes['http.host']).must_equal 'example.com'
5152
_(span.attributes['http.method']).must_equal 'GET'
52-
_(span.attributes['http.status_code']).must_equal 200
5353
_(span.attributes['http.scheme']).must_equal 'http'
54-
_(span.attributes['http.host']).must_equal 'example.com'
54+
_(span.attributes['http.status_code']).must_equal 200
5555
_(span.attributes['http.target']).must_equal '/success'
56+
_(span.attributes['http.url']).must_equal 'http://example.com/success'
5657
assert_requested(
5758
:get,
5859
'http://example.com/success',
@@ -71,11 +72,12 @@
7172

7273
_(exporter.finished_spans.size).must_equal 1
7374
_(span.name).must_equal 'HTTP GET'
75+
_(span.attributes['http.host']).must_equal 'example.com'
7476
_(span.attributes['http.method']).must_equal 'GET'
75-
_(span.attributes['http.status_code']).must_equal 500
7677
_(span.attributes['http.scheme']).must_equal 'http'
77-
_(span.attributes['http.host']).must_equal 'example.com'
78+
_(span.attributes['http.status_code']).must_equal 500
7879
_(span.attributes['http.target']).must_equal '/failure'
80+
_(span.attributes['http.url']).must_equal 'http://example.com/failure'
7981
assert_requested(
8082
:get,
8183
'http://example.com/failure',
@@ -90,10 +92,11 @@
9092

9193
_(exporter.finished_spans.size).must_equal 1
9294
_(span.name).must_equal 'HTTP GET'
95+
_(span.attributes['http.host']).must_equal 'example.com'
9396
_(span.attributes['http.method']).must_equal 'GET'
9497
_(span.attributes['http.scheme']).must_equal 'http'
95-
_(span.attributes['http.host']).must_equal 'example.com'
9698
_(span.attributes['http.target']).must_equal '/timeout'
99+
_(span.attributes['http.url']).must_equal 'http://example.com/timeout'
97100
_(span.status.code).must_equal(
98101
OpenTelemetry::Trace::Status::ERROR
99102
)
@@ -119,11 +122,12 @@
119122

120123
_(exporter.finished_spans.size).must_equal 1
121124
_(span.name).must_equal 'HTTP GET'
125+
_(span.attributes['http.host']).must_equal 'example.com'
122126
_(span.attributes['http.method']).must_equal 'OVERRIDE'
123-
_(span.attributes['http.status_code']).must_equal 200
124127
_(span.attributes['http.scheme']).must_equal 'http'
125-
_(span.attributes['http.host']).must_equal 'example.com'
128+
_(span.attributes['http.status_code']).must_equal 200
126129
_(span.attributes['http.target']).must_equal '/success'
130+
_(span.attributes['http.url']).must_equal 'http://example.com/success'
127131
_(span.attributes['test.attribute']).must_equal 'test.value'
128132
assert_requested(
129133
:get,
@@ -186,8 +190,8 @@
186190

187191
_(exporter.finished_spans.size).must_equal 1
188192
_(span.name).must_equal 'HTTP GET'
189-
_(span.attributes['http.method']).must_equal 'GET'
190193
_(span.attributes['http.host']).must_equal 'example.com'
194+
_(span.attributes['http.method']).must_equal 'GET'
191195
end
192196

193197
it 'creates a span on connect for a non-ignored request' do
@@ -215,6 +219,8 @@
215219
end
216220

217221
it 'emits span on connect' do
222+
port = nil
223+
218224
TCPServer.open('localhost', 0) do |server|
219225
Thread.start do
220226
server.accept
@@ -231,8 +237,9 @@
231237
_(span.name).must_equal 'connect'
232238
_(span.attributes['net.peer.name']).must_equal('localhost')
233239
_(span.attributes['net.peer.port']).wont_be_nil
240+
_(span.attributes['net.peer.port']).must_equal(port)
234241

235-
assert_http_spans(target: '/example', exception: 'Excon::Error::Timeout')
242+
assert_http_spans(port: port, target: '/example', exception: 'Excon::Error::Timeout')
236243
end
237244

238245
it 'captures errors' do
@@ -248,7 +255,7 @@
248255
# Depending on the Ruby and Excon Version this will be a SocketError, Socket::ResolutionError or Resolv::ResolvError
249256
_(span_event.attributes['exception.type']).must_match(/(Socket|Resolv)/)
250257

251-
assert_http_spans(host: 'invalid.com', target: '/example')
258+
assert_http_spans(host: 'invalid.com', port: 99_999, target: '/example')
252259
end
253260

254261
it '[BUG] fails to emit an HTTP CONNECT span when connecting through an SSL proxy for an HTTP service' do
@@ -296,13 +303,14 @@
296303
end
297304
end
298305

299-
def assert_http_spans(scheme: 'http', host: 'localhost', target: '/', exception: nil)
306+
def assert_http_spans(scheme: 'http', host: 'localhost', port: nil, target: '/', exception: nil)
300307
exporter.finished_spans[1..].each do |http_span|
301308
_(http_span.name).must_equal 'HTTP GET'
309+
_(http_span.attributes['http.host']).must_equal host
302310
_(http_span.attributes['http.method']).must_equal 'GET'
303311
_(http_span.attributes['http.scheme']).must_equal scheme
304-
_(http_span.attributes['http.host']).must_equal host
305312
_(http_span.attributes['http.target']).must_equal target
313+
_(http_span.attributes['http.url']).must_equal "#{scheme}://#{host}#{port&.to_s&.prepend(':')}#{target}"
306314
_(http_span.status.code).must_equal(
307315
OpenTelemetry::Trace::Status::ERROR
308316
)

0 commit comments

Comments
 (0)