Skip to content
Draft
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 @@ -15,5 +15,6 @@ module Ethon
end
end

require_relative 'ethon/http_helper'
require_relative 'ethon/instrumentation'
require_relative 'ethon/version'
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
module Instrumentation
module Ethon
# Helper module for HTTP method normalization
# @api private
module HttpHelper
# Lightweight struct to hold span creation attributes
SpanCreationAttributes = Struct.new(:span_name, :attributes, keyword_init: true)

# Pre-computed mapping to avoid string allocations during normalization
METHOD_CACHE = {
'CONNECT' => 'CONNECT',
'DELETE' => 'DELETE',
'GET' => 'GET',
'HEAD' => 'HEAD',
'OPTIONS' => 'OPTIONS',
'PATCH' => 'PATCH',
'POST' => 'POST',
'PUT' => 'PUT',
'TRACE' => 'TRACE',
'connect' => 'CONNECT',
'delete' => 'DELETE',
'get' => 'GET',
'head' => 'HEAD',
'options' => 'OPTIONS',
'patch' => 'PATCH',
'post' => 'POST',
'put' => 'PUT',
'trace' => 'TRACE',
:connect => 'CONNECT',
:delete => 'DELETE',
:get => 'GET',
:head => 'HEAD',
:options => 'OPTIONS',
:patch => 'PATCH',
:post => 'POST',
:put => 'PUT',
:trace => '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

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

# Prepares span data using stable semantic conventions
# @param method [String, Symbol] The HTTP method
# @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
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.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
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,11 @@ module Patches
module Dup
# Ethon::Easy patch for instrumentation
module Easy
ACTION_NAMES_TO_HTTP_METHODS = Hash.new do |h, k|
# #to_s is required because user input could be symbol or string
h[k] = k.to_s.upcase
end
HTTP_METHODS_TO_SPAN_NAMES = Hash.new do |h, k|
h[k] = k.to_s
h[k] = 'HTTP' if k == '_OTHER'
end

# Constant for the HTTP status range
HTTP_STATUS_SUCCESS_RANGE = (100..399)

def http_request(url, action_name, options = {})
@otel_method = ACTION_NAMES_TO_HTTP_METHODS[action_name]
@otel_method = action_name
super
end

Expand Down Expand Up @@ -77,12 +68,11 @@ def reset
end

def otel_before_request
method = '_OTHER' # Could be GET or not HTTP at all
method = @otel_method if instance_variable_defined?(:@otel_method) && !@otel_method.nil?
span_data = HttpHelper.span_attrs_for_dup(@otel_method)

@otel_span = tracer.start_span(
HTTP_METHODS_TO_SPAN_NAMES[method],
attributes: span_creation_attributes(method),
span_data.span_name,
attributes: span_creation_attributes(span_data),
kind: :client
)

Expand All @@ -99,12 +89,8 @@ def otel_span_started?

private

def span_creation_attributes(method)
http_method = (method == '_OTHER' ? 'N/A' : method)
instrumentation_attrs = {
'http.method' => http_method,
'http.request.method' => method
}
def span_creation_attributes(span_data)
instrumentation_attrs = {}

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

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 @@ -12,17 +12,11 @@ module Patches
module Old
# Ethon::Easy patch for instrumentation
module Easy
ACTION_NAMES_TO_HTTP_METHODS = Hash.new do |h, k|
# #to_s is required because user input could be symbol or string
h[k] = k.to_s.upcase
end
HTTP_METHODS_TO_SPAN_NAMES = Hash.new { |h, k| h[k] = "HTTP #{k}" }

# Constant for the HTTP status range
HTTP_STATUS_SUCCESS_RANGE = (100..399)

def http_request(url, action_name, options = {})
@otel_method = ACTION_NAMES_TO_HTTP_METHODS[action_name]
@otel_method = action_name
super
end

Expand Down Expand Up @@ -73,12 +67,11 @@ def reset
end

def otel_before_request
method = 'N/A' # Could be GET or not HTTP at all
method = @otel_method if instance_variable_defined?(:@otel_method) && !@otel_method.nil?
span_data = HttpHelper.span_attrs_for_old(@otel_method)

@otel_span = tracer.start_span(
HTTP_METHODS_TO_SPAN_NAMES[method],
attributes: span_creation_attributes(method),
span_data.span_name,
attributes: span_creation_attributes(span_data),
kind: :client
)

Expand All @@ -95,10 +88,8 @@ def otel_span_started?

private

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

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

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 @@ -12,20 +12,11 @@ module Patches
module Stable
# Ethon::Easy patch for instrumentation
module Easy
ACTION_NAMES_TO_HTTP_METHODS = Hash.new do |h, k|
# #to_s is required because user input could be symbol or string
h[k] = k.to_s.upcase
end
HTTP_METHODS_TO_SPAN_NAMES = Hash.new do |h, k|
h[k] = k.to_s
h[k] = 'HTTP' if k == '_OTHER'
end

# Constant for the HTTP status range
HTTP_STATUS_SUCCESS_RANGE = (100..399)

def http_request(url, action_name, options = {})
@otel_method = ACTION_NAMES_TO_HTTP_METHODS[action_name]
@otel_method = action_name
super
end

Expand Down Expand Up @@ -76,12 +67,11 @@ def reset
end

def otel_before_request
method = '_OTHER' # Could be GET or not HTTP at all
method = @otel_method if instance_variable_defined?(:@otel_method) && !@otel_method.nil?
span_data = HttpHelper.span_attrs_for_stable(@otel_method)

@otel_span = tracer.start_span(
HTTP_METHODS_TO_SPAN_NAMES[method],
attributes: span_creation_attributes(method),
span_data.span_name,
attributes: span_creation_attributes(span_data),
kind: :client
)

Expand All @@ -98,10 +88,8 @@ def otel_span_started?

private

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

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

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
Loading