Skip to content

Commit

Permalink
Merge pull request #3316 from DataDog/tonycthsu/mysql2-on-error
Browse files Browse the repository at this point in the history
Add `on_error` settings for `mysql2`
  • Loading branch information
TonyCTHsu committed Dec 19, 2023
2 parents bb96f8f + 863c9ec commit e6c48b0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
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
4 changes: 4 additions & 0 deletions lib/datadog/tracing/contrib/mysql2/configuration/settings.rb
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

0 comments on commit e6c48b0

Please sign in to comment.