Skip to content

Commit da75015

Browse files
adinhodovicxrmxshalevr
authored
fix: Ensure compability with Psycopg3 to extract libpq build version (#2500)
* fix: Ensure compability with Psycopg3 to extract libpq build version Struggling with getting dbapi and psycopg3 working. Think this is the error, __libpq_version does not exist on psycopg3 https://github.com/psycopg/psycopg/blob/master/psycopg/psycopg/pq/pq_ctypes.py#L1220 * docs: Add changelog entry * docs: Fix spelling --------- Co-authored-by: Riccardo Magliocchetti <[email protected]> Co-authored-by: Shalev Roda <[email protected]>
1 parent c28f9b8 commit da75015

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

Diff for: CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3939

4040
### Fixed
4141

42+
- `opentelemetry-instrumentation-dbapi` Fix compatibility with Psycopg3 to extract libpq build version (#2500)[https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2500]
4243
- `opentelemetry-instrumentation-grpc` AioClientInterceptor should propagate with a Metadata object
4344
([#2363](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2363))
4445
- `opentelemetry-instrumentation-boto3sqs` Instrument Session and resource
@@ -126,7 +127,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
126127
([#1959](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1959))
127128
- `opentelemetry-resource-detector-azure` Added dependency for Cloud Resource ID attribute
128129
([#2072](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2072))
129-
130+
130131
## Version 1.21.0/0.42b0 (2023-11-01)
131132

132133
### Added
@@ -1540,4 +1541,3 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
15401541
- `opentelemetry-resource-detector-azure` Suppress instrumentation for `urllib` call
15411542
([#2178](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2178))
15421543
- AwsLambdaInstrumentor handles and re-raises function exception ([#2245](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2245))
1543-

Diff for: instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -427,12 +427,19 @@ def traced_execution(
427427
if args and self._commenter_enabled:
428428
try:
429429
args_list = list(args)
430+
if hasattr(self._connect_module, "__libpq_version__"):
431+
libpq_version = self._connect_module.__libpq_version__
432+
else:
433+
libpq_version = (
434+
self._connect_module.pq.__build_version__
435+
)
436+
430437
commenter_data = {
431438
# Psycopg2/framework information
432439
"db_driver": f"psycopg2:{self._connect_module.__version__.split(' ')[0]}",
433440
"dbapi_threadsafety": self._connect_module.threadsafety,
434441
"dbapi_level": self._connect_module.apilevel,
435-
"libpq_version": self._connect_module.__libpq_version__,
442+
"libpq_version": libpq_version,
436443
"driver_paramstyle": self._connect_module.paramstyle,
437444
}
438445
if self._commenter_options.get(

Diff for: instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py

+26
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,32 @@ def test_executemany_comment(self):
275275
r"Select 1 /\*dbapi_threadsafety=123,driver_paramstyle='test',libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
276276
)
277277

278+
def test_compatible_build_version_psycopg_psycopg2_libpq(self):
279+
connect_module = mock.MagicMock()
280+
connect_module.__version__ = mock.MagicMock()
281+
connect_module.pq = mock.MagicMock()
282+
connect_module.pq.__build_version__ = 123
283+
connect_module.apilevel = 123
284+
connect_module.threadsafety = 123
285+
connect_module.paramstyle = "test"
286+
287+
db_integration = dbapi.DatabaseApiIntegration(
288+
"testname",
289+
"testcomponent",
290+
enable_commenter=True,
291+
commenter_options={"db_driver": False, "dbapi_level": False},
292+
connect_module=connect_module,
293+
)
294+
mock_connection = db_integration.wrapped_connection(
295+
mock_connect, {}, {}
296+
)
297+
cursor = mock_connection.cursor()
298+
cursor.executemany("Select 1;")
299+
self.assertRegex(
300+
cursor.query,
301+
r"Select 1 /\*dbapi_threadsafety=123,driver_paramstyle='test',libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;",
302+
)
303+
278304
def test_executemany_flask_integration_comment(self):
279305
connect_module = mock.MagicMock()
280306
connect_module.__version__ = mock.MagicMock()

0 commit comments

Comments
 (0)