Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ task default: [:each]

def foreach_gem(cmd)
Dir.glob("**/opentelemetry-*.gemspec") do |gemspec|
name = File.basename(gemspec, ".gemspec")
next if gemspec.start_with?('.')

dir = File.dirname(gemspec)
puts "**** Entering #{dir}"
Dir.chdir(dir) do
Expand Down
1 change: 1 addition & 0 deletions instrumentation/all/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ group :test do
Dir.entries('../')
.select { |entry| File.directory?(File.join('../', entry)) }
.reject { |entry| excluded_instrumentations.include?(entry) }
.reject { |entry| entry.start_with?('.') }
.sort
.each { |dir| gem "opentelemetry-instrumentation-#{dir}", path: "../#{dir}" }
end
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Ethon
# @api private
module HttpHelper
# Lightweight struct to hold span creation attributes
SpanCreationAttributes = Struct.new(:span_name, :normalized_method, :original_method, keyword_init: true)
SpanCreationAttributes = Struct.new(:span_name, :attributes, keyword_init: true)

# Pre-computed mapping to avoid string allocations during normalization
METHOD_CACHE = {
Expand Down Expand Up @@ -44,41 +44,83 @@ module HttpHelper
:trace => 'TRACE'
}.freeze

# Pre-computed span names for old semantic conventions to avoid allocations
OLD_SPAN_NAMES = {
'CONNECT' => 'HTTP CONNECT',
'DELETE' => 'HTTP DELETE',
'GET' => 'HTTP GET',
'HEAD' => 'HTTP HEAD',
'OPTIONS' => 'HTTP OPTIONS',
'PATCH' => 'HTTP PATCH',
'POST' => 'HTTP POST',
'PUT' => 'HTTP PUT',
'TRACE' => 'HTTP TRACE'
}.freeze
private_constant :METHOD_CACHE

# Prepares span data using old semantic conventions
# @param method [String, Symbol] The HTTP method
# @return [SpanCreationAttributes] struct containing span_name and attributes hash
def self.span_attrs_for_old(method)
client_context_attrs = OpenTelemetry::Common::HTTP::ClientContext.attributes
normalized = METHOD_CACHE[method]
attributes = client_context_attrs.dup

# Determine base span name and method value
if normalized
span_name = normalized
method_value = normalized
else
span_name = 'HTTP'
method_value = '_OTHER'
end

attributes['http.method'] ||= method_value

private_constant :METHOD_CACHE, :OLD_SPAN_NAMES
SpanCreationAttributes.new(span_name: span_name, attributes: attributes)
end

# Prepares all span data for the specified semantic convention in a single call
# Prepares span data using stable semantic conventions
# @param method [String, Symbol] The HTTP method
# @param semconv [Symbol] The semantic convention to use (:stable or :old)
# @return [SpanCreationAttributes] struct containing span_name, normalized_method, and original_method
def self.span_attrs_for(method, semconv: :stable)
# @return [SpanCreationAttributes] struct containing span_name and attributes hash
def self.span_attrs_for_stable(method)
client_context_attrs = OpenTelemetry::Common::HTTP::ClientContext.attributes
url_template = client_context_attrs['url.template']
normalized = METHOD_CACHE[method]
attributes = client_context_attrs.dup

# Determine base span name and method value
if normalized
span_name = semconv == :old ? OLD_SPAN_NAMES[normalized] : normalized
SpanCreationAttributes.new(
span_name: span_name,
normalized_method: normalized,
original_method: nil
)
base_name = normalized
method_value = normalized
original = nil
else
SpanCreationAttributes.new(
span_name: 'HTTP',
normalized_method: '_OTHER',
original_method: method.to_s
)
base_name = 'HTTP'
method_value = '_OTHER'
original = method.to_s
end

span_name = url_template ? "#{base_name} #{url_template}" : base_name
attributes['http.request.method'] ||= method_value
attributes['http.request.method_original'] ||= original if original

SpanCreationAttributes.new(span_name: span_name, attributes: attributes)
end

# Prepares span data using both old and stable semantic conventions
# @param method [String, Symbol] The HTTP method
# @return [SpanCreationAttributes] struct containing span_name and attributes hash
def self.span_attrs_for_dup(method)
client_context_attrs = OpenTelemetry::Common::HTTP::ClientContext.attributes
url_template = client_context_attrs['url.template']
normalized = METHOD_CACHE[method]
attributes = client_context_attrs.dup

# Determine base span name and method value
if normalized
base_name = normalized
method_value = normalized
original = nil
else
base_name = 'HTTP'
method_value = '_OTHER'
original = method.to_s
end

