Skip to content

Commit

Permalink
Merge pull request #3303 from alexevanczuk/ae-add-ability-to-configur…
Browse files Browse the repository at this point in the history
…e-pg-error-handler
  • Loading branch information
marcotc committed Dec 6, 2023
2 parents ff53df3 + 243664c commit f3358c0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
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? }` |
### 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)
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 @@ -99,10 +99,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]
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 @@ -171,6 +171,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

0 comments on commit f3358c0

Please sign in to comment.