Skip to content

Commit bdf3782

Browse files
committed
squash: what
1 parent 32cec21 commit bdf3782

File tree

30 files changed

+320
-135
lines changed

30 files changed

+320
-135
lines changed

instrumentation/ethon/lib/opentelemetry/instrumentation/ethon/helpers.rb

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ module Ethon
1111
module Helpers
1212
extend self
1313

14-
# Known HTTP methods as defined in RFC9110, RFC5789, and httpbis-safe-method-w-body
15-
KNOWN_METHODS = %w[
14+
# Default known HTTP methods as defined in RFC9110, RFC5789, and httpbis-safe-method-w-body
15+
DEFAULT_KNOWN_METHODS = %w[
1616
CONNECT
1717
DELETE
1818
GET
@@ -25,18 +25,39 @@ module Helpers
2525
QUERY
2626
].freeze
2727

28-
private_constant :KNOWN_METHODS
28+
private_constant :DEFAULT_KNOWN_METHODS
29+
30+
# Returns the list of known HTTP methods, checking the environment variable first
31+
#
32+
# @return [Array<String>] List of known HTTP methods in uppercase
33+
# @api private
34+
def known_methods
35+
@known_methods ||= if (env_methods = ENV.fetch('OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS', nil))
36+
env_methods.split(',').map(&:strip).map(&:upcase).freeze
37+
else
38+
DEFAULT_KNOWN_METHODS
39+
end
40+
end
2941

3042
# Normalizes an HTTP method to match OpenTelemetry semantic conventions.
43+
# Returns both the normalized method and the original if they differ.
3144
#
3245
# @param method [String, Symbol] The HTTP method to normalize
33-
# @return [String] The normalized method name (uppercase if known, '_OTHER' if unknown)
46+
# @return [Array<String, String|nil>] A tuple of [normalized_method, original_method]
47+
# where original_method is nil if it matches the normalized method
3448
# @api private
3549
def normalize_method(method)
36-
return '_OTHER' if method.nil? || method.to_s.empty?
50+
return ['_OTHER', nil] if method.nil? || method.to_s.empty?
51+
52+
original = method.to_s
53+
normalized = original.upcase
3754

38-
normalized = method.to_s.upcase
39-
KNOWN_METHODS.include?(normalized) ? normalized : '_OTHER'
55+
if known_methods.include?(normalized)
56+
# Return original as nil if it already matches the normalized form
57+
[normalized, (original == normalized ? nil : original)]
58+
else
59+
['_OTHER', original]
60+
end
4061
end
4162

4263
# Formats the span name based on the HTTP method and URL template if available

instrumentation/excon/lib/opentelemetry/instrumentation/excon/helpers.rb

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ module Excon
1111
module Helpers
1212
extend self
1313

14-
# Known HTTP methods as defined in RFC9110, RFC5789, and httpbis-safe-method-w-body
15-
KNOWN_METHODS = %w[
14+
# Default known HTTP methods as defined in RFC9110, RFC5789, and httpbis-safe-method-w-body
15+
DEFAULT_KNOWN_METHODS = %w[
1616
CONNECT
1717
DELETE
1818
GET
@@ -25,18 +25,39 @@ module Helpers
2525
QUERY
2626
].freeze
2727

28-
private_constant :KNOWN_METHODS
28+
private_constant :DEFAULT_KNOWN_METHODS
29+
30+
# Returns the list of known HTTP methods, checking the environment variable first
31+
#
32+
# @return [Array<String>] List of known HTTP methods in uppercase
33+
# @api private
34+
def known_methods
35+
@known_methods ||= if (env_methods = ENV.fetch('OTEL_INSTRUMENTATION_HTTP_KNOWN_METHODS', nil))
36+
env_methods.split(',').map { |v| v.strip.upcase }.freeze
37+
else
38+
DEFAULT_KNOWN_METHODS
39+
end
40+
end
2941

3042
# Normalizes an HTTP method to match OpenTelemetry semantic conventions.
43+
# Returns both the normalized method and the original if they differ.
3144
#
3245
# @param method [String, Symbol] The HTTP method to normalize
33-
# @return [String] The normalized method name (uppercase if known, '_OTHER' if unknown)
46+
# @return [Array<String, String|nil>] A tuple of [normalized_method, original_method]
47+
# where original_method is nil if it matches the normalized method
3448
# @api private
3549
def normalize_method(method)
36-
return '_OTHER' if method.nil? || method.to_s.empty?
50+
return ['_OTHER', nil] if method.nil? || method.to_s.empty?
51+
52+
original = method.to_s
53+
normalized = original.upcase
3754

38-
normalized = method.to_s.upcase
39-
KNOWN_METHODS.include?(normalized) ? normalized : '_OTHER'
55+
if known_methods.include?(normalized)
56+
# Return original as nil if it already matches the normalized form
57+
[normalized, (original == normalized ? nil : original)]
58+
else
59+
['_OTHER', original]
60+
end
4061
end
4162

4263
# Formats the span name based on the HTTP method and URL template if available

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TracerMiddleware < ::Excon::Middleware::Base
1919
def request_call(datum)
2020
return @stack.request_call(datum) if untraced?(datum)
2121

22-
http_method = Helpers.normalize_method(datum[:method])
22+
http_method, original_method = Helpers.normalize_method(datum[:method])
2323
cleansed_url = OpenTelemetry::Common::Utilities.cleanse_url(::Excon::Utils.request_uri(datum))
2424
attributes = {
2525
OpenTelemetry::SemanticConventions::Trace::HTTP_HOST => datum[:host],
@@ -36,6 +36,7 @@ def request_call(datum)
3636
'server.address' => datum[:hostname],
3737
'server.port' => datum[:port]
3838
}
39+
attributes['http.request.method_original'] = original_method if original_method
3940
attributes['url.query'] = datum[:query] if datum[:query]
4041
peer_service = Excon::Instrumentation.instance.config[:peer_service]
4142
attributes[OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE] = peer_service if peer_service

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TracerMiddleware < ::Excon::Middleware::Base
1919
def request_call(datum)
2020
return @stack.request_call(datum) if untraced?(datum)
2121

22-
http_method = Helpers.normalize_method(datum[:method])
22+
http_method, original_method = Helpers.normalize_method(datum[:method])
2323
attributes = {
2424
OpenTelemetry::SemanticConventions::Trace::HTTP_HOST => datum[:host],
2525
OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => http_method,
@@ -29,11 +29,11 @@ def request_call(datum)
2929
OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => datum[:hostname],
3030
OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => datum[:port]
3131
}
32+
attributes['http.request.method_original'] = original_method if original_method
3233
peer_service = Excon::Instrumentation.instance.config[:peer_service]
3334
attributes[OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE] = peer_service if peer_service
3435
attributes.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)
35-
# Old semconv used "HTTP {method}" format for span names
36-
span_name = attributes['url.template'] ? "#{http_method} #{attributes['url.template']}" : "HTTP #{http_method}"
36+
span_name = Helpers.format_span_name(attributes, http_method)
3737
span = tracer.start_span(span_name, attributes: attributes, kind: :client)
3838
ctx = OpenTelemetry::Trace.context_with_span(span)
3939
datum[:otel_span] = span

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TracerMiddleware < ::Excon::Middleware::Base
1919
def request_call(datum)
2020
return @stack.request_call(datum) if untraced?(datum)
2121

22-
http_method = Helpers.normalize_method(datum[:method])
22+
http_method, original_method = Helpers.normalize_method(datum[:method])
2323
attributes = {
2424
'http.request.method' => http_method,
2525
'url.scheme' => datum[:scheme],
@@ -28,6 +28,7 @@ def request_call(datum)
2828
'server.address' => datum[:hostname],
2929
'server.port' => datum[:port]
3030
}
31+
attributes['http.request.method_original'] = original_method if original_method
3132
attributes['url.query'] = datum[:query] if datum[:query]
3233
peer_service = Excon::Instrumentation.instance.config[:peer_service]
3334
attributes[OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE] = peer_service if peer_service

instrumentation/excon/lib/opentelemetry/instrumentation/excon/patches/dup/socket.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def connect
3535
span_name = 'CONNECT'
3636
span_kind = :client
3737
else
38-
span_name = 'connect'
38+
span_name = 'tcp.connect'
3939
span_kind = :internal
4040
end
4141

instrumentation/excon/lib/opentelemetry/instrumentation/excon/patches/old/socket.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ def connect
2727
attributes = { OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => conn_address, OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => conn_port }.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)
2828

