Skip to content

Commit

Permalink
Merge pull request #4066 from bill-kolokithas/elasticsearch_on_error_…
Browse files Browse the repository at this point in the history
…support

Add config support for "on_error" in Elasticsearch tracing
  • Loading branch information
TonyCTHsu authored Nov 26, 2024
2 parents 3f8615b + 72bdaad commit ce4393e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ Datadog.configure_onto(client.transport, **options)
| `service_name` | `DD_TRACE_ELASTICSEARCH_SERVICE_NAME` | `String` | Name of application running the `elasticsearch` instrumentation. May be overridden by `global_default_service_name`. [See _Additional Configuration_ for more details](#additional-configuration) | `elasticsearch` |
| `peer_service` | `DD_TRACE_ELASTICSEARCH_PEER_SERVICE` | `String` | Name of external service the application connects to | `nil` |
| `quantize` | | `Hash` | Hash containing options for quantization. May include `:show` with an Array of keys to not quantize (or `:all` to skip quantization), or `:exclude` with Array of keys to exclude entirely. | `{}` |
| `on_error` | | `Proc` | Custom error handler invoked when a request raises an error. Provides `span` and `error` as arguments. Sets error on the span by default. Useful for ignoring transient errors. | `proc { \|span, error\| span.set_error(error) unless span.nil? }` |

### Ethon

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class Settings < Contrib::Configuration::Settings
o.type :string, nilable: true
o.env Ext::ENV_PEER_SERVICE
end

option :on_error do |o|
o.type :proc, nilable: true
end
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion lib/datadog/tracing/contrib/elasticsearch/patcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def perform_request(*args)
# `Client#transport` is the most convenient object both for this integration and the library
# as users have shared access to it across all `elasticsearch` versions.
service ||= Datadog.configuration_for(transport, :service_name) || datadog_configuration[:service_name]
on_error = Datadog.configuration_for(transport, :on_error) || datadog_configuration[:on_error]

method = args[0]
path = args[1]
Expand All @@ -49,7 +50,11 @@ def perform_request(*args)
url = full_url.path
response = nil

Tracing.trace(Datadog::Tracing::Contrib::Elasticsearch::Ext::SPAN_QUERY, service: service) do |span|
Tracing.trace(
Datadog::Tracing::Contrib::Elasticsearch::Ext::SPAN_QUERY,
service: service,
on_error: on_error
) do |span|
begin
connection = transport.connections.first
host = connection.host[:host] if connection
Expand Down
30 changes: 30 additions & 0 deletions spec/datadog/tracing/contrib/elasticsearch/patcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,36 @@
end
end

describe 'get request' do
context 'when requesting a document that does not exist' do
let(:index_name) { 'some_index' }
let(:document_id) { 999 }
let(:exception_class) do
if defined?(Elastic::Transport) # version >= 8
Elastic::Transport::Transport::Errors::NotFound
else # version < 8
Elasticsearch::Transport::Transport::Errors::NotFound
end
end

subject(:request) { client.perform_request 'GET', "#{index_name}/_doc/#{document_id}" }

it 'marks span with error' do
expect { request }.to raise_error(exception_class)
expect(span).to have_error
end

context 'when configured_with `on_error`' do
let(:configuration_options) { { on_error: ->(_span, _error) { false } } }

it 'does not mark span with error' do
expect { request }.to raise_error(exception_class)
expect(span).not_to have_error
end
end
end
end

describe 'indexing request' do
let(:document_body) do
{
Expand Down

0 comments on commit ce4393e

Please sign in to comment.