From 6f492f9ef0206b7871a46814415a58299e962bfa Mon Sep 17 00:00:00 2001 From: Nenad Noveljic <18366081+nenadnoveljic@users.noreply.github.com> Date: Fri, 1 Nov 2024 14:39:46 +0100 Subject: [PATCH] Fix missing appended SQL comments (#18958) * Fix missing appended SQL comments * ficed changelog * linter * changelog --- sqlserver/changelog.d/18958.fixed | 1 + sqlserver/datadog_checks/sqlserver/activity.py | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 sqlserver/changelog.d/18958.fixed diff --git a/sqlserver/changelog.d/18958.fixed b/sqlserver/changelog.d/18958.fixed new file mode 100644 index 0000000000000..0ba896b9a6e49 --- /dev/null +++ b/sqlserver/changelog.d/18958.fixed @@ -0,0 +1 @@ +Fix missing appended SQL comments. diff --git a/sqlserver/datadog_checks/sqlserver/activity.py b/sqlserver/datadog_checks/sqlserver/activity.py index 39bb0afe9fc4b..63ca36d1cc69e 100644 --- a/sqlserver/datadog_checks/sqlserver/activity.py +++ b/sqlserver/datadog_checks/sqlserver/activity.py @@ -14,7 +14,7 @@ from datadog_checks.base.utils.tracking import tracked_method from datadog_checks.sqlserver.config import SQLServerConfig from datadog_checks.sqlserver.const import STATIC_INFO_ENGINE_EDITION, STATIC_INFO_VERSION -from datadog_checks.sqlserver.utils import extract_sql_comments_and_procedure_name +from datadog_checks.sqlserver.utils import extract_sql_comments, extract_sql_comments_and_procedure_name try: import datadog_agent @@ -24,6 +24,8 @@ DEFAULT_COLLECTION_INTERVAL = 10 MAX_PAYLOAD_BYTES = 19e6 +TAIL_TEXT_SIZE = 200 + CONNECTIONS_QUERY = """\ SELECT login_name AS user_name, @@ -59,6 +61,10 @@ ELSE req.statement_end_offset END - req.statement_start_offset) / 2) + 1) AS statement_text, SUBSTRING(qt.text, 1, {proc_char_limit}) as text, + CASE + WHEN LEN(qt.text) > {proc_char_limit} THEN RIGHT(qt.text, {tail_text_size}) + ELSE '' + END AS tail_text, c.client_tcp_port as client_port, c.client_net_address as client_address, sess.host_name as host_name, @@ -213,6 +219,7 @@ def _get_activity(self, cursor, exec_request_columns): query = ACTIVITY_QUERY.format( exec_request_columns=', '.join(['req.{}'.format(r) for r in exec_request_columns]), proc_char_limit=self._config.stored_procedure_characters_limit, + tail_text_size=TAIL_TEXT_SIZE, ) self.log.debug("Running query [%s]", query) cursor.execute(query) @@ -286,6 +293,10 @@ def _obfuscate_and_sanitize_row(self, row): ) # sqlserver doesn't have a boolean data type so convert integer to boolean comments, row['is_proc'], procedure_name = extract_sql_comments_and_procedure_name(row['text']) + if 'tail_text' in row: + appended_comments, _ = extract_sql_comments(row['tail_text']) + if appended_comments: + comments = list(set(comments + appended_comments)) if row['is_proc'] and 'text' in row: try: procedure_statement = obfuscate_sql_with_metadata(