-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Upgrade postgres check to psycopg3 #15411
Merged
Merged
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
dcaee7e
Upgrade postgres integration to use psycopg3
jmeunier28 9793b74
allow for unclean shutdown on timeout to stop job loop threads && mis…
jmeunier28 f2a695c
fix blocking test for pg 9.6
jmeunier28 1d4cb95
fix typo
jmeunier28 1cc2845
defaults for ssl inputs should all be strings
jmeunier28 e2e31b1
set display_default to string
jmeunier28 3b63c9c
fix test_statement_metrics test for 9.6
jmeunier28 e9f1903
wrap test connections in context manager
jmeunier28 fd0987d
fix replication slot test for all pg versions
jmeunier28 5c24f04
style fix for checks base
jmeunier28 e9a2308
add psycopg[binary] to requirements
jmeunier28 380104a
set pscyopg[binary] license
jmeunier28 14dfdf2
set license to orig psycopg lib
jmeunier28 349f6b1
sync licenses
jmeunier28 3b7c879
try using psycopg[c] instead of binary install
jmeunier28 ffcc8a4
review feedback
jmeunier28 6cb39b8
better log msg on check cancellation
jmeunier28 b2c9865
fix flakey cancel tests
jmeunier28 e72c545
move back to psycopg[binary] && try to fix build in omnibus
jmeunier28 302f51b
prevent segfault possibility in all tests
jmeunier28 24f3aca
make RL tests less flakey
jmeunier28 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ prometheus-client,PyPI,Apache-2.0,Copyright 2015 The Prometheus Authors | |
protobuf,PyPI,BSD-3-Clause,Copyright 2008 Google Inc. | ||
protobuf,PyPI,BSD-3-Clause,Copyright 2008 Google Inc. All rights reserved. | ||
psutil,PyPI,BSD-3-Clause,"Copyright (c) 2009, Jay Loden, Dave Daeschler, Giampaolo Rodola'" | ||
psycopg,PyPI,LGPL-3.0-only,Copyright (C) 2020 The Psycopg Team | ||
psycopg2-binary,PyPI,BSD-3-Clause,Copyright 2013 Federico Di Gregorio | ||
psycopg2-binary,PyPI,LGPL-3.0-only,Copyright (C) 2013 Federico Di Gregorio | ||
pyasn1,PyPI,BSD-3-Clause,"Copyright (c) 2005-2019, Ilya Etingof <[email protected]>" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
|
||
import logging | ||
|
||
import psycopg2 | ||
from psycopg.rows import dict_row | ||
|
||
from datadog_checks.base.utils.db.sql import compute_sql_signature | ||
from datadog_checks.base.utils.tracking import tracked_method | ||
|
@@ -16,12 +16,12 @@ | |
PREPARE_STATEMENT_QUERY = 'PREPARE dd_{query_signature} AS {statement}' | ||
|
||
PARAM_TYPES_COUNT_QUERY = '''\ | ||
SELECT CARDINALITY(parameter_types) FROM pg_prepared_statements WHERE name = 'dd_{query_signature}' | ||
SELECT CARDINALITY(parameter_types) as count FROM pg_prepared_statements WHERE name = 'dd_{query_signature}' | ||
''' | ||
|
||
EXECUTE_PREPARED_STATEMENT_QUERY = 'EXECUTE dd_{prepared_statement}({generic_values})' | ||
|
||
EXPLAIN_QUERY = 'SELECT {explain_function}($stmt${statement}$stmt$)' | ||
EXPLAIN_QUERY = 'SELECT {explain_function}($stmt${statement}$stmt$) as explain_statement' | ||
|
||
|
||
def agent_check_getter(self): | ||
|
@@ -81,7 +81,7 @@ def explain_statement(self, dbname, statement, obfuscated_statement): | |
result = self._explain_prepared_statement(dbname, statement, obfuscated_statement, query_signature) | ||
self._deallocate_prepared_statement(dbname, query_signature) | ||
if result: | ||
return result[0][0][0] | ||
return result[0]['explain_statement'][0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do prefer |
||
return None | ||
|
||
def _set_plan_cache_mode(self, dbname): | ||
|
@@ -112,7 +112,10 @@ def _get_number_of_parameters_for_prepared_statement(self, dbname, query_signatu | |
rows = self._execute_query_and_fetch_rows( | ||
dbname, PARAM_TYPES_COUNT_QUERY.format(query_signature=query_signature) | ||
) | ||
return rows[0][0] if rows else 0 | ||
count = 0 | ||
if rows and 'count' in rows[0]: | ||
count = rows[0]['count'] | ||
return count | ||
|
||
@tracked_method(agent_check_getter=agent_check_getter) | ||
def _explain_prepared_statement(self, dbname, statement, obfuscated_statement, query_signature): | ||
|
@@ -157,15 +160,14 @@ def _deallocate_prepared_statement(self, dbname, query_signature): | |
) | ||
|
||
def _execute_query(self, dbname, query): | ||
# Psycopg2 connections do not get closed when context ends; | ||
# leaving context will just mark the connection as inactive in MultiDatabaseConnectionPool | ||
with self._check.db_pool.get_connection(dbname, self._check._config.idle_connection_timeout) as conn: | ||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cursor: | ||
with conn.cursor(row_factory=dict_row) as cursor: | ||
logger.debug('Executing query=[%s]', query) | ||
cursor.execute(query) | ||
|
||
def _execute_query_and_fetch_rows(self, dbname, query): | ||
with self._check.db_pool.get_connection(dbname, self._check._config.idle_connection_timeout) as conn: | ||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cursor: | ||
with conn.cursor(row_factory=dict_row) as cursor: | ||
logger.debug('Executing query=[%s]', query) | ||
cursor.execute(query) | ||
return cursor.fetchall() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've been seeing json serialization errors for a long time now due to some activity payloads containing
IPv4Address
objects