Skip to content

Commit

Permalink
Update Postgres collection error type to match MySQL (#9698)
Browse files Browse the repository at this point in the history
* [Update] Collection error type to array

* [Update] Collection err tests
  • Loading branch information
alexbarksdale committed Jul 13, 2021
1 parent c25dc14 commit 5e40911
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions postgres/datadog_checks/postgres/statement_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,9 @@ def _collect_plan_for_statement(self, row):
plan_dict, explain_err_code, err_msg = self._run_explain_safe(
row['datname'], row['query'], obfuscated_statement
)
collection_error = None
collection_errors = None
if explain_err_code:
collection_error = {'code': explain_err_code.value, 'message': err_msg if err_msg else None}
collection_errors = [{'code': explain_err_code.value, 'message': err_msg if err_msg else None}]

plan, normalized_plan, obfuscated_plan, plan_signature = None, None, None, None
if plan_dict:
Expand Down Expand Up @@ -395,7 +395,7 @@ def _collect_plan_for_statement(self, row):
"plan": {
"definition": obfuscated_plan,
"signature": plan_signature,
"collection_error": collection_error,
"collection_errors": collection_errors,
},
"query_signature": query_signature,
"resource_hash": query_signature,
Expand Down
14 changes: 7 additions & 7 deletions postgres/tests/test_pg_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def test_get_db_explain_setup_state(integration_check, dbm_instance, dbname, exp

@pytest.mark.parametrize("pg_stat_activity_view", ["pg_stat_activity", "datadog.pg_stat_activity()"])
@pytest.mark.parametrize(
"user,password,dbname,query,arg,expected_error_tag,expected_collection_error,expected_statement_truncated",
"user,password,dbname,query,arg,expected_error_tag,expected_collection_errors,expected_statement_truncated",
[
(
"bob",
Expand Down Expand Up @@ -509,7 +509,7 @@ def test_get_db_explain_setup_state(integration_check, dbm_instance, dbname, exp
"SELECT * FROM kennel WHERE id = %s",
123,
"error:explain-{}".format(DBExplainError.invalid_schema),
{'code': 'invalid_schema', 'message': "<class 'psycopg2.errors.InvalidSchemaName'>"},
[{'code': 'invalid_schema', 'message': "<class 'psycopg2.errors.InvalidSchemaName'>"}],
StatementTruncationState.not_truncated.value,
),
(
Expand All @@ -519,7 +519,7 @@ def test_get_db_explain_setup_state(integration_check, dbm_instance, dbname, exp
"SELECT * FROM kennel WHERE id = %s",
123,
"error:explain-{}".format(DBExplainError.failed_function),
{'code': 'failed_function', 'message': "<class 'psycopg2.errors.UndefinedFunction'>"},
[{'code': 'failed_function', 'message': "<class 'psycopg2.errors.UndefinedFunction'>"}],
StatementTruncationState.not_truncated.value,
),
(
Expand All @@ -542,7 +542,7 @@ def test_get_db_explain_setup_state(integration_check, dbm_instance, dbname, exp
# looking at the length in bytes when testing for truncated statements
u'\u20AC\u20AC\u20AC\u20AC\u20AC\u20AC\u20AC\u20AC\u20AC\u20AC',
"error:explain-{}".format(DBExplainError.query_truncated),
{'code': 'query_truncated', 'message': 'track_activity_query_size=1024'},
[{'code': 'query_truncated', 'message': 'track_activity_query_size=1024'}],
StatementTruncationState.truncated.value,
),
],
Expand All @@ -558,7 +558,7 @@ def test_statement_samples_collect(
query,
arg,
expected_error_tag,
expected_collection_error,
expected_collection_errors,
expected_statement_truncated,
):
dbm_instance['pg_stat_activity_view'] = pg_stat_activity_view
Expand Down Expand Up @@ -612,9 +612,9 @@ def test_statement_samples_collect(
# validate the events to ensure we've provided an explanation for not providing an exec plan
for event in matching:
if event['db']['plan']['definition'] is None:
assert event['db']['plan']['collection_error'] == expected_collection_error
assert event['db']['plan']['collection_errors'] == expected_collection_errors
else:
assert event['db']['plan']['collection_error'] is None
assert event['db']['plan']['collection_errors'] is None

finally:
conn.close()
Expand Down

0 comments on commit 5e40911

Please sign in to comment.