2929
if is_a?(::Excon::SSLSocket) && @data[:proxy]
30-
span_name = 'HTTP CONNECT'
30+
span_name = 'CONNECT'
3131
span_kind = :client
3232
else
33-
span_name = 'connect'
33+
span_name = 'tcp.connect'
3434
span_kind = :internal
3535
end
3636

instrumentation/excon/lib/opentelemetry/instrumentation/excon/patches/stable/socket.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def connect
3030
span_name = 'CONNECT'
3131
span_kind = :client
3232
else
33-
span_name = 'connect'
33+
span_name = 'tcp.connect'
3434
span_kind = :internal
3535
end
3636

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@
257257
Excon::Socket.new(hostname: uri.host, port: uri.port)
258258

259259
_(exporter.finished_spans.size).must_equal 1
260-
_(span.name).must_equal('connect')
260+
_(span.name).must_equal('tcp.connect')
261261
_(span.kind).must_equal(:internal)
262262
_(span.attributes['net.peer.name']).must_equal('example.com')
263263
_(span.attributes['net.peer.port']).must_equal(80)
@@ -293,7 +293,7 @@
293293
end
294294

295295
_(exporter.finished_spans.size).must_equal(3)
296-
_(span.name).must_equal 'connect'
296+
_(span.name).must_equal 'tcp.connect'
297297
_(span.attributes['net.peer.name']).must_equal('localhost')
298298
_(span.attributes['net.peer.port']).wont_be_nil
299299
_(span.attributes['net.peer.port']).must_equal(port)
@@ -309,7 +309,7 @@
309309
_(-> { Excon.get('http://invalid.com:99999/example') }).must_raise
310310

