Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faraday: Add on_error option #3431

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ connection.get('/foo')
| `distributed_tracing` | | Enables [distributed tracing](#distributed-tracing) | `true` |
| `split_by_domain` | | Uses the request domain as the service name when set to `true`. | `false` |
| `error_handler` | | A `Proc` that accepts a `response` parameter. If it evaluates to a *truthy* value, the trace span is marked as an error. By default only sets 5XX responses as errors. | `nil` |

| `on_error` | Custom error handler invoked when a request raises an error. Provided `span` and `error` as arguments. Sets an error on the span by default. | `proc { \|span, error\| span.set_error(error) unless span.nil? }` |

### Grape

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ class Settings < Contrib::Configuration::Settings
end

option :distributed_tracing, default: true, type: :bool

option :error_handler do |o|
o.type :proc
o.default_proc(&DEFAULT_ERROR_HANDLER)
end

option :on_error do |o|
o.type :proc, nilable: true
end

option :split_by_domain, default: false, type: :bool

option :service_name do |o|
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/tracing/contrib/faraday/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def call(env)
# Do this once to reduce expensive regex calls.
request_options = build_request_options!(env)

Tracing.trace(Ext::SPAN_REQUEST) do |span, trace|
Tracing.trace(Ext::SPAN_REQUEST, on_error: request_options[:on_error]) do |span, trace|
annotate!(span, env, request_options)
propagate!(trace, span, env) if request_options[:distributed_tracing] && Tracing.enabled?
app.call(env).on_complete { |resp| handle_response(span, resp, request_options) }
Expand Down
11 changes: 11 additions & 0 deletions spec/datadog/tracing/contrib/faraday/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,17 @@
expect(span.get_tag('span.kind')).to eq('client')
end

context 'when given `on_error`' do
let(:configuration_options) { { on_error: proc { @error_handler_called = true } } }

it do
expect { response }.to raise_error(Faraday::ConnectionFailed)

expect(span).not_to have_error
expect(@error_handler_called).to be_truthy
end
end

it_behaves_like 'a peer service span' do
let(:peer_service_val) { 'example.com' }
let(:peer_service_source) { 'peer.hostname' }
Expand Down
Loading