Skip to content

Commit d96038c

Browse files
fix: use psycopg2 DSN parser to parse DSN [backport #5272 to 1.9] (#5283)
Backport of #5272 to 1.9 This change fixes an issue with DSN parsing with the psycopg2 integration, likely introduced after the refactor of #4294. Fixes #5269. ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines) are followed. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Author is aware of the performance implications of this PR as reported in the benchmarks PR comment. ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer is aware of, and discussed the performance implications of this PR as reported in the benchmarks PR comment. Co-authored-by: Gabriele N. Tornetta <[email protected]>
1 parent b2830b3 commit d96038c

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

ddtrace/contrib/psycopg/connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from psycopg2.extensions import connection
77
from psycopg2.extensions import cursor
8+
from psycopg2.extensions import parse_dsn
89

910
from ddtrace import config
1011
from ddtrace.internal.constants import COMPONENT
@@ -13,7 +14,6 @@
1314
from ...ext import SpanTypes
1415
from ...ext import db
1516
from ...ext import net
16-
from ...ext import sql
1717

1818

1919
class TracedCursor(cursor):
@@ -60,7 +60,7 @@ def __init__(self, *args, **kwargs):
6060
super(TracedConnection, self).__init__(*args, **kwargs)
6161

6262
# add metadata (from the connection, string, etc)
63-
dsn = sql.parse_pg_dsn(self.dsn)
63+
dsn = parse_dsn(self.dsn)
6464
self._datadog_tags = {
6565
net.TARGET_HOST: dsn.get("host"),
6666
net.TARGET_PORT: dsn.get("port"),

ddtrace/contrib/psycopg/patch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
import psycopg2
4+
from psycopg2.extensions import parse_dsn
45
from psycopg2.sql import Composable
56

67
from ddtrace import Pin
@@ -11,7 +12,6 @@
1112
from ddtrace.ext import SpanTypes
1213
from ddtrace.ext import db
1314
from ddtrace.ext import net
14-
from ddtrace.ext import sql
1515
from ddtrace.internal.constants import COMPONENT
1616
from ddtrace.vendor import wrapt
1717

@@ -105,7 +105,7 @@ def patch_conn(conn, traced_conn_cls=Psycopg2TracedConnection):
105105
c = traced_conn_cls(conn)
106106

107107
# fetch tags from the dsn
108-
dsn = sql.parse_pg_dsn(conn.dsn)
108+
dsn = parse_dsn(conn.dsn)
109109
tags = {
110110
net.TARGET_HOST: dsn.get("host"),
111111
net.TARGET_PORT: dsn.get("port"),
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
psycopg2: Fixes a bug with DSN parsing integration.

0 commit comments

Comments
 (0)