311311
_(exporter.finished_spans.size).must_equal(3)
312-
_(span.name).must_equal 'connect'
312+
_(span.name).must_equal 'tcp.connect'
313313
_(span.attributes['net.peer.name']).must_equal('invalid.com')
314314
_(span.attributes['net.peer.port']).must_equal(99_999)
315315
# stable semconv
@@ -328,7 +328,7 @@
328328
_(-> { Excon.get('http://localhost/', proxy: 'https://proxy_user:proxy_pass@localhost') }).must_raise(Excon::Error::Socket)
329329

330330
_(exporter.finished_spans.size).must_equal(3)
331-
_(span.name).must_equal 'connect'
331+
_(span.name).must_equal 'tcp.connect'
332332
_(span.kind).must_equal(:internal)
333333
_(span.attributes['net.peer.name']).must_equal('localhost')
334334
_(span.attributes['net.peer.port']).must_equal(443)
@@ -358,7 +358,7 @@
358358
_(-> { Excon.get('http://localhost', proxy: 'https://proxy_user:proxy_pass@localhost') }).must_raise(Excon::Error::Socket)
359359

360360
_(exporter.finished_spans.size).must_equal(3)
361-
_(span.name).must_equal 'connect'
361+
_(span.name).must_equal 'tcp.connect'
362362
_(span.kind).must_equal(:internal)
363363
_(span.attributes['net.peer.name']).must_equal('localhost')
364364
_(span.attributes['net.peer.port']).must_equal(443)

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
Excon.get('http://example.com/success')
5050

5151
_(exporter.finished_spans.size).must_equal 1
52-
_(span.name).must_equal 'HTTP GET'
52+
_(span.name).must_equal 'GET'
5353
_(span.attributes['http.host']).must_equal 'example.com'
5454
_(span.attributes['http.method']).must_equal 'GET'
5555
_(span.attributes['http.scheme']).must_equal 'http'
@@ -73,7 +73,7 @@
7373
Excon.get('http://example.com/failure')
7474

7575
_(exporter.finished_spans.size).must_equal 1
76-
_(span.name).must_equal 'HTTP GET'
76+
_(span.name).must_equal 'GET'
7777
_(span.attributes['http.host']).must_equal 'example.com'
7878
_(span.attributes['http.method']).must_equal 'GET'
7979
_(span.attributes['http.scheme']).must_equal 'http'
@@ -93,7 +93,7 @@
9393
end.must_raise Excon::Error::Timeout
9494

9595
_(exporter.finished_spans.size).must_equal 1
96-
_(span.name).must_equal 'HTTP GET'
96+
_(span.name).must_equal 'GET'
9797
_(span.attributes['http.host']).must_equal 'example.com'
9898
_(span.attributes['http.method']).must_equal 'GET'
9999
_(span.attributes['http.scheme']).must_equal 'http'
@@ -123,7 +123,7 @@
123123
end
124124

