Skip to content

Commit f59066e

Browse files
committed
squash: reduce feature envy
1 parent 078f3fd commit f59066e

File tree

43 files changed

+586
-668
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+586
-668
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ module Ethon
1515
end
1616
end
1717

18+
require_relative 'ethon/http_helper'
1819
require_relative 'ethon/instrumentation'
1920
require_relative 'ethon/version'
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
module OpenTelemetry
8+
module Instrumentation
9+
module Ethon
10+
# Helper module for HTTP method normalization
11+
# @api private
12+
module HttpHelper
13+
# Lightweight struct to hold span creation attributes
14+
SpanCreationAttributes = Struct.new(:span_name, :normalized_method, :original_method, keyword_init: true)
15+
16+
# Pre-computed mapping to avoid string allocations during normalization
17+
METHOD_CACHE = {
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+
'connect' => 'CONNECT',
28+
'delete' => 'DELETE',
29+
'get' => 'GET',
30+
'head' => 'HEAD',
31+
'options' => 'OPTIONS',
32+
'patch' => 'PATCH',
33+
'post' => 'POST',
34+
'put' => 'PUT',
35+
'trace' => 'TRACE',
36+
:connect => 'CONNECT',
37+
:delete => 'DELETE',
38+
:get => 'GET',
39+
:head => 'HEAD',
40+
:options => 'OPTIONS',
41+
:patch => 'PATCH',
42+
:post => 'POST',
43+
:put => 'PUT',
44+
:trace => 'TRACE'
45+
}.freeze
46+
47+
# Pre-computed span names for old semantic conventions to avoid allocations
48+
OLD_SPAN_NAMES = {
49+
'CONNECT' => 'HTTP CONNECT',
50+
'DELETE' => 'HTTP DELETE',
51+
'GET' => 'HTTP GET',
52+
'HEAD' => 'HTTP HEAD',
53+
'OPTIONS' => 'HTTP OPTIONS',
54+
'PATCH' => 'HTTP PATCH',
55+
'POST' => 'HTTP POST',
56+
'PUT' => 'HTTP PUT',
57+
'TRACE' => 'HTTP TRACE'
58+
}.freeze
59+
60+
private_constant :METHOD_CACHE, :OLD_SPAN_NAMES
61+
62+
# Prepares all span data for the specified semantic convention in a single call
63+
# @param method [String, Symbol] The HTTP method
64+
# @param semconv [Symbol] The semantic convention to use (:stable or :old)
65+
# @return [SpanCreationAttributes] struct containing span_name, normalized_method, and original_method
66+
def self.span_attrs_for(method, semconv: :stable)
67+
normalized = METHOD_CACHE[method]
68+
if normalized
69+
span_name = semconv == :old ? OLD_SPAN_NAMES[normalized] : normalized
70+
SpanCreationAttributes.new(
71+
span_name: span_name,
72+
normalized_method: normalized,
73+
original_method: nil
74+
)
75+
else
76+
SpanCreationAttributes.new(
77+
span_name: 'HTTP',
78+
normalized_method: '_OTHER',
79+
original_method: method.to_s
80+
)
81+
end
82+
end
83+
end
84+
end
85+
end
86+
end

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

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

7-
require_relative '../http_helper'
8-
97
module OpenTelemetry
108
module Instrumentation
119
module Ethon
@@ -70,12 +68,11 @@ def reset
7068
end
7169

7270
def otel_before_request
73-
normalized_method, original_method = HttpHelper.normalize_method(@otel_method)
74-
span_name = HttpHelper.span_name_for_stable(normalized_method)
71+
span_data = HttpHelper.span_attrs_for(@otel_method)
7572

7673
@otel_span = tracer.start_span(
77-
span_name,
78-
attributes: span_creation_attributes(normalized_method, original_method),
74+
span_data.span_name,
75+
attributes: span_creation_attributes(span_data),
7976
kind: :client
8077
)
8178

@@ -92,12 +89,12 @@ def otel_span_started?
9289

9390
private
9491

95-
def span_creation_attributes(normalized_method, original_method)
92+
def span_creation_attributes(span_data)
9693
instrumentation_attrs = {
97-
'http.method' => normalized_method,
98-
'http.request.method' => normalized_method
94+
'http.method' => span_data.normalized_method,
95+
'http.request.method' => span_data.normalized_method
9996
}
100-
instrumentation_attrs['http.request.method_original'] = original_method if original_method
97+
instrumentation_attrs['http.request.method_original'] = span_data.original_method if span_data.original_method
10198

10299
uri = _otel_cleanse_uri(url)
103100
if uri

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

Lines changed: 0 additions & 89 deletions
This file was deleted.

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

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

7-
require_relative '../http_helper'
8-
97
module OpenTelemetry
108
module Instrumentation
119
module Ethon
@@ -69,12 +67,11 @@ def reset
6967
end
7068

7169
def otel_before_request
72-
normalized_method, _original_method = HttpHelper.normalize_method(@otel_method)
73-
span_name = HttpHelper.span_name_for_old(normalized_method)
70+
span_data = HttpHelper.span_attrs_for(@otel_method, semconv: :old)
7471

