Skip to content

Commit 6ea3035

Browse files
authored
Merge pull request #3344 from DataDog/tonycthsu/fix-error-status-codes
Fix `error_status_codes` options
2 parents be1c557 + 4c68809 commit 6ea3035

17 files changed

Lines changed: 341 additions & 78 deletions

File tree

lib/datadog/tracing/contrib/excon/configuration/settings.rb

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
require_relative '../../../span_operation'
44
require_relative '../../configuration/settings'
5+
require_relative '../../status_range_matcher'
6+
require_relative '../../status_range_env_parser'
57
require_relative '../ext'
68

79
module Datadog
@@ -39,20 +41,11 @@ class Settings < Contrib::Configuration::Settings
3941
option :error_status_codes do |o|
4042
o.env Ext::ENV_ERROR_STATUS_CODES
4143
o.default 400...600
42-
o.env_parser do |value|
43-
values = if value.include?(',')
44-
value.split(',')
45-
else
46-
value.split
47-
end
48-
values.map! do |v|
49-
v.gsub!(/\A[\s,]*|[\s,]*\Z/, '')
50-
51-
v.empty? ? nil : v
52-
end
53-
54-
values.compact!
55-
values
44+
o.setter do |v|
45+
Tracing::Contrib::StatusRangeMatcher.new(v) if v
46+
end
47+
o.env_parser do |v|
48+
Tracing::Contrib::StatusRangeEnvParser.call(v) if v
5649
end
5750
end
5851

lib/datadog/tracing/contrib/faraday/configuration/settings.rb

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
require_relative '../../configuration/settings'
4+
require_relative '../../status_range_matcher'
5+
require_relative '../../status_range_env_parser'
46
require_relative '../ext'
57

68
module Datadog
@@ -42,20 +44,11 @@ class Settings < Contrib::Configuration::Settings
4244
option :error_status_codes do |o|
4345
o.env Ext::ENV_ERROR_STATUS_CODES
4446
o.default 400...600
45-
o.env_parser do |value|
46-
values = if value.include?(',')
47-
value.split(',')
48-
else
49-
value.split
50-
end
51-
values.map! do |v|
52-
v.gsub!(/\A[\s,]*|[\s,]*\Z/, '')
53-
54-
v.empty? ? nil : v
55-
end
56-
57-
values.compact!
58-
values
47+
o.setter do |v|
48+
Tracing::Contrib::StatusRangeMatcher.new(v) if v
49+
end
50+
o.env_parser do |v|
51+
Tracing::Contrib::StatusRangeEnvParser.call(v) if v
5952
end
6053
end
6154

lib/datadog/tracing/contrib/http/configuration/settings.rb

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
require_relative '../../configuration/settings'
4+
require_relative '../../status_range_matcher'
5+
require_relative '../../status_range_env_parser'
46
require_relative '../ext'
57

68
module Datadog
@@ -43,20 +45,11 @@ class Settings < Contrib::Configuration::Settings
4345
option :error_status_codes do |o|
4446
o.env Ext::ENV_ERROR_STATUS_CODES
4547
o.default 400...600
46-
o.env_parser do |value|
47-
values = if value.include?(',')
48-
value.split(',')
49-
else
50-
value.split
51-
end
52-
values.map! do |v|
53-
v.gsub!(/\A[\s,]*|[\s,]*\Z/, '')
54-
55-
v.empty? ? nil : v
56-
end
57-
58-
values.compact!
59-
values
48+
o.setter do |v|
49+
Tracing::Contrib::StatusRangeMatcher.new(v) if v
50+
end
51+
o.env_parser do |v|
52+
Tracing::Contrib::StatusRangeEnvParser.call(v) if v
6053
end
6154
end
6255

lib/datadog/tracing/contrib/httpclient/configuration/settings.rb

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
require_relative '../../configuration/settings'
4+
require_relative '../../status_range_matcher'
5+
require_relative '../../status_range_env_parser'
46
require_relative '../ext'
57