125125
_(exporter.finished_spans.size).must_equal 1
126-
_(span.name).must_equal 'HTTP GET'
126+
_(span.name).must_equal 'GET'
127127
_(span.attributes['http.host']).must_equal 'example.com'
128128
_(span.attributes['http.method']).must_equal 'OVERRIDE'
129129
_(span.attributes['http.scheme']).must_equal 'http'
@@ -204,7 +204,7 @@
204204
Excon.get('http://example.com/body')
205205

206206
_(exporter.finished_spans.size).must_equal 1
207-
_(span.name).must_equal 'HTTP GET'
207+
_(span.name).must_equal 'GET'
208208
_(span.attributes['http.host']).must_equal 'example.com'
209209
_(span.attributes['http.method']).must_equal 'GET'
210210
end
@@ -215,7 +215,7 @@
215215
Excon::Socket.new(hostname: uri.host, port: uri.port)
216216

217217
_(exporter.finished_spans.size).must_equal 1
218-
_(span.name).must_equal('connect')
218+
_(span.name).must_equal('tcp.connect')
219219
_(span.kind).must_equal(:internal)
220220
_(span.attributes['net.peer.name']).must_equal('example.com')
221221
_(span.attributes['net.peer.port']).must_equal(80)
@@ -249,7 +249,7 @@
249249
end
250250

251251
_(exporter.finished_spans.size).must_equal(3)
252-
_(span.name).must_equal 'connect'
252+
_(span.name).must_equal 'tcp.connect'
253253
_(span.attributes['net.peer.name']).must_equal('localhost')
254254
_(span.attributes['net.peer.port']).wont_be_nil
255255
_(span.attributes['net.peer.port']).must_equal(port)
@@ -261,7 +261,7 @@
261261
_(-> { Excon.get('http://invalid.com:99999/example') }).must_raise
262262

263263
_(exporter.finished_spans.size).must_equal(3)
264-
_(span.name).must_equal 'connect'
264+
_(span.name).must_equal 'tcp.connect'
265265
_(span.attributes['net.peer.name']).must_equal('invalid.com')
266266
_(span.attributes['net.peer.port']).must_equal(99_999)
267267

@@ -277,7 +277,7 @@
277277
_(-> { Excon.get('http://localhost/', proxy: 'https://proxy_user:proxy_pass@localhost') }).must_raise(Excon::Error::Socket)
278278

279279
_(exporter.finished_spans.size).must_equal(3)
280-
_(span.name).must_equal 'connect'
280+
_(span.name).must_equal 'tcp.connect'
281281
_(span.kind).must_equal(:internal)
282282
_(span.attributes['net.peer.name']).must_equal('localhost')
283283
_(span.attributes['net.peer.port']).must_equal(443)
@@ -289,7 +289,7 @@
289289
_(-> { Excon.get('https://localhost/', proxy: 'https://proxy_user:proxy_pass@localhost') }).must_raise(Excon::Error::Socket)
290290

291291
_(exporter.finished_spans.size).must_equal(3)
292-
_(span.name).must_equal 'HTTP CONNECT'
292+
_(span.name).must_equal 'CONNECT'
293293
_(span.kind).must_equal(:client)
294294
_(span.attributes['net.peer.name']).must_equal('localhost')
295295
_(span.attributes['net.peer.port']).must_equal(443)
@@ -301,7 +301,7 @@
301301
_(-> { Excon.get('http://localhost', proxy: 'https://proxy_user:proxy_pass@localhost') }).must_raise(Excon::Error::Socket)
302302

303303
_(exporter.finished_spans.size).must_equal(3)
304-
_(span.name).must_equal 'connect'
304+
_(span.name).must_equal 'tcp.connect'
305305
_(span.kind).must_equal(:internal)
306306
_(span.attributes['net.peer.name']).must_equal('localhost')
307307
_(span.attributes['net.peer.port']).must_equal(443)
@@ -320,7 +320,7 @@
320320

321321
def assert_http_spans(scheme: 'http', host: 'localhost', port: nil, target: '/', exception: nil)
322322
exporter.finished_spans[1..].each do |http_span|
323-
_(http_span.name).must_equal 'HTTP GET'
323+
_(http_span.name).must_equal 'GET'
324324
_(http_span.attributes['http.host']).must_equal host
325325
_(http_span.attributes['http.method']).must_equal 'GET'
326326
_(http_span.attributes['http.scheme']).must_equal scheme

0 commit comments

Comments
 (0)