7572
@otel_span = tracer.start_span(
76-
span_name,
77-
attributes: span_creation_attributes(normalized_method),
73+
span_data.span_name,
74+
attributes: span_creation_attributes(span_data),
7875
kind: :client
7976
)
8077

@@ -91,9 +88,9 @@ def otel_span_started?
9188

9289
private
9390

94-
def span_creation_attributes(normalized_method)
91+
def span_creation_attributes(span_data)
9592
instrumentation_attrs = {
96-
'http.method' => normalized_method
93+
'http.method' => span_data.normalized_method
9794
}
9895

9996
uri = _otel_cleanse_uri(url)

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

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

7-
require_relative '../http_helper'
8-
97
module OpenTelemetry
108
module Instrumentation
119
module Ethon
@@ -69,12 +67,11 @@ def reset
6967
end
7068

7169
def otel_before_request
72-
normalized_method, original_method = HttpHelper.normalize_method(@otel_method)
73-
span_name = HttpHelper.span_name_for_stable(normalized_method)
70+
span_data = HttpHelper.span_attrs_for(@otel_method)
7471

7572
@otel_span = tracer.start_span(
76-
span_name,
77-
attributes: span_creation_attributes(normalized_method, original_method),
73+
span_data.span_name,
74+
attributes: span_creation_attributes(span_data),
7875
kind: :client
7976
)
8077

@@ -91,11 +88,11 @@ def otel_span_started?
9188

9289
private
9390

94-
def span_creation_attributes(normalized_method, original_method)
91+
def span_creation_attributes(span_data)
9592
instrumentation_attrs = {
96-
'http.request.method' => normalized_method
93+
'http.request.method' => span_data.normalized_method
9794
}
98-
instrumentation_attrs['http.request.method_original'] = original_method if original_method
95+
instrumentation_attrs['http.request.method_original'] = span_data.original_method if span_data.original_method
9996

10097
uri = _otel_cleanse_uri(url)
10198
if uri

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ module Excon
1515
end
1616
end
1717

18+
require_relative 'excon/http_helper'
1819
require_relative 'excon/instrumentation'
1920
require_relative 'excon/version'
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
module OpenTelemetry
8+
module Instrumentation
9+
module Excon
10+
# Utility module for HTTP-related helper methods
11+
# @api private
12+
module HttpHelper
13+
# Lightweight struct to hold span creation attributes
14+
SpanCreationAttributes = Struct.new(:span_name, :normalized_method, :original_method, keyword_init: true)
15+
16+
# Pre-computed mapping to avoid string allocations during normalization
17+
METHOD_CACHE = {
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+
'connect' => 'CONNECT',
28+
'delete' => 'DELETE',
29+
'get' => 'GET',
30+
'head' => 'HEAD',
31+
'options' => 'OPTIONS',
32+
'patch' => 'PATCH',
33+
'post' => 'POST',
34+
'put' => 'PUT',
35+
'trace' => 'TRACE',
36+
:connect => 'CONNECT',
37+
:delete => 'DELETE',
38+
:get => 'GET',
39+
:head => 'HEAD',
40+
:options => 'OPTIONS',
41+
:patch => 'PATCH',
42+
:post => 'POST',
43+
:put => 'PUT',
44+
:trace => 'TRACE'
45+
}.freeze
46+
47+
# Pre-computed span names for old semantic conventions to avoid allocations
48+
OLD_SPAN_NAMES = {
49+
'CONNECT' => 'HTTP CONNECT',
50+
'DELETE' => 'HTTP DELETE',
51+
'GET' => 'HTTP GET',
52+
'HEAD' => 'HTTP HEAD',
53+
'OPTIONS' => 'HTTP OPTIONS',
54+
'PATCH' => 'HTTP PATCH',
55+
'POST' => 'HTTP POST',
56+
'PUT' => 'HTTP PUT',
57+
'TRACE' => 'HTTP TRACE'
58+
}.freeze
59+
60+
private_constant :METHOD_CACHE, :OLD_SPAN_NAMES
61+
62+
# Prepares all span data for the specified semantic convention in a single call
63+
# @param method [String, Symbol] The HTTP method
64+
# @param semconv [Symbol] The semantic convention to use (:stable or :old)
65+
# @return [SpanCreationAttributes] struct containing span_name, normalized_method, and original_method
66+
def self.span_attrs_for(method, semconv: :stable)
67+
normalized = METHOD_CACHE[method]
68+
if normalized
69+
span_name = semconv == :old ? OLD_SPAN_NAMES[normalized] : normalized
70+
SpanCreationAttributes.new(
71+
span_name: span_name,
72+
normalized_method: normalized,
73+
original_method: nil
74+
)
75+
else
76+
SpanCreationAttributes.new(
77+
span_name: 'HTTP',
78+
normalized_method: '_OTHER',
79+
original_method: method.to_s
80+
)
81+
end
82+
end
83+
end
84+
end
85+
end
86+
end

0 commit comments

Comments
 (0)