span_name = url_template ? "#{base_name} #{url_template}" : base_name
attributes['http.method'] ||= method_value
attributes['http.request.method'] ||= method_value
attributes['http.request.method_original'] ||= original if original

SpanCreationAttributes.new(span_name: span_name, attributes: attributes)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def reset
end

def otel_before_request
span_data = HttpHelper.span_attrs_for(@otel_method)
span_data = HttpHelper.span_attrs_for_dup(@otel_method)

@otel_span = tracer.start_span(
span_data.span_name,
Expand All @@ -90,11 +90,7 @@ def otel_span_started?
private

def span_creation_attributes(span_data)
instrumentation_attrs = {
'http.method' => span_data.normalized_method,
'http.request.method' => span_data.normalized_method
}
instrumentation_attrs['http.request.method_original'] = span_data.original_method if span_data.original_method
instrumentation_attrs = {}

uri = _otel_cleanse_uri(url)
if uri
Expand All @@ -106,9 +102,7 @@ def span_creation_attributes(span_data)

config = Ethon::Instrumentation.instance.config
instrumentation_attrs['peer.service'] = config[:peer_service] if config[:peer_service]
instrumentation_attrs.merge!(
OpenTelemetry::Common::HTTP::ClientContext.attributes
)
instrumentation_attrs.merge!(span_data.attributes)
end

# Returns a URL string with userinfo removed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def reset
end

def otel_before_request
span_data = HttpHelper.span_attrs_for(@otel_method, semconv: :old)
span_data = HttpHelper.span_attrs_for_old(@otel_method)

@otel_span = tracer.start_span(
span_data.span_name,
Expand All @@ -89,9 +89,7 @@ def otel_span_started?
private

def span_creation_attributes(span_data)
instrumentation_attrs = {
'http.method' => span_data.normalized_method
}
instrumentation_attrs = {}

uri = _otel_cleanse_uri(url)
if uri
Expand All @@ -101,9 +99,7 @@ def span_creation_attributes(span_data)

config = Ethon::Instrumentation.instance.config
instrumentation_attrs['peer.service'] = config[:peer_service] if config[:peer_service]
instrumentation_attrs.merge!(
OpenTelemetry::Common::HTTP::ClientContext.attributes
)
instrumentation_attrs.merge!(span_data.attributes)
end

# Returns a URL string with userinfo removed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def reset
end

def otel_before_request
span_data = HttpHelper.span_attrs_for(@otel_method)
span_data = HttpHelper.span_attrs_for_stable(@otel_method)

@otel_span = tracer.start_span(
span_data.span_name,
Expand All @@ -89,10 +89,7 @@ def otel_span_started?
private

def span_creation_attributes(span_data)
instrumentation_attrs = {
'http.request.method' => span_data.normalized_method
}
instrumentation_attrs['http.request.method_original'] = span_data.original_method if span_data.original_method
instrumentation_attrs = {}

uri = _otel_cleanse_uri(url)
if uri
Expand All @@ -102,9 +99,7 @@ def span_creation_attributes(span_data)

config = Ethon::Instrumentation.instance.config
instrumentation_attrs['peer.service'] = config[:peer_service] if config[:peer_service]
instrumentation_attrs.merge!(
OpenTelemetry::Common::HTTP::ClientContext.attributes
)
instrumentation_attrs.merge!(span_data.attributes)
end

# Returns a URL string with userinfo removed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module Excon
# Utility module for HTTP-related helper methods
# @api private
module HttpHelper
# Lightweight struct to hold span creation attributes
SpanCreationAttributes = Struct.new(:span_name, :normalized_method, :original_method, keyword_init: true)
# Lightweight struct to hold span creation data
SpanCreationAttributes = Struct.new(:span_name, :attributes, keyword_init: true)

# Pre-computed mapping to avoid string allocations during normalization
METHOD_CACHE = {
Expand Down Expand Up @@ -44,41 +44,83 @@ module HttpHelper
:trace => 'TRACE'
}.freeze

# Pre-computed span names for old semantic conventions to avoid allocations
OLD_SPAN_NAMES = {
'CONNECT' => 'HTTP CONNECT',
'DELETE' => 'HTTP DELETE',
'GET' => 'HTTP GET',
'HEAD' => 'HTTP HEAD',
'OPTIONS' => 'HTTP OPTIONS',
'PATCH' => 'HTTP PATCH',
'POST' => 'HTTP POST',
'PUT' => 'HTTP PUT',
'TRACE' => 'HTTP TRACE'
}.freeze
private_constant :METHOD_CACHE

# Prepares span data using old semantic conventions
# @param method [String, Symbol] The HTTP method
# @return [SpanCreationAttributes] struct containing span_name and attributes hash
def self.span_attrs_for_old(method)
client_context_attrs = OpenTelemetry::Common::HTTP::ClientContext.attributes
normalized = METHOD_CACHE[method]
attributes = client_context_attrs.dup

# Determine base span name and method value
if normalized
span_name = normalized
method_value = normalized
else
span_name = 'HTTP'
method_value = '_OTHER'
end

attributes['http.method'] ||= method_value

private_constant :METHOD_CACHE, :OLD_SPAN_NAMES
SpanCreationAttributes.new(span_name: span_name, attributes: attributes)
end

# Prepares all span data for the specified semantic convention in a single call
# Prepares span data using stable semantic conventions
# @param method [String, Symbol] The HTTP method
# @param semconv [Symbol] The semantic convention to use (:stable or :old)
# @return [SpanCreationAttributes] struct containing span_name, normalized_method, and original_method
def self.span_attrs_for(method, semconv: :stable)
# @return [SpanCreationAttributes] struct containing span_name and attributes hash
def self.span_attrs_for_stable(method)
client_context_attrs = OpenTelemetry::Common::HTTP::ClientContext.attributes
url_template = client_context_attrs['url.template']
normalized = METHOD_CACHE[method]
attributes = client_context_attrs.dup

# Determine base span name and method value
if normalized
span_name = semconv == :old ? OLD_SPAN_NAMES[normalized] : normalized
SpanCreationAttributes.new(
span_name: span_name,
normalized_method: normalized,
original_method: nil
)
base_name = normalized
method_value = normalized
original = nil
else
SpanCreationAttributes.new(
span_name: 'HTTP',
normalized_method: '_OTHER',
original_method: method.to_s
)
base_name = 'HTTP'
method_value = '_OTHER'
original = method.to_s
end

span_name = url_template ? "#{base_name} #{url_template}" : base_name
attributes['http.request.method'] ||= method_value
attributes['http.request.method_original'] ||= original if original

SpanCreationAttributes.new(span_name: span_name, attributes: attributes)
end

# Prepares span data using both old and stable semantic conventions
# @param method [String, Symbol] The HTTP method
# @return [SpanCreationAttributes] struct containing span_name and attributes hash
def self.span_attrs_for_dup(method)
client_context_attrs = OpenTelemetry::Common::HTTP::ClientContext.attributes
url_template = client_context_attrs['url.template']
normalized = METHOD_CACHE[method]
attributes = client_context_attrs.dup

# Determine base span name and method value
if normalized
base_name = normalized
method_value = normalized
original = nil
else
base_name = 'HTTP'
method_value = '_OTHER'
original = method.to_s
end

span_name = url_template ? "#{base_name} #{url_template}" : base_name
attributes['http.method'] ||= method_value
attributes['http.request.method'] ||= method_value
attributes['http.request.method_original'] ||= original if original

SpanCreationAttributes.new(span_name: span_name, attributes: attributes)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,26 @@ class TracerMiddleware < ::Excon::Middleware::Base
def request_call(datum)
return @stack.request_call(datum) if untraced?(datum)

span_data = HttpHelper.span_attrs_for(datum[:method])
span_data = HttpHelper.span_attrs_for_dup(datum[:method])

cleansed_url = OpenTelemetry::Common::Utilities.cleanse_url(::Excon::Utils.request_uri(datum))
attributes = {
OpenTelemetry::SemanticConventions::Trace::HTTP_HOST => datum[:host],
OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => span_data.normalized_method,
OpenTelemetry::SemanticConventions::Trace::HTTP_SCHEME => datum[:scheme],
OpenTelemetry::SemanticConventions::Trace::HTTP_TARGET => datum[:path],
OpenTelemetry::SemanticConventions::Trace::HTTP_URL => cleansed_url,
OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => datum[:hostname],
OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => datum[:port],
'http.request.method' => span_data.normalized_method,
'url.scheme' => datum[:scheme],
'url.path' => datum[:path],
'url.full' => cleansed_url,
'server.address' => datum[:hostname],
'server.port' => datum[:port]
}
attributes['http.request.method_original'] = span_data.original_method if span_data.original_method
attributes['url.query'] = datum[:query] if datum[:query]
peer_service = Excon::Instrumentation.instance.config[:peer_service]
attributes[OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE] = peer_service if peer_service
attributes.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)
attributes.merge!(span_data.attributes)
span = tracer.start_span(span_data.span_name, attributes: attributes, kind: :client)
ctx = OpenTelemetry::Trace.context_with_span(span)
datum[:otel_span] = span
Expand Down
Loading