Skip to content

Commit 224f1ea

Browse files
committed
Main
Created a readable constant in 8 files to freeze ranges 100..399 . Relate to #1172
1 parent c2ffafc commit 224f1ea

File tree

8 files changed

+34
-20
lines changed
  • instrumentation
    • ethon/lib/opentelemetry/instrumentation/ethon/patches
    • excon/lib/opentelemetry/instrumentation/excon/middlewares
    • faraday/lib/opentelemetry/instrumentation/faraday/middlewares
    • http_client/lib/opentelemetry/instrumentation/http_client/patches
    • httpx/lib/opentelemetry/instrumentation/httpx
    • http/lib/opentelemetry/instrumentation/http/patches
    • net_http/lib/opentelemetry/instrumentation/net/http/patches
    • restclient/lib/opentelemetry/instrumentation/restclient/patches

8 files changed

+34
-20
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ module Easy
1616
end
1717
HTTP_METHODS_TO_SPAN_NAMES = Hash.new { |h, k| h[k] = "HTTP #{k}" }
1818

19+
# Constant for the HTTP status range
20+
HTTP_STATUS_SUCCESS_RANGE = (100..399).freeze
21+
1922
def http_request(url, action_name, options = {})
2023
@otel_method = ACTION_NAMES_TO_HTTP_METHODS[action_name]
2124
super
@@ -42,7 +45,7 @@ def complete
4245
@otel_span.status = OpenTelemetry::Trace::Status.error("Request has failed: #{message}")
4346
else
4447
@otel_span.set_attribute('http.status_code', response_code)
45-
@otel_span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(response_code.to_i)
48+
@otel_span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(response_code.to_i)
4649
end
4750
ensure
4851
@otel_span&.finish

instrumentation/excon/lib/opentelemetry/instrumentation/excon/middlewares/tracer_middleware.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# frozen_string_literal: true
2-
32
# Copyright The OpenTelemetry Authors
43
#
54
# SPDX-License-Identifier: Apache-2.0
@@ -21,11 +20,13 @@ class TracerMiddleware < ::Excon::Middleware::Base
2120
hash[uppercase_method] ||= "HTTP #{uppercase_method}"
2221
end.freeze
2322

23+
# Constant for the HTTP status range
24+
HTTP_STATUS_SUCCESS_RANGE = (100..399).freeze
25+
2426
def request_call(datum)
2527
return @stack.request_call(datum) if untraced?(datum)
2628

2729
http_method = HTTP_METHODS_TO_UPPERCASE[datum[:method]]
28-
2930
attributes = {
3031
OpenTelemetry::SemanticConventions::Trace::HTTP_HOST => datum[:host],
3132
OpenTelemetry::SemanticConventions::Trace::HTTP_METHOD => http_method,
@@ -35,19 +36,14 @@ def request_call(datum)
3536
OpenTelemetry::SemanticConventions::Trace::NET_PEER_NAME => datum[:hostname],
3637
OpenTelemetry::SemanticConventions::Trace::NET_PEER_PORT => datum[:port]
3738
}
38-
3939
peer_service = Excon::Instrumentation.instance.config[:peer_service]
4040
attributes[OpenTelemetry::SemanticConventions::Trace::PEER_SERVICE] = peer_service if peer_service
4141
attributes.merge!(OpenTelemetry::Common::HTTP::ClientContext.attributes)
42-
4342
span = tracer.start_span(HTTP_METHODS_TO_SPAN_NAMES[http_method], attributes: attributes, kind: :client)
4443
ctx = OpenTelemetry::Trace.context_with_span(span)
45-
4644
datum[:otel_span] = span
4745
datum[:otel_token] = OpenTelemetry::Context.attach(ctx)
48-
4946
OpenTelemetry.propagation.inject(datum[:headers])
50-
5147
@stack.request_call(datum)
5248
end
5349

@@ -68,7 +64,6 @@ def self.around_default_stack
6864
# If the default stack contains a version of the trace middleware already...
6965
existing_trace_middleware = default_stack.find { |m| m <= TracerMiddleware }
7066
default_stack.delete(existing_trace_middleware) if existing_trace_middleware
71-
7267
# Inject after the ResponseParser middleware
7368
response_middleware_index = default_stack.index(::Excon::Middleware::ResponseParser).to_i
7469
default_stack.insert(response_middleware_index + 1, self)
@@ -84,7 +79,7 @@ def handle_response(datum)
8479
if datum.key?(:response)
8580
response = datum[:response]
8681
span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, response[:status])
87-
span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(response[:status].to_i)
82+
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(response[:status].to_i)
8883
end
8984

9085
if datum.key?(:error)
@@ -93,7 +88,6 @@ def handle_response(datum)
9388
end
9489

9590
span.finish
96-
9791
OpenTelemetry::Context.detach(datum.delete(:otel_token)) if datum.include?(:otel_token)
9892
end
9993
rescue StandardError => e