68
module Datadog
@@ -43,20 +45,11 @@ class Settings < Contrib::Configuration::Settings
4345
option :error_status_codes do |o|
4446
o.env Ext::ENV_ERROR_STATUS_CODES
4547
o.default 400...600
46-
o.env_parser do |value|
47-
values = if value.include?(',')
48-
value.split(',')
49-
else
50-
value.split
51-
end
52-
values.map! do |v|
53-
v.gsub!(/\A[\s,]*|[\s,]*\Z/, '')
54-
55-
v.empty? ? nil : v
56-
end
57-
58-
values.compact!
59-
values
48+
o.setter do |v|
49+
Tracing::Contrib::StatusRangeMatcher.new(v) if v
50+
end
51+
o.env_parser do |v|
52+
Tracing::Contrib::StatusRangeEnvParser.call(v) if v
6053
end
6154
end
6255

lib/datadog/tracing/contrib/httprb/configuration/settings.rb

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
require_relative '../../configuration/settings'
4+
require_relative '../../status_range_matcher'
5+
require_relative '../../status_range_env_parser'
46
require_relative '../ext'
57

68
module Datadog
@@ -43,20 +45,11 @@ class Settings < Contrib::Configuration::Settings
4345
option :error_status_codes do |o|
4446
o.env Ext::ENV_ERROR_STATUS_CODES
4547
o.default 400...600
46-
o.env_parser do |value|
47-
values = if value.include?(',')
48-
value.split(',')
49-
else
50-
value.split
51-
end
52-
values.map! do |v|
53-
v.gsub!(/\A[\s,]*|[\s,]*\Z/, '')
54-
55-
v.empty? ? nil : v
56-
end
57-
58-
values.compact!
59-
values
48+
o.setter do |v|
49+
Tracing::Contrib::StatusRangeMatcher.new(v) if v
50+
end
51+
o.env_parser do |v|
52+
Tracing::Contrib::StatusRangeEnvParser.call(v) if v
6053
end
6154
end
6255

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
module Datadog
4+
module Tracing
5+
module Contrib
6+
# Parsing status range from environment variable
7+
class StatusRangeEnvParser
8+
class << self
9+
def call(value)
10+
[].tap do |array|
11+
value.split(',').each do |e|
12+
next unless e
13+
14+
v = e.split('-')
15+
16+
case v.length
17+
when 0 then next
18+
when 1 then array << Integer(v.first)
19+
when 2 then array << (Integer(v.first)..Integer(v.last))
20+
else
21+
Datadog.logger.debug(
22+
"Invalid error_status_codes configuration: Unable to parse #{value}, containing #{v}."
23+
)
24+
next
25+
end
26+
end
27+
end
28+
end
29+
end
30+
end
31+
end
32+
end
33+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
module Datadog
4+
module Tracing
5+
module Contrib
6+
# Useful checking whether the defined range covers status code
7+
class StatusRangeMatcher
8+
def initialize(ranges)
9+
@ranges = Array(ranges)
10+
end
11+
12+
def include?(status)
13+
@ranges.any? do |e|
14+
case e
15+
when Range
16+
e.include? status
17+
when Integer
18+
e == status
19+
end
20+
end
21+
end
22+
end
23+
end
24+
end
25+
end

spec/datadog/tracing/contrib/excon/instrumentation_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,20 @@
187187
expect(request_span).not_to have_error
188188
end
189189
end
190+
191+
context 'when configured from env' do
192+
around do |example|
193+
ClimateControl.modify('DD_TRACE_EXCON_ERROR_STATUS_CODES' => '500-600') do
194+
example.run
195+
end
196+
end
197+
198+
it do
199+
connection.get(path: '/not_found')
200+
201+
expect(span).not_to have_error
202+
end
203+
end
190204
end
191205

192206
context 'when the request times out' do

spec/datadog/tracing/contrib/faraday/middleware_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,20 @@
320320
expect(span).not_to have_error
321321
end
322322
end
323+
324+
context 'when configured from env' do
325+
around do |example|
326+
ClimateControl.modify('DD_TRACE_FARADAY_ERROR_STATUS_CODES' => '500-600') do
327+
example.run
328+
end
329+
end
330+
331+
it do
332+
client.get('not_found')
333+
334+
expect(span).not_to have_error
335+
end
336+
end
323337
end
324338

325339
context 'when split by domain' do

spec/datadog/tracing/contrib/http/request_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
subject(:response) { client.get(path) }
4444
before { stub_request(:any, "#{uri}#{path}").to_return(status: status_code, body: '{}') }
4545

46-
include_examples 'with error status code configuration'
46+
include_examples 'with error status code configuration', env: 'DD_TRACE_HTTP_ERROR_STATUS_CODES'
4747
end
4848

4949
describe '#get' do

0 commit comments

Comments
 (0)