Skip to content

Commit 078f3fd

Browse files
committed
squash: refactoring more
1 parent 0efff9c commit 078f3fd

File tree

23 files changed

+307
-172
lines changed

23 files changed

+307
-172
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Easy
1818
HTTP_STATUS_SUCCESS_RANGE = (100..399)
1919

2020
def http_request(url, action_name, options = {})
21-
@otel_method = action_name.to_s.upcase
21+
@otel_method = action_name
2222
super
2323
end
2424

@@ -70,10 +70,7 @@ def reset
7070
end
7171

7272
def otel_before_request
73-
method = '_OTHER' # Could be GET or not HTTP at all
74-
method = @otel_method if instance_variable_defined?(:@otel_method) && !@otel_method.nil?
75-
76-
normalized_method, original_method = HttpHelper.normalize_method(method)
73+
normalized_method, original_method = HttpHelper.normalize_method(@otel_method)
7774
span_name = HttpHelper.span_name_for_stable(normalized_method)
7875

7976
@otel_span = tracer.start_span(
@@ -96,9 +93,8 @@ def otel_span_started?
9693
private
9794

9895
def span_creation_attributes(normalized_method, original_method)
99-
http_method = (normalized_method == '_OTHER' ? 'N/A' : normalized_method)
10096
instrumentation_attrs = {
101-
'http.method' => http_method,
97+
'http.method' => normalized_method,
10298
'http.request.method' => normalized_method
10399
}
104100
instrumentation_attrs['http.request.method_original'] = original_method if original_method

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

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,36 @@ module Ethon
1010
module Patches
1111
# Helper module for HTTP method normalization
1212
module HttpHelper
13-
KNOWN_METHODS = %w[CONNECT DELETE GET HEAD OPTIONS PATCH POST PUT TRACE].freeze
13+
# Pre-computed mapping to avoid string allocations during normalization
14+
METHOD_CACHE = {
15+
'CONNECT' => 'CONNECT',
16+
'DELETE' => 'DELETE',
17+
'GET' => 'GET',
18+
'HEAD' => 'HEAD',
19+
'OPTIONS' => 'OPTIONS',
20+
'PATCH' => 'PATCH',
21+
'POST' => 'POST',
22+
'PUT' => 'PUT',
23+
'TRACE' => 'TRACE',
24+
'connect' => 'CONNECT',
25+
'delete' => 'DELETE',
26+
'get' => 'GET',
27+
'head' => 'HEAD',
28+
'options' => 'OPTIONS',
29+
'patch' => 'PATCH',
30+
'post' => 'POST',
31+
'put' => 'PUT',
32+
'trace' => 'TRACE',
33+
:connect => 'CONNECT',
34+
:delete => 'DELETE',
35+
:get => 'GET',
36+
:head => 'HEAD',
37+
:options => 'OPTIONS',
38+
:patch => 'PATCH',
39+
:post => 'POST',
40+
:put => 'PUT',
41+
:trace => 'TRACE'
42+
}.freeze
1443

1544
# Pre-computed span names for old semantic conventions to avoid allocations
1645
OLD_SPAN_NAMES = {
@@ -25,13 +54,19 @@ module HttpHelper
2554
'TRACE' => 'HTTP TRACE'
2655
}.freeze
2756

57+
private_constant :METHOD_CACHE, :OLD_SPAN_NAMES
58+
59+
# Normalizes an HTTP method to uppercase per OpenTelemetry semantic conventions.
60+
# @param method [String, Symbol] The HTTP method to normalize
61+
# @return [Array<String, String|nil>] A tuple of [normalized_method, original_method]
62+
# where normalized_method is either a known method or '_OTHER',
63+
# and original_method is the original value if it was normalized to '_OTHER', or nil
2864
def self.normalize_method(method)
29-
normalized = method.is_a?(String) ? method.upcase : method.to_s.upcase
30-
if KNOWN_METHODS.include?(normalized)
31-
[normalized, nil]
32-
else
33-
['_OTHER', normalized]
34-
end
65+
normalized = METHOD_CACHE[method]
66+
return [normalized, nil] if normalized
67+
68+
# Mixed case or unknown methods are treated as '_OTHER'
69+
['_OTHER', method.to_s]
3570
end
3671

3772
# Generates span name for stable semantic conventions

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Easy
1818
HTTP_STATUS_SUCCESS_RANGE = (100..399)
1919

2020
def http_request(url, action_name, options = {})
21-
@otel_method = action_name.to_s.upcase
21+
@otel_method = action_name
2222
super
2323
end
2424

@@ -69,10 +69,7 @@ def reset
6969
end
7070

7171
def otel_before_request
72-
method = '_OTHER' # Could be GET or not HTTP at all
73-
method = @otel_method if instance_variable_defined?(:@otel_method) && !@otel_method.nil?
74-
75-
normalized_method, _original_method = HttpHelper.normalize_method(method)
72+
normalized_method, _original_method = HttpHelper.normalize_method(@otel_method)
7673
span_name = HttpHelper.span_name_for_old(normalized_method)
7774

7875
@otel_span = tracer.start_span(

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Easy
1818
HTTP_STATUS_SUCCESS_RANGE = (100..399)
1919

2020
def http_request(url, action_name, options = {})
21-
@otel_method = action_name.to_s.upcase
21+
@otel_method = action_name
2222
super
2323
end
2424

@@ -69,10 +69,7 @@ def reset
6969
end
7070

7171
def otel_before_request
72-
method = '_OTHER' # Could be GET or not HTTP at all
73-
method = @otel_method if instance_variable_defined?(:@otel_method) && !@otel_method.nil?
74-
75-
normalized_method, original_method = HttpHelper.normalize_method(method)
72+
normalized_method, original_method = HttpHelper.normalize_method(@otel_method)
7673
span_name = HttpHelper.span_name_for_stable(normalized_method)
7774

7875
@otel_span = tracer.start_span(

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
easy.perform
7272

7373
_(span.name).must_equal 'HTTP'
74-
_(span.attributes['http.method']).must_equal 'N/A'
74+
_(span.attributes['http.method']).must_equal '_OTHER'
7575
_(span.attributes['http.status_code']).must_be_nil
7676
_(span.attributes['http.url']).must_equal 'http://example.com/test'
7777
_(span.attributes['net.peer.name']).must_equal 'example.com'
@@ -92,7 +92,7 @@
9292
# NOTE: check the finished spans since we expect to have closed it
9393
span = exporter.finished_spans.first
9494
_(span.name).must_equal 'HTTP'
95-
_(span.attributes['http.method']).must_equal 'N/A'
95+
_(span.attributes['http.method']).must_equal '_OTHER'
9696
_(span.attributes['http.status_code']).must_be_nil
9797
_(span.attributes['http.url']).must_equal 'http://example.com/test'
9898
_(span.attributes['http.request.method']).must_equal '_OTHER'
@@ -121,7 +121,7 @@ def stub_response(options)
121121
it 'when response is successful' do
122122
stub_response(response_code: 200) do
123123
_(span.name).must_equal 'HTTP'
124-
_(span.attributes['http.method']).must_equal 'N/A'
124+
_(span.attributes['http.method']).must_equal '_OTHER'
125125
_(span.attributes['http.request.method']).must_equal '_OTHER'
126126
_(span.attributes['http.status_code']).must_equal 200
127127
_(span.attributes['http.response.status_code']).must_equal 200
@@ -137,7 +137,7 @@ def stub_response(options)
137137
it 'when response is not successful' do
138138
stub_response(response_code: 500) do
139139
_(span.name).must_equal 'HTTP'
140-
_(span.attributes['http.method']).must_equal 'N/A'
140+
_(span.attributes['http.method']).must_equal '_OTHER'
141141
_(span.attributes['http.request.method']).must_equal '_OTHER'
142142
_(span.attributes['http.status_code']).must_equal 500
143143
_(span.attributes['http.response.status_code']).must_equal 500
@@ -153,7 +153,7 @@ def stub_response(options)
153153
it 'when request times out' do
154154
stub_response(response_code: 0, return_code: :operation_timedout) do
155155
_(span.name).must_equal 'HTTP'
156-
_(span.attributes['http.method']).must_equal 'N/A'
156+
_(span.attributes['http.method']).must_equal '_OTHER'
157157
_(span.attributes['http.request.method']).must_equal '_OTHER'
158158
_(span.attributes['http.status_code']).must_be_nil
159159
_(span.attributes['http.response.status_code']).must_be_nil
@@ -232,7 +232,7 @@ def stub_response(options)
232232
end
233233

234234
it 'cleans up @otel_method' do
235-
_(easy.instance_eval { @otel_method }).must_equal 'PUT'
235+
_(easy.instance_eval { @otel_method }).must_equal :put
236236

237237
easy.reset
238238

@@ -274,10 +274,10 @@ def stub_response(options)
274274
stub_response(response_code: 200) do
275275
_(exporter.finished_spans.size).must_equal 1
276276
_(span.name).must_equal 'HTTP'
277-
_(span.attributes['http.method']).must_equal 'N/A'
277+
_(span.attributes['http.method']).must_equal '_OTHER'
278278
_(span.attributes['http.url']).must_equal 'http://example.com/purge'
279279
_(span.attributes['http.request.method']).must_equal '_OTHER'
280-
_(span.attributes['http.request.method_original']).must_equal 'PURGE'
280+
_(span.attributes['http.request.method_original']).must_equal 'purge'
281281
_(span.attributes['url.full']).must_equal 'http://example.com/purge'
282282
end
283283
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def stub_response(options)
214214
end
215215

216216
it 'cleans up @otel_method' do
217-
_(easy.instance_eval { @otel_method }).must_equal 'PUT'
217+
_(easy.instance_eval { @otel_method }).must_equal :put
218218

219219
easy.reset
220220

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def stub_response(options)
215215
end
216216

217217
it 'cleans up @otel_method' do
218-
_(easy.instance_eval { @otel_method }).must_equal 'PUT'
218+
_(easy.instance_eval { @otel_method }).must_equal :put
219219

220220
easy.reset
221221

@@ -258,7 +258,7 @@ def stub_response(options)
258258
_(exporter.finished_spans.size).must_equal 1
259259
_(span.name).must_equal 'HTTP'
260260
_(span.attributes['http.request.method']).must_equal '_OTHER'
261-
_(span.attributes['http.request.method_original']).must_equal 'PURGE'
261+
_(span.attributes['http.request.method_original']).must_equal 'purge'
262262
_(span.attributes['url.full']).must_equal 'http://example.com/purge'
263263
end
264264
end

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

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,36 @@ module Excon
1010
module Middlewares
1111
# Utility module for HTTP-related helper methods
1212
module HttpHelper
13-
# Standard HTTP methods as defined in the OpenTelemetry semantic conventions
14-
# https://opentelemetry.io/docs/specs/semconv/http/http-spans/
15-
KNOWN_METHODS = %w[CONNECT DELETE GET HEAD OPTIONS PATCH POST PUT TRACE].freeze
13+
# Pre-computed mapping to avoid string allocations during normalization
14+
METHOD_CACHE = {
15+
'CONNECT' => 'CONNECT',
16+
'DELETE' => 'DELETE',
17+
'GET' => 'GET',
18+
'HEAD' => 'HEAD',
19+
'OPTIONS' => 'OPTIONS',
20+
'PATCH' => 'PATCH',
21+
'POST' => 'POST',
22+
'PUT' => 'PUT',
23+
'TRACE' => 'TRACE',
24+
'connect' => 'CONNECT',
25+
'delete' => 'DELETE',
26+
'get' => 'GET',
27+
'head' => 'HEAD',
28+
'options' => 'OPTIONS',
29+
'patch' => 'PATCH',
30+
'post' => 'POST',
31+
'put' => 'PUT',
32+
'trace' => 'TRACE',
33+
:connect => 'CONNECT',
34+
:delete => 'DELETE',
35+
:get => 'GET',
36+
:head => 'HEAD',
37+
:options => 'OPTIONS',
38+
:patch => 'PATCH',
39+
:post => 'POST',
40+
:put => 'PUT',
41+
:trace => 'TRACE'
42+
}.freeze
1643

1744
# Pre-computed span names for old semantic conventions to avoid allocations
1845
OLD_SPAN_NAMES = {
@@ -27,20 +54,19 @@ module HttpHelper
2754
'TRACE' => 'HTTP TRACE'
2855
}.freeze
2956

30-
# Normalizes an HTTP method according to OpenTelemetry semantic conventions
57+
private_constant :METHOD_CACHE, :OLD_SPAN_NAMES
58+
59+
# Normalizes an HTTP method to uppercase per OpenTelemetry semantic conventions.
3160
# @param method [String, Symbol] The HTTP method to normalize
3261
# @return [Array<String, String|nil>] A tuple of [normalized_method, original_method]
33-
# - For known methods: returns [uppercase_method, nil]
34-
# - For unknown methods: returns ['_OTHER', uppercase_original_method]
62+
# where normalized_method is either a known method or '_OTHER',
63+
# and original_method is the original value if it was normalized to '_OTHER', or nil
3564
def self.normalize_method(method)
36-
return [nil, nil] if method.nil?
37-
38-
normalized = method.is_a?(String) ? method.upcase : method.to_s.upcase
39-
if KNOWN_METHODS.include?(normalized)
40-
[normalized, nil]
41-
else
42-
['_OTHER', normalized]
43-
end
65+
normalized = METHOD_CACHE[method]
66+
return [normalized, nil] if normalized
67+
68+
# Mixed case or unknown methods are treated as '_OTHER'
69+
['_OTHER', method.to_s]
4470
end
4571

4672
# Generates span name for stable semantic conventions

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,11 @@ module Dup
1414
# TracerMiddleware propagates context and instruments Faraday requests
1515
# by way of its middleware system
1616
class TracerMiddleware < ::Faraday::Middleware
17-
HTTP_METHODS_SYMBOL_TO_STRING = {
18-
connect: 'CONNECT',
19-
delete: 'DELETE',
20-
get: 'GET',
21-
head: 'HEAD',
22-
options: 'OPTIONS',
23-
patch: 'PATCH',
24-
post: 'POST',
25-
put: 'PUT',
26-
trace: 'TRACE'
27-
}.freeze
28-
2917
# Constant for the HTTP status range
3018
HTTP_STATUS_SUCCESS_RANGE = (100..399)
3119

3220
def call(env)
33-
http_method = HTTP_METHODS_SYMBOL_TO_STRING[env.method]
34-
normalized_method, original_method = HttpHelper.normalize_method(http_method || env.method)
21+
normalized_method, original_method = HttpHelper.normalize_method(env.method)
3522

3623
# Per semantic conventions, span name uses 'HTTP' when method is unknown
3724
span_name = HttpHelper.span_name_for_stable(normalized_method)

0 commit comments

Comments
 (0)