Skip to content

Commit bfcc9f3

Browse files
committed
squash: missed a spot
1 parent 8283d07 commit bfcc9f3

File tree

3 files changed

+48
-70
lines changed

3 files changed

+48
-70
lines changed

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

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#
55
# SPDX-License-Identifier: Apache-2.0
66

7+
require_relative '../../helpers'
8+
79
module OpenTelemetry
810
module Instrumentation
911
module Ethon
@@ -12,20 +14,11 @@ module Patches
1214
module Dup
1315
# Ethon::Easy patch for instrumentation
1416
module Easy
15-
ACTION_NAMES_TO_HTTP_METHODS = Hash.new do |h, k|
16-
# #to_s is required because user input could be symbol or string
17-
h[k] = k.to_s.upcase
18-
end
19-
HTTP_METHODS_TO_SPAN_NAMES = Hash.new do |h, k|
20-
h[k] = k.to_s
21-
h[k] = 'HTTP' if k == '_OTHER'
22-
end
23-
2417
# Constant for the HTTP status range
2518
HTTP_STATUS_SUCCESS_RANGE = (100..399)
2619

2720
def http_request(url, action_name, options = {})
28-
@otel_method = ACTION_NAMES_TO_HTTP_METHODS[action_name]
21+
@otel_method, @otel_original_method = Helpers.normalize_method(action_name)
2922
super
3023
end
3124

@@ -73,15 +66,17 @@ def reset
7366
ensure
7467
@otel_span = nil
7568
@otel_method = nil
69+
@otel_original_method = nil
7670
@otel_original_headers = nil
7771
end
7872

7973
def otel_before_request
80-
method = '_OTHER' # Could be GET or not HTTP at all
81-
method = @otel_method if instance_variable_defined?(:@otel_method) && !@otel_method.nil?
74+
method = @otel_method || '_OTHER'
75+
original_method = @otel_original_method if instance_variable_defined?(:@otel_original_method)
8276

83-
span_attrs = span_creation_attributes(method)
84-
span_name = determine_span_name(span_attrs, method)
77+
span_attrs = span_creation_attributes(method, original_method)
78+
# For dup semconv, when method is unknown use "HTTP" as span name
79+
span_name = method == '_OTHER' ? 'HTTP' : Helpers.format_span_name(span_attrs, method)
8580

