Skip to content

Commit

Permalink
[postgres] debug log when query is truncated (#19272)
Browse files Browse the repository at this point in the history
* debug log when query is truncated

* fix lint

* log warnings when track_activity_query_size < 4096

* fix logging
  • Loading branch information
lu-zhengda authored Dec 18, 2024
1 parent 49d8041 commit a68e989
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions postgres/datadog_checks/postgres/statement_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

TRACK_ACTIVITY_QUERY_SIZE_UNKNOWN_VALUE = -1

TRACK_ACTIVITY_QUERY_SIZE_SUGGESTED_VALUE = 4096

SUPPORTED_EXPLAIN_STATEMENTS = frozenset({'select', 'table', 'delete', 'insert', 'replace', 'update', 'with'})

# columns from pg_stat_activity which correspond to attributes common to all databases and are therefore stored in
Expand Down Expand Up @@ -548,17 +550,16 @@ def _collect_statement_samples(self):
"gauge",
)

@staticmethod
def _to_active_session(row, track_activity_query_size):
def _to_active_session(self, row, track_activity_query_size):
if (row.get('backend_type', 'client backend') != 'client backend') or (
row['state'] is not None and row['state'] != 'idle'
):
active_row = {key: val for key, val in row.items() if val is not None and key != 'query'}
# Create an active_row, for each session by
# 1. Removing all null key/value pairs and the original query
# 2. if row['statement'] is none, replace with ERROR: failed to obfuscate so we can still collect activity
active_row['query_truncated'] = PostgresStatementSamples._get_truncation_state(
track_activity_query_size, row['query']
active_row['query_truncated'] = self._get_truncation_state(
track_activity_query_size, row['query'], row['query_signature']
).value
if row['statement'] is None:
active_row['statement'] = "ERROR: failed to obfuscate"
Expand Down Expand Up @@ -690,7 +691,10 @@ def _run_explain_safe(self, dbname, statement, obfuscated_statement, query_signa

track_activity_query_size = self._get_track_activity_query_size()

if self._get_truncation_state(track_activity_query_size, statement) == StatementTruncationState.truncated:
if (
self._get_truncation_state(track_activity_query_size, statement, query_signature)
== StatementTruncationState.truncated
):
return (
None,
DBExplainError.query_truncated,
Expand Down Expand Up @@ -826,7 +830,7 @@ def _collect_plan_for_statement(self, row):
"comments": row['dd_comments'],
},
"query_truncated": self._get_truncation_state(
self._get_track_activity_query_size(), row['query']
self._get_track_activity_query_size(), row['query'], row['query_signature']
).value,
},
'postgres': {k: v for k, v in row.items() if k not in pg_stat_activity_sample_exclude_keys},
Expand Down Expand Up @@ -915,8 +919,7 @@ def _report_activity_event(self):
def _get_track_activity_query_size(self):
return int(self._check.pg_settings.get("track_activity_query_size", TRACK_ACTIVITY_QUERY_SIZE_UNKNOWN_VALUE))

@staticmethod
def _get_truncation_state(track_activity_query_size, statement):
def _get_truncation_state(self, track_activity_query_size, statement, query_signature):
# Only check is a statement is truncated if the value of track_activity_query_size was loaded correctly
# to avoid confusingly reporting a wrong indicator by using a default that might be wrong for the database
if track_activity_query_size == TRACK_ACTIVITY_QUERY_SIZE_UNKNOWN_VALUE:
Expand All @@ -930,4 +933,23 @@ def _get_truncation_state(track_activity_query_size, statement):
# would falsely report it as a truncated statement
statement_bytes = bytes(statement, "utf-8")
truncated = len(statement_bytes) >= track_activity_query_size - (MAX_CHARACTER_SIZE_IN_BYTES + 1)
return StatementTruncationState.truncated if truncated else StatementTruncationState.not_truncated
if truncated:
if track_activity_query_size < TRACK_ACTIVITY_QUERY_SIZE_SUGGESTED_VALUE:
self._log.warning(
"Statement with query_signature=%s was truncated. Query size: %d, track_activity_query_size: %d "
"See https://docs.datadoghq.com/database_monitoring/setup_postgres/troubleshooting%s "
"for more details on how to increase the track_activity_query_size setting.",
query_signature,
len(statement_bytes),
track_activity_query_size,
"#query-samples-are-truncated",
)
else:
self._log.debug(
"Statement with query_signature=%s was truncated. Query size: %d, track_activity_query_size: %d",
query_signature,
len(statement_bytes),
track_activity_query_size,
)
return StatementTruncationState.truncated
return StatementTruncationState.not_truncated

0 comments on commit a68e989

Please sign in to comment.