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

Add on_error settings for mysql2 #3316

Merged
merged 4 commits into from
Dec 19, 2023
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
4 changes: 2 additions & 2 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -1218,8 +1218,8 @@ client.query("SELECT * FROM users WHERE group='x'")
|-----------------------|--------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------|
| `service_name` | `DD_TRACE_MYSQL2_SERVICE_NAME` | Name of application running the `mysql2` instrumentation. May be overridden by `global_default_service_name`. [See *Additional Configuration* for more details](#additional-configuration) | `mysql2` |
| `peer_service` | `DD_TRACE_MYSQL2_PEER_SERVICE` | Name of external service the application connects to | `nil` |
| `comment_propagation` | `DD_DBM_PROPAGATION_MODE` | SQL comment propagation mode for database monitoring. <br />(example: `disabled` \| `service`\| `full`). <br /><br />**Important**: *Note that enabling sql comment propagation results in potentially confidential data (service names) being stored in the databases which can then be accessed by other 3rd parties that have been granted access to the database.* | `'disabled'` |

| `comment_propagation` | `DD_DBM_PROPAGATION_MODE` | SQL comment propagation mode for database monitoring. <br />(example: `disabled` \| `service`\| `full`). <br /><br />**Important**: *Note that enabling SQL comment propagation results in potentially confidential data (service names) being stored in the databases which can then be accessed by other third parties that have been granted access to the database.* | `'disabled'` |
| `on_error` | | Custom error handler invoked when MySQL raises an error. Provided `span` and `error` as arguments. Sets error on the span by default. Useful for ignoring errors that are handled at the application level. | `proc { \|span, error\| span.set_error(error) unless span.nil? }` |
### Net/HTTP

The Net/HTTP integration will trace any HTTP call using the standard lib Net::HTTP module.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,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
3 changes: 2 additions & 1 deletion lib/datadog/tracing/contrib/mysql2/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ def self.included(base)
module InstanceMethods
def query(sql, options = {})
service = Datadog.configuration_for(self, :service_name) || datadog_configuration[:service_name]
on_error = Datadog.configuration_for(self, :on_error) || datadog_configuration[:on_error]

Tracing.trace(Ext::SPAN_QUERY, service: service) do |span, trace_op|
Tracing.trace(Ext::SPAN_QUERY, service: service, on_error: on_error) do |span, trace_op|
span.resource = sql
span.span_type = Tracing::Metadata::Ext::SQL::TYPE

Expand Down
23 changes: 23 additions & 0 deletions spec/datadog/tracing/contrib/mysql2/patcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@
end

it_behaves_like 'with sql comment propagation', span_op_name: 'mysql2.query'

context 'when configured with `on_error`' do
before do
Datadog.configure_onto(client, on_error: ->(_span, _error) { false })
end

let(:sql_statement) { 'SELECT INVALID' }

it 'does not mark span with error' do
expect { query }.to raise_error(Mysql2::Error)

expect(span).not_to have_error
end
end
end

context 'when a successful query is made' do
Expand Down Expand Up @@ -149,6 +163,15 @@
it_behaves_like 'configured peer service span', 'DD_TRACE_MYSQL2_PEER_SERVICE', error: Mysql2::Error do
let(:configuration_options) { {} }
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 { query }.to raise_error(Mysql2::Error)
expect(span).not_to have_error
end
end
end
end
end
Expand Down
Loading