instrumentation/faraday/lib/opentelemetry/instrumentation/faraday/middlewares/tracer_middleware.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module Instrumentation
99
module Faraday
1010
module Middlewares
1111
# TracerMiddleware propagates context and instruments Faraday requests
12-
# by way of its middlware system
12+
# by way of its middleware system
1313
class TracerMiddleware < ::Faraday::Middleware
1414
HTTP_METHODS_SYMBOL_TO_STRING = {
1515
connect: 'CONNECT',
@@ -23,6 +23,9 @@ class TracerMiddleware < ::Faraday::Middleware
2323
trace: 'TRACE'
2424
}.freeze
2525

26+
# Constant for the HTTP status range
27+
HTTP_STATUS_SUCCESS_RANGE = (100..399).freeze
28+
2629
def call(env)
2730
http_method = HTTP_METHODS_SYMBOL_TO_STRING[env.method]
2831
config = Faraday::Instrumentation.instance.config
@@ -68,7 +71,7 @@ def tracer
6871

6972
def trace_response(span, status)
7073
span.set_attribute('http.status_code', status)
71-
span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(status.to_i)
74+
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(status.to_i)
7275
end
7376
end
7477
end

instrumentation/http/lib/opentelemetry/instrumentation/http/patches/client.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ module HTTP
1010
module Patches
1111
# Module to prepend to HTTP::Client for instrumentation
1212
module Client
13+
# Constant for the HTTP status range
14+
HTTP_STATUS_SUCCESS_RANGE = (100..399).freeze
15+
1316
def perform(req, options)
1417
uri = req.uri
1518
request_method = req.verb.to_s.upcase
@@ -43,7 +46,7 @@ def annotate_span_with_response!(span, response)
4346

4447
status_code = response.status.to_i
4548
span.set_attribute('http.status_code', status_code)
46-
span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(status_code.to_i)
49+
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(status_code)
4750
end
4851

4952
def create_request_span_name(request_method, request_path)

instrumentation/http_client/lib/opentelemetry/instrumentation/http_client/patches/client.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ module HttpClient
1010
module Patches
1111
# Module to prepend to HTTPClient for instrumentation
1212
module Client
13+
# Constant for the HTTP status range
14+
HTTP_STATUS_SUCCESS_RANGE = (100..399).freeze
15+
1316
private
1417

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

4447
span.set_attribute('http.status_code', status_code)
45-
span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(status_code.to_i)
48+
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(status_code)
4649
end
4750

4851
def tracer

instrumentation/httpx/lib/opentelemetry/instrumentation/httpx/plugin.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ module Plugin
1111
# Instruments around HTTPX's request/response lifecycle in order to generate
1212
# an OTEL trace.
1313
class RequestTracer
14+
# Constant for the HTTP status range
15+
HTTP_STATUS_SUCCESS_RANGE = (100..399).freeze
16+
1417
def initialize(request)
1518
@request = request
1619
end
@@ -54,7 +57,7 @@ def finish(response)
5457
@span.status = Trace::Status.error("Unhandled exception of type: #{response.error.class}")
5558
else
5659
@span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, response.status)
57-
@span.status = Trace::Status.error unless (100..399).cover?(response.status)
60+
@span.status = Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(response.status)
5861
end
5962

6063
OpenTelemetry::Context.detach(@trace_token) if @trace_token

instrumentation/net_http/lib/opentelemetry/instrumentation/net/http/patches/instrumentation.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ module Instrumentation
1414
HTTP_METHODS_TO_SPAN_NAMES = Hash.new { |h, k| h[k] = "HTTP #{k}" }
1515
USE_SSL_TO_SCHEME = { false => 'http', true => 'https' }.freeze
1616

17+
# Constant for the HTTP status range
18+
HTTP_STATUS_SUCCESS_RANGE = (100..399).freeze
19+
1720
def request(req, body = nil, &block)
1821
# Do not trace recursive call for starting the connection
1922
return super unless started?
@@ -78,7 +81,7 @@ def annotate_span_with_response!(span, response)
7881
status_code = response.code.to_i
7982

8083
span.set_attribute(OpenTelemetry::SemanticConventions::Trace::HTTP_STATUS_CODE, status_code)
81-
span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(status_code.to_i)
84+
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(status_code)
8285
end
8386

8487
def tracer

instrumentation/restclient/lib/opentelemetry/instrumentation/restclient/patches/request.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ module RestClient
1010
module Patches
1111
# Module to prepend to RestClient::Request for instrumentation
1212
module Request
13+
# Constant for the HTTP status range
14+
HTTP_STATUS_SUCCESS_RANGE = (100..399).freeze
15+
1316
def execute(&block)
1417
trace_request do |_span|
1518
super
@@ -49,13 +52,12 @@ def trace_request
4952
# If so, add additional attributes.
5053
if response.is_a?(::RestClient::Response)
5154
span.set_attribute('http.status_code', response.code)
52-
span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(response.code.to_i)
55+
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(response.code.to_i)
5356
end
5457
end
5558
rescue ::RestClient::ExceptionWithResponse => e
5659
span.set_attribute('http.status_code', e.http_code)
57-
span.status = OpenTelemetry::Trace::Status.error unless (100..399).cover?(e.http_code.to_i)
58-
60+
span.status = OpenTelemetry::Trace::Status.error unless HTTP_STATUS_SUCCESS_RANGE.cover?(e.http_code.to_i)
5961
raise e
6062
ensure
6163
span.finish

0 commit comments

Comments
 (0)