Skip to content

[BUG]: ddtrace reports errors for individual SQL statements #13886

Open
@patrys

Description

@patrys

Tracer Version(s)

2.18.1

Python Version(s)

Python 3.12.11

Pip Version(s)

pip 25.0.1

Bug Report

A typical concurrency-safe pattern for ensuring an object exists in the database works like this:

  1. Create a transaction savepoint
  2. Run a SELECT
  3. If SELECT returns no rows, attempt an INSERT
  4. If INSERT violates a unique index, attempt a second SELECT

Now, ddtrace instrumentation hooks into the psycopg driver, reports that failing INSERT as an error span, and Datadog proceeds to open an issue under error reporting (and charging us for it). This is a handled problem, the Python code surrounding it handles it gracefully, and it is a common enough pattern that it's part of the Django ORM.

The only workaround I found is to disable the database instrumentation completely, which is not something I want to do.

Reproduction Code

  1. Create a database table with a unique index.
  2. Write Python code that attempts to insert the same value twice.
  3. Run said Python code with ddtrace instrumentation enabled.
  4. Observe that an issue is created in the Errors tab in Datadog.

Error Logs

No response

Libraries in Use

Django==5.2.1
psycopg==3.2.6

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions