-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1238 from Kyle-Neale/grape_status_codes
Grape status codes
- Loading branch information
Showing
5 changed files
with
225 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require 'set' | ||
require 'ddtrace/ext/http' | ||
|
||
module Datadog | ||
module Contrib | ||
# Contains methods helpful for tracing/annotating HTTP request libraries | ||
class StatusCodeMatcher | ||
REGEX_PARSER = /^\d{3}(?:-\d{3})?(?:,\d{3}(?:-\d{3})?)*$/ | ||
|
||
def initialize(range) | ||
@error_response_range = range | ||
set_range | ||
end | ||
|
||
def include?(exception_status) | ||
set_range.include?(exception_status) | ||
end | ||
|
||
def to_s | ||
@error_response_range.to_s | ||
end | ||
|
||
private | ||
|
||
def set_range | ||
@datadog_set ||= begin | ||
set = Set.new | ||
handle_statuses.each do |statuses| | ||
status = statuses.to_s.split('-') | ||
if status.length == 1 | ||
set.add(Integer(status[0])) | ||
elsif status.length == 2 | ||
min, max = status.minmax | ||
Array(min..max).each do |i| | ||
set.add(Integer(i)) | ||
end | ||
end | ||
end | ||
set | ||
end | ||
@datadog_set | ||
end | ||
|
||
def error_responses | ||
return @error_response_range if @error_response_range.is_a?(String) && !@error_response_range.nil? | ||
@error_response_range.join(',') if @error_response_range.is_a?(Array) && !@error_response_range.empty? | ||
end | ||
|
||
def handle_statuses | ||
if error_responses | ||
filter_error_responses = error_responses.gsub(/\s+/, '').split(',').select do |code| | ||
if !code.to_s.match(REGEX_PARSER) | ||
Datadog.logger.debug("Invalid config provided: #{code}. Must be formatted like '400-403,405,410-499'.") | ||
next | ||
else | ||
true | ||
end | ||
end | ||
filter_error_responses.empty? ? Datadog::Ext::HTTP::ERROR_RANGE.to_a : filter_error_responses | ||
else | ||
Datadog.logger.debug('No valid config was provided for :error_statuses - falling back to default.') | ||
Datadog::Ext::HTTP::ERROR_RANGE.to_a | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters