Skip to content

Commit

Permalink
Main
Browse files Browse the repository at this point in the history
Created a readable constant in 8 files to freeze ranges 100..399 .

Relate to open-telemetry#1172
  • Loading branch information
Victorsesan committed Oct 19, 2024
1 parent c2ffafc commit 224f1ea
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 20 deletions.
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).freeze

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
@@ -1,5 +1,4 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0
Expand All @@ -21,11 +20,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).freeze

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 +36,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 +64,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 +79,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 +88,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).freeze

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).freeze

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).freeze

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).freeze

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).freeze

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).freeze

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

0 comments on commit 224f1ea

Please sign in to comment.