Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Freeze all range objects #1172 #1222

Merged
merged 9 commits into from
Nov 24, 2024
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.5"

x-shared-config:
base: &base
command: /bin/bash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ module Easy
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]
super
Expand All @@ -42,7 +45,7 @@ def complete
@otel_span.status = OpenTelemetry::Trace::Status.error("Request has failed: #{message}")
else
@otel_span.set_attribute('http.status_code', response_code)
@otel_span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(response_code.to_i)
@otel_span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(response_code.to_i)
end
ensure
@otel_span&.finish
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class TracerMiddleware < ::Excon::Middleware::Base
hash[uppercase_method] ||= "HTTP #{uppercase_method}"
end.freeze

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

def request_call(datum)
return @stack.request_call(datum) if untraced?(datum)

http_method = HTTP_METHODS_TO_UPPERCASE[datum[:method]]

attributes = {
OpenTelemetry::SemanticConventions::Trace::HTTP_HOST => datum[:host],
OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => http_method,
Expand All @@ -35,19 +37,14 @@ def request_call(datum)
OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => datum[:hostname],
OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => datum[:port]
}

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)

span = tracer.start_span(HTTP_METHODS_TO_SPAN_NAMES[http_method], attributes: attributes, kind: :client)
ctx = OpenTelemetry::Trace.context_with_span(span)

datum[:otel_span] = span
datum[:otel_token] = OpenTelemetry::Context.attach(ctx)

OpenTelemetry.propagation.inject(datum[:headers])

@stack.request_call(datum)
end

Expand All @@ -68,7 +65,6 @@ def self.around_default_stack
# If the default stack contains a version of the trace middleware already...
existing_trace_middleware = default_stack.find { |m| m <= TracerMiddleware }
default_stack.delete(existing_trace_middleware) if existing_trace_middleware

# Inject after the ResponseParser middleware
response_middleware_index = default_stack.index(::Excon::Middleware::ResponseParser).to_i
default_stack.insert(response_middleware_index + 1, self)
Expand All @@ -84,7 +80,7 @@ def handle_response(datum)
if datum.key?(:response)
response = datum[:response]
span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, response[:status])
span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(response[:status].to_i)
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(response[:status].to_i)
end

if datum.key?(:error)
Expand All @@ -93,7 +89,6 @@ def handle_response(datum)
end

span.finish

OpenTelemetry::Context.detach(datum.delete(:otel_token)) if datum.include?(:otel_token)
end
rescue StandardError => e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Instrumentation
module Faraday
module Middlewares
# TracerMiddleware propagates context and instruments Faraday requests
# by way of its middlware system
# by way of its middleware system
class TracerMiddleware < ::Faraday::Middleware
HTTP_METHODS_SYMBOL_TO_STRING = {
connect: 'CONNECT',
Expand All @@ -23,6 +23,9 @@ class TracerMiddleware < ::Faraday::Middleware
trace: 'TRACE'
}.freeze

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

def call(env)
http_method = HTTP_METHODS_SYMBOL_TO_STRING[env.method]
config = Faraday::Instrumentation.instance.config
Expand Down Expand Up @@ -68,7 +71,7 @@ def tracer

def trace_response(span, status)
span.set_attribute('http.status_code', status)
span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(status.to_i)
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(status.to_i)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module HTTP
module Patches
# Module to prepend to HTTP::Client for instrumentation
module Client
# Constant for the HTTP status range
HTTP_STATUS_SUCCESS_RANGE = (100..399)

def perform(req, options)
uri = req.uri
request_method = req.verb.to_s.upcase
Expand Down Expand Up @@ -43,7 +46,7 @@ def annotate_span_with_response!(span, response)

status_code = response.status.to_i
span.set_attribute('http.status_code', status_code)
span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(status_code.to_i)
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(status_code)
end

def create_request_span_name(request_method, request_path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module HttpClient
module Patches
# Module to prepend to HTTPClient for instrumentation
module Client
# Constant for the HTTP status range
HTTP_STATUS_SUCCESS_RANGE = (100..399)

private

def do_get_block(req, proxy, conn, &block)
Expand Down Expand Up @@ -42,7 +45,7 @@ def annotate_span_with_response!(span, response)
status_code = response.status_code.to_i

span.set_attribute('http.status_code', status_code)
span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(status_code.to_i)
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(status_code)
end

def tracer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module Plugin
# Instruments around HTTPX's request/response lifecycle in order to generate
# an OTEL trace.
class RequestTracer
# Constant for the HTTP status range
HTTP_STATUS_SUCCESS_RANGE = (100..399)

def initialize(request)
@request = request
end
Expand Down Expand Up @@ -54,7 +57,7 @@ def finish(response)
@span.status = Trace::Status.error("Unhandled exception of type: #{response.error.class}")
else
@span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, response.status)
@span.status = Trace::Status.error unless (100..399).cover?(response.status)
@span.status = Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(response.status)
end

OpenTelemetry::Context.detach(@trace_token) if @trace_token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ module Instrumentation
HTTP_METHODS_TO_SPAN_NAMES = Hash.new { |h, k| h[k] = "HTTP #{k}" }
USE_SSL_TO_SCHEME = { false => 'http', true => 'https' }.freeze

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

def request(req, body = nil, &block)
# Do not trace recursive call for starting the connection
return super unless started?
Expand Down Expand Up @@ -78,7 +81,7 @@ def annotate_span_with_response!(span, response)
status_code = response.code.to_i

span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, status_code)
span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(status_code.to_i)
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(status_code)
end

def tracer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module RestClient
module Patches
# Module to prepend to RestClient::Request for instrumentation
module Request
# Constant for the HTTP status range
HTTP_STATUS_SUCCESS_RANGE = (100..399)

def execute(&block)
trace_request do |_span|
super
Expand Down Expand Up @@ -49,13 +52,12 @@ def trace_request
# If so, add additional attributes.
if response.is_a?(::RestClient::Response)
span.set_attribute('http.status_code', response.code)
span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(response.code.to_i)
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(response.code.to_i)
end
end
rescue ::RestClient::ExceptionWithResponse => e
span.set_attribute('http.status_code', e.http_code)
span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(e.http_code.to_i)

span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(e.http_code.to_i)
raise e
ensure
span.finish
Expand Down
Loading