Skip to content

Commit 14b1c09

Browse files
committed
replace conditional logic with polymorpism
1 parent a6341e9 commit 14b1c09

File tree

28 files changed

+406
-168
lines changed

28 files changed

+406
-168
lines changed

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

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,32 @@ module HttpHelper
4646

4747
private_constant :METHOD_CACHE
4848

49-
# Prepares all span data in a single call
49+
# Prepares span data using old semantic conventions
5050
# @param method [String, Symbol] The HTTP method
51-
# @param semconv [Symbol] Semantic convention to use (:old, :stable, or :dup)
5251
# @return [SpanCreationAttributes] struct containing span_name and attributes hash
53-
def self.span_attrs_for(method, semconv: :old)
52+
def self.span_attrs_for_old(method)
53+
client_context_attrs = OpenTelemetry::Common::HTTP::ClientContext.attributes
54+
normalized = METHOD_CACHE[method]
55+
attributes = client_context_attrs.dup
56+
57+
# Determine base span name and method value
58+
if normalized
59+
span_name = normalized
60+
method_value = normalized
61+
else
62+
span_name = 'HTTP'
63+
method_value = '_OTHER'
64+
end
65+
66+
attributes['http.method'] ||= method_value
67+
68+
SpanCreationAttributes.new(span_name: span_name, attributes: attributes)
69+
end
70+
71+
# Prepares span data using stable semantic conventions
72+
# @param method [String, Symbol] The HTTP method
73+
# @return [SpanCreationAttributes] struct containing span_name and attributes hash
74+
def self.span_attrs_for_stable(method)
5475
client_context_attrs = OpenTelemetry::Common::HTTP::ClientContext.attributes
5576
url_template = client_context_attrs['url.template']
5677
normalized = METHOD_CACHE[method]
@@ -60,32 +81,45 @@ def self.span_attrs_for(method, semconv: :old)
6081
if normalized
6182
base_name = normalized
6283
method_value = normalized
84+
original = nil
6385
else
6486
base_name = 'HTTP'
6587
method_value = '_OTHER'
6688
original = method.to_s
6789
end
6890

69-
# Set span name based on semantic convention
70-
span_name = if semconv == :old
71-
base_name
72-
else
73-
(url_template ? "#{base_name} #{url_template}" : base_name)
74-
end
75-
76-
# Set attributes based on semantic convention
77-
case semconv
78-
when :old
79-
attributes['http.method'] ||= method_value
80-
when :stable
81-
attributes['http.request.method'] ||= method_value
82-
attributes['http.request.method_original'] ||= original if original
83-
when :dup
84-
attributes['http.method'] ||= method_value
85-
attributes['http.request.method'] ||= method_value
86-
attributes['http.request.method_original'] ||= original if original
91+
span_name = url_template ? "#{base_name} #{url_template}" : base_name
92+
attributes['http.request.method'] ||= method_value
93+
attributes['http.request.method_original'] ||= original if original
94+
95+
SpanCreationAttributes.new(span_name: span_name, attributes: attributes)
96+
end
97+
98+
# Prepares span data using both old and stable semantic conventions
99+
# @param method [String, Symbol] The HTTP method
100+
# @return [SpanCreationAttributes] struct containing span_name and attributes hash
101+
def self.span_attrs_for_dup(method)
102+
client_context_attrs = OpenTelemetry::Common::HTTP::ClientContext.attributes
103+
url_template = client_context_attrs['url.template']
104+
normalized = METHOD_CACHE[method]
105+
attributes = client_context_attrs.dup
106+
107+
# Determine base span name and method value
108+
if normalized
109+
base_name = normalized
110+
method_value = normalized
111+
original = nil
112+
else
113+
base_name = 'HTTP'
114+
method_value = '_OTHER'
115+
original = method.to_s
87116
end
88117

118+
span_name = url_template ? "#{base_name} #{url_template}" : base_name
119+
attributes['http.method'] ||= method_value
120+
attributes['http.request.method'] ||= method_value
121+
attributes['http.request.method_original'] ||= original if original
122+
89123
SpanCreationAttributes.new(span_name: span_name, attributes: attributes)
90124
end
91125
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def reset
6868
end
6969

7070
def otel_before_request
71-
span_data = HttpHelper.span_attrs_for(@otel_method, semconv: :dup)
71+
span_data = HttpHelper.span_attrs_for_dup(@otel_method)
7272

