Skip to content

Commit

Permalink
add option to not report 4xx as errors for Grape
Browse files Browse the repository at this point in the history
  • Loading branch information
mkigikm authored and zachmccormick committed Mar 23, 2020
1 parent 3322c65 commit 9fb56bf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/ddtrace/contrib/grape/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Settings < Contrib::Configuration::Settings

option :enabled, default: true
option :service_name, default: Ext::SERVICE_NAME
option :error_for_4xx, default: true
end
end
end
Expand Down
18 changes: 17 additions & 1 deletion lib/ddtrace/contrib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ def endpoint_run(name, start, finish, id, payload)
end

# catch thrown exceptions
span.set_error(payload[:exception_object]) unless payload[:exception_object].nil?
if exception_is_error?(payload[:exception_object])
span.set_error(payload[:exception_object])
end

# override the current span with this notification values
span.set_tag(Ext::TAG_ROUTE_ENDPOINT, api_view) unless api_view.nil?
Expand Down Expand Up @@ -177,6 +179,10 @@ def service_name
datadog_configuration[:service_name]
end

def error_for_4xx?
datadog_configuration[:error_for_4xx]
end

def analytics_enabled?
Contrib::Analytics.enabled?(datadog_configuration[:analytics_enabled])
end
Expand All @@ -192,6 +198,16 @@ def enabled?
def datadog_configuration
Datadog.configuration[:grape]
end

def exception_is_error?(exception)
return false unless exception

if !error_for_4xx? && defined?(::Grape::Exceptions::Base) && exception.class < ::Grape::Exceptions::Base
status = exception.status
end

status.nil? || status > 499
end
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions test/contrib/grape/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ def test_traced_api_exception
assert_nil(run.parent)
end

def test_traced_api_4xx_exception_report_no_error
Datadog.configure do |c|
c.use :grape, error_for_4xx: false
end
post '/base/hard_failure'

spans = @tracer.writer.spans()
render = spans[0]
assert_equal(render.status, 0)

Datadog.configure do |c|
c.use :grape, error_for_4xx: true
end
end

def test_mine_4xx_exception_report_error
post '/base/hard_failure'

spans = @tracer.writer.spans()
render = spans[0]
assert_equal(render.status, 1)
end

def test_traced_api_before_after_filters
# it should trace the endpoint body and all before/after filters
get '/filtered/before_after'
Expand Down

0 comments on commit 9fb56bf

Please sign in to comment.