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 ability to configure pg error_handler #3303

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
1 change: 1 addition & 0 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,7 @@ end
| `service_name` | `DD_TRACE_PG_SERVICE_NAME` | Name of application running the `pg` instrumentation. May be overridden by `global_default_service_name`. [See *Additional Configuration* for more details](#additional-configuration) | `pg` |
| `peer_service` | `DD_TRACE_PG_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'` |
| `error_handler` | Custom error handler invoked when a job raises an error. Provided `span` and `error` as arguments. Sets error on the span by default. Useful for ignoring errors from Postgres that are handled at the application level. | `proc { |span, error| span.set_error(error) unless span.nil? }` |
alexevanczuk marked this conversation as resolved.
Show resolved Hide resolved

### Presto

Expand Down
5 changes: 5 additions & 0 deletions lib/datadog/tracing/contrib/pg/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class Settings < Contrib::Configuration::Settings
o.default false
end

option :error_handler do |o|
o.type :proc
o.default_proc(&Tracing::SpanOperation::Events::DEFAULT_ON_ERROR)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best I can tell this is the literal constant that represented the existing default behavior, so I wanted to reference it explicitly here to maintain existing behavior.

end

option :analytics_sample_rate do |o|
o.type :float
o.env Ext::ENV_ANALYTICS_SAMPLE_RATE
Expand Down
2 changes: 2 additions & 0 deletions lib/datadog/tracing/contrib/pg/instrumentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ def sync_exec_prepared(statement_name, params, *args, &block)

def trace(name, sql: nil, statement_name: nil, block: nil)
service = Datadog.configuration_for(self, :service_name) || datadog_configuration[:service_name]
error_handler = datadog_configuration[:error_handler]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let me know if there is another standard way to get the error handler here.

resource = statement_name || sql

Tracing.trace(
name,
on_error: error_handler,
service: service,
resource: resource,
type: Tracing::Metadata::Ext::SQL::TYPE
Expand Down
11 changes: 11 additions & 0 deletions spec/datadog/tracing/contrib/pg/patcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@
it_behaves_like 'configured peer service span', 'DD_TRACE_PG_PEER_SERVICE', error: PG::Error do
let(:configuration_options) { {} }
end

context 'when there is custom error handling' do
let(:configuration_options) { { error_handler: error_handler } }
let(:error_handler) { ->(_span, _error) { false } }

it 'calls the error handler' do
expect { exec }.to raise_error(PG::Error)
expect(spans.count).to eq(1)
expect(span).to_not have_error
end
end
end
end

Expand Down