7373
@otel_span = tracer.start_span(
7474
span_data.span_name,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def reset
6767
end
6868

6969
def otel_before_request
70-
span_data = HttpHelper.span_attrs_for(@otel_method, semconv: :old)
70+
span_data = HttpHelper.span_attrs_for_old(@otel_method)
7171

7272
@otel_span = tracer.start_span(
7373
span_data.span_name,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def reset
6767
end
6868

6969
def otel_before_request
70-
span_data = HttpHelper.span_attrs_for(@otel_method, semconv: :stable)
70+
span_data = HttpHelper.span_attrs_for_stable(@otel_method)
7171

7272
@otel_span = tracer.start_span(
7373
span_data.span_name,

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

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,32 @@ module HttpHelper
4646

4747
private_constant :METHOD_CACHE
4848

49-
# Prepares all span data in a single call, including ClientContext attributes
49+
# Prepares span data using old semantic conventions
5050
# @param method [String, Symbol] The HTTP method
51-
# @param semconv [Symbol] Semantic convention to use (:old, :stable, or :dup)
5251
# @return [SpanCreationAttributes] struct containing span_name and attributes hash
53-
def self.span_attrs_for(method, semconv: :old)
52+
def self.span_attrs_for_old(method)
53+
client_context_attrs = OpenTelemetry::Common::HTTP::ClientContext.attributes
54+
normalized = METHOD_CACHE[method]
55+
attributes = client_context_attrs.dup
56+
57+
# Determine base span name and method value
58+
if normalized
59+
span_name = normalized
60+
method_value = normalized
61+
else
62+
span_name = 'HTTP'
63+
method_value = '_OTHER'
64+
end
65+
66+
attributes['http.method'] ||= method_value
67+
68+
SpanCreationAttributes.new(span_name: span_name, attributes: attributes)
69+
end
70+
71+
# Prepares span data using stable semantic conventions
72+
# @param method [String, Symbol] The HTTP method
73+
# @return [SpanCreationAttributes] struct containing span_name and attributes hash
74+
def self.span_attrs_for_stable(method)
5475
client_context_attrs = OpenTelemetry::Common::HTTP::ClientContext.attributes
5576
url_template = client_context_attrs['url.template']
5677
normalized = METHOD_CACHE[method]
@@ -60,32 +81,45 @@ def self.span_attrs_for(method, semconv: :old)
6081
if normalized
6182
base_name = normalized
6283
method_value = normalized
84+
original = nil
6385
else
6486
base_name = 'HTTP'
6587
method_value = '_OTHER'
6688
original = method.to_s
6789
end
6890

69-
# Set span name based on semantic convention
70-
span_name = if semconv == :old
71-
base_name
72-
else
73-
(url_template ? "#{base_name} #{url_template}" : base_name)
74-
end
75-
76-
# Set attributes based on semantic convention
77-
case semconv
78-
when :old
79-
attributes['http.method'] ||= method_value
80-
when :stable
81-
attributes['http.request.method'] ||= method_value
82-
attributes['http.request.method_original'] ||= original if original
83-
when :dup
84-
attributes['http.method'] ||= method_value
85-
attributes['http.request.method'] ||= method_value
86-
attributes['http.request.method_original'] ||= original if original
91+
span_name = url_template ? "#{base_name} #{url_template}" : base_name
92+
attributes['http.request.method'] ||= method_value
93+
attributes['http.request.method_original'] ||= original if original
94+
95+
SpanCreationAttributes.new(span_name: span_name, attributes: attributes)
96+
end
97+
98+
# Prepares span data using both old and stable semantic conventions
99+
# @param method [String, Symbol] The HTTP method
100+
# @return [SpanCreationAttributes] struct containing span_name and attributes hash
101+
def self.span_attrs_for_dup(method)
102+
client_context_attrs = OpenTelemetry::Common::HTTP::ClientContext.attributes
103+
url_template = client_context_attrs['url.template']
104+
normalized = METHOD_CACHE[method]
105+
attributes = client_context_attrs.dup
106+
107+
# Determine base span name and method value
108+
if normalized
109+
base_name = normalized
110+
method_value = normalized
111+
original = nil
112+
else
113+
base_name = 'HTTP'
114+
method_value = '_OTHER'
115+
original = method.to_s
87116
end
88117

118+
span_name = url_template ? "#{base_name} #{url_template}" : base_name
119+
attributes['http.method'] ||= method_value
120+
attributes['http.request.method'] ||= method_value
121+
attributes['http.request.method_original'] ||= original if original
122+
89123
SpanCreationAttributes.new(span_name: span_name, attributes: attributes)
90124
end
91125
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TracerMiddleware < ::Excon::Middleware::Base
1717
def request_call(datum)
1818
return @stack.request_call(datum) if untraced?(datum)
1919

20-
span_data = HttpHelper.span_attrs_for(datum[:method], semconv: :dup)
20+
span_data = HttpHelper.span_attrs_for_dup(datum[:method])
2121

2222
cleansed_url = OpenTelemetry::Common::Utilities.cleanse_url(::Excon::Utils.request_uri(datum))
2323
attributes = {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TracerMiddleware < ::Excon::Middleware::Base
1717
def request_call(datum)
1818
return @stack.request_call(datum) if untraced?(datum)
1919

20-
span_data = HttpHelper.span_attrs_for(datum[:method], semconv: :old)
20+
span_data = HttpHelper.span_attrs_for_old(datum[:method])
2121

2222
attributes = {
2323
OpenTelemetry::SemanticConventions::Trace::HTTP_HOST => datum[:host],

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TracerMiddleware < ::Excon::Middleware::Base
1717
def request_call(datum)
1818
return @stack.request_call(datum) if untraced?(datum)
1919

20-
span_data = HttpHelper.span_attrs_for(datum[:method], semconv: :stable)
20+
span_data = HttpHelper.span_attrs_for_stable(datum[:method])
2121

2222
attributes = { 'url.scheme' => datum[:scheme],
2323
'url.path' => datum[:path],

instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/http_helper.rb

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,32 @@ module HttpHelper
4848

4949
module_function
5050

51-
# Prepares all span data in a single call
51+
# Prepares span data using old semantic conventions
5252
# @param method [String, Symbol] The HTTP method
53-
# @param semconv [Symbol] Semantic convention to use (:old, :stable, or :dup)
5453
# @return [SpanCreationAttributes] struct containing span_name and attributes hash
55-
def span_attrs_for(method, semconv: :old)
54+
def span_attrs_for_old(method)
55+
client_context_attrs = OpenTelemetry::Common::HTTP::ClientContext.attributes
56+
normalized = METHOD_CACHE[method]
57+
attributes = client_context_attrs.dup
58+
59+
# Determine base span name and method value
60+
if normalized
61+
span_name = normalized
62+
method_value = normalized
63+
else
64+
span_name = 'HTTP'
65+
method_value = '_OTHER'
66+
end
67+
68+
attributes['http.method'] ||= method_value
69+
70+
SpanCreationAttributes.new(span_name: span_name, attributes: attributes)
71+
end
72+
73+
# Prepares span data using stable semantic conventions
74+
# @param method [String, Symbol] The HTTP method
75+
# @return [SpanCreationAttributes] struct containing span_name and attributes hash
76+
def span_attrs_for_stable(method)
5677
client_context_attrs = OpenTelemetry::Common::HTTP::ClientContext.attributes
5778
url_template = client_context_attrs['url.template']
5879
normalized = METHOD_CACHE[method]
@@ -62,32 +83,45 @@ def span_attrs_for(method, semconv: :old)
6283
if normalized
6384
base_name = normalized
6485
method_value = normalized
86+
original = nil
6587
else
6688
base_name = 'HTTP'
6789
method_value = '_OTHER'
6890
original = method.to_s
6991
end
7092

71-
# Set span name based on semantic convention
72-
span_name = if semconv == :old
73-
base_name
74-
else
75-
(url_template ? "#{base_name} #{url_template}" : base_name)
76-
end
77-
78-
# Set attributes based on semantic convention
79-
case semconv
80-
when :old
81-
attributes['http.method'] ||= method_value
82-
when :stable
83-
attributes['http.request.method'] ||= method_value
84-
attributes['http.request.method_original'] ||= original if original
85-
when :dup
86-
attributes['http.method'] ||= method_value
87-
attributes['http.request.method'] ||= method_value
88-
attributes['http.request.method_original'] ||= original if original
93+
span_name = url_template ? "#{base_name} #{url_template}" : base_name
94+
attributes['http.request.method'] ||= method_value
95+
attributes['http.request.method_original'] ||= original if original
96+
97+
SpanCreationAttributes.new(span_name: span_name, attributes: attributes)
98+
end
99+
100+
# Prepares span data using both old and stable semantic conventions
101+
# @param method [String, Symbol] The HTTP method
102+
# @return [SpanCreationAttributes] struct containing span_name and attributes hash
103+
def span_attrs_for_dup(method)
104+
client_context_attrs = OpenTelemetry::Common::HTTP::ClientContext.attributes
105+
url_template = client_context_attrs['url.template']
106+
normalized = METHOD_CACHE[method]
107+
attributes = client_context_attrs.dup
108+
109+
# Determine base span name and method value
110+
if normalized
111+
base_name = normalized
112+
method_value = normalized
113+
original = nil
114+
else
115+
base_name = 'HTTP'
116+
method_value = '_OTHER'
117+
original = method.to_s
89118
end
90119

120+
span_name = url_template ? "#{base_name} #{url_template}" : base_name
121+
attributes['http.method'] ||= method_value
122+
attributes['http.request.method'] ||= method_value
123+
attributes['http.request.method_original'] ||= original if original
124+
91125
SpanCreationAttributes.new(span_name: span_name, attributes: attributes)
92126
end
93127
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TracerMiddleware < ::Faraday::Middleware
1616
HTTP_STATUS_SUCCESS_RANGE = (100..399)
1717

1818
def call(env)
19-
span_data = HttpHelper.span_attrs_for(env.method, semconv: :dup)
19+
span_data = HttpHelper.span_attrs_for_dup(env.method)
2020

2121
config = Faraday::Instrumentation.instance.config
2222

0 commit comments

Comments
 (0)