8681
@otel_span = tracer.start_span(
8782
span_name,
@@ -102,12 +97,14 @@ def otel_span_started?
10297

10398
private
10499

105-
def span_creation_attributes(method)
106-
http_method = (method == '_OTHER' ? 'N/A' : method)
100+
def span_creation_attributes(method, original_method)
101+
# For dup semconv, use 'N/A' for old http.method when unknown
102+
http_method_attr = method == '_OTHER' ? 'N/A' : method
107103
instrumentation_attrs = {
108-
'http.method' => http_method,
104+
'http.method' => http_method_attr,
109105
'http.request.method' => method
110106
}
107+
instrumentation_attrs['http.request.method_original'] = original_method if original_method
111108

112109
uri = _otel_cleanse_uri(url)
113110
if uri
@@ -141,17 +138,6 @@ def _otel_cleanse_uri(url)
141138
def tracer
142139
Ethon::Instrumentation.instance.tracer
143140
end
144-
145-
def determine_span_name(attributes, method)
146-
# According to https://opentelemetry.io/docs/specs/semconv/http/http-spans/#name
147-
# Span name should be "{http.request.method} {url.template}" if template is available,
148-
# otherwise just "{http.request.method}"
149-
# For non-HTTP methods, return 'HTTP' as per the spec
150-
template = attributes['url.template']
151-
http_method = method == '_OTHER' ? 'HTTP' : method
152-
153-
template ? "#{http_method} #{template}" : http_method
154-
end
155141
end
156142
end
157143
end

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#
55
# SPDX-License-Identifier: Apache-2.0
66

7+
require_relative '../../helpers'
8+
79
module OpenTelemetry
810
module Instrumentation
911
module Ethon
@@ -12,17 +14,11 @@ module Patches
1214
module Old
1315
# Ethon::Easy patch for instrumentation
1416
module Easy
15-
ACTION_NAMES_TO_HTTP_METHODS = Hash.new do |h, k|
16-
# #to_s is required because user input could be symbol or string
17-
h[k] = k.to_s.upcase
18-
end
19-
HTTP_METHODS_TO_SPAN_NAMES = Hash.new { |h, k| h[k] = "HTTP #{k}" }
20-
2117
# Constant for the HTTP status range
2218
HTTP_STATUS_SUCCESS_RANGE = (100..399)
2319

2420
def http_request(url, action_name, options = {})
25-
@otel_method = ACTION_NAMES_TO_HTTP_METHODS[action_name]
21+
@otel_method, @otel_original_method = Helpers.normalize_method(action_name)
2622
super
2723
end
2824

@@ -69,16 +65,25 @@ def reset
6965
ensure
7066
@otel_span = nil
7167
@otel_method = nil
68+
@otel_original_method = nil
7269
@otel_original_headers = nil
7370
end
7471

7572
def otel_before_request
76-
method = 'N/A' # Could be GET or not HTTP at all
77-
method = @otel_method if instance_variable_defined?(:@otel_method) && !@otel_method.nil?
73+
method = @otel_method || '_OTHER'
74+
original_method = @otel_original_method if instance_variable_defined?(:@otel_original_method)
75+
76+
attributes = span_creation_attributes(method, original_method)
77+
# For old semconv, when method is unknown use "HTTP {method}" format
78+
span_name = if method == '_OTHER'
79+
# Use 'N/A' for old semconv attribute value
80+
"HTTP #{attributes['http.method']}"
81+
else
82+
Helpers.format_span_name(attributes, method)
83+
end
7884

79-
attributes = span_creation_attributes(method)
8085
@otel_span = tracer.start_span(
81-
determine_span_name(attributes, method),
86+
span_name,
8287
attributes: attributes,
8388
kind: :client
8489
)
@@ -96,10 +101,13 @@ def otel_span_started?
96101

97102
private
98103

99-
def span_creation_attributes(method)
104+
def span_creation_attributes(method, original_method)
105+
# For old semconv, use 'N/A' when method is unknown
106+
http_method_attr = method == '_OTHER' ? 'N/A' : method
100107
instrumentation_attrs = {
101-
'http.method' => method
108+
'http.method' => http_method_attr
102109
}
110+
instrumentation_attrs['http.request.method_original'] = original_method if original_method
103111

104112
uri = _otel_cleanse_uri(url)
105113
if uri
@@ -131,11 +139,6 @@ def _otel_cleanse_uri(url)
131139
def tracer
132140
Ethon::Instrumentation.instance.tracer
133141
end
134-
135-
def determine_span_name(attributes, http_method)
136-
template = attributes['url.template']
137-
template ? "#{http_method} #{template}" : "HTTP #{http_method}"
138-
end
139142
end
140143
end
141144
end

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

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#
55
# SPDX-License-Identifier: Apache-2.0
66

7+
require_relative '../../helpers'
8+
79
module OpenTelemetry
810
module Instrumentation
911
module Ethon
@@ -12,20 +14,11 @@ module Patches
1214
module Stable
1315
# Ethon::Easy patch for instrumentation
1416
module Easy
15-
ACTION_NAMES_TO_HTTP_METHODS = Hash.new do |h, k|
16-
# #to_s is required because user input could be symbol or string
17-
h[k] = k.to_s.upcase
18-
end
19-
HTTP_METHODS_TO_SPAN_NAMES = Hash.new do |h, k|
20-
h[k] = k.to_s
21-
h[k] = 'HTTP' if k == '_OTHER'
22-
end
23-
2417
# Constant for the HTTP status range
2518
HTTP_STATUS_SUCCESS_RANGE = (100..399)
2619

2720
def http_request(url, action_name, options = {})
28-
@otel_method = ACTION_NAMES_TO_HTTP_METHODS[action_name]
21+
@otel_method, @otel_original_method = Helpers.normalize_method(action_name)
2922
super
3023
end
3124

@@ -72,16 +65,20 @@ def reset
7265
ensure
7366
@otel_span = nil
7467
@otel_method = nil
68+
@otel_original_method = nil
7569
@otel_original_headers = nil
7670
end
7771

7872
def otel_before_request
79-
method = '_OTHER' # Could be GET or not HTTP at all
80-
method = @otel_method if instance_variable_defined?(:@otel_method) && !@otel_method.nil?
73+
method = @otel_method || '_OTHER'
74+
original_method = @otel_original_method if instance_variable_defined?(:@otel_original_method)
75+
76+
attributes = span_creation_attributes(method, original_method)
77+
# For stable semconv, when method is unknown use "HTTP" as span name
78+
span_name = method == '_OTHER' ? 'HTTP' : Helpers.format_span_name(attributes, method)
8179

82-
attributes = span_creation_attributes(method)
8380
@otel_span = tracer.start_span(
84-
determine_span_name(attributes, method),
81+
span_name,
8582
attributes: attributes,
8683
kind: :client
8784
)
@@ -99,10 +96,11 @@ def otel_span_started?
9996

10097
private
10198

102-
def span_creation_attributes(method)
99+
def span_creation_attributes(method, original_method)
103100
instrumentation_attrs = {
104101
'http.request.method' => method
105102
}
103+
instrumentation_attrs['http.request.method_original'] = original_method if original_method
106104

107105
uri = _otel_cleanse_uri(url)
108106
if uri
@@ -134,15 +132,6 @@ def _otel_cleanse_uri(url)
134132
def tracer
135133
Ethon::Instrumentation.instance.tracer
136134
end
137-
138-
def determine_span_name(attributes, http_method)
139-
template = attributes['url.template']
140-
if template
141-
"#{http_method} #{template}"
142-
else
143-
http_method == '_OTHER' ? 'HTTP' : http_method
144-
end
145-
end
146135
end
147136
end
148137
end

0 commit comments

Comments
 (0)