From e39cec986b6ceba650eff139cf6cfa7df5c6e8bc Mon Sep 17 00:00:00 2001 From: Pablo Marcos Date: Thu, 29 Aug 2024 15:43:04 +0000 Subject: [PATCH] Disable query_metric_log collection setting the interval to 0 --- docs/en/operations/settings/settings.md | 7 +++++-- .../system-tables/query_metric_log.md | 2 +- src/Core/Settings.h | 2 +- src/Core/SettingsChangesHistory.cpp | 2 +- src/Interpreters/executeQuery.cpp | 21 ++++++++++++++----- .../03203_system_query_metric_log.reference | 1 + .../03203_system_query_metric_log.sh | 6 +++++- 7 files changed, 30 insertions(+), 11 deletions(-) diff --git a/docs/en/operations/settings/settings.md b/docs/en/operations/settings/settings.md index 8ef5dc73821..5d7debdcce5 100644 --- a/docs/en/operations/settings/settings.md +++ b/docs/en/operations/settings/settings.md @@ -1834,9 +1834,12 @@ Default value: 0 (no restriction). ## query_metric_log_interval (#query_metric_log_interval) The interval in milliseconds at which the [query_metric_log](../../operations/system-tables/query_metric_log.md) for individual queries is collected. -If set to 0, it will take the `collect_interval_milliseconds` from the [query_metric_log setting](../../operations/server-configuration-parameters/settings.md#query_metric_log). -Default value: 0 +If set to any negative value, it will take the value `collect_interval_milliseconds` from the [query_metric_log setting](../../operations/server-configuration-parameters/settings.md#query_metric_log) or default to 1000 if not present. + +To disable the collection of a single query, set `query_metric_log_interval` to 0. + +Default value: -1 ## insert_quorum {#insert_quorum} diff --git a/docs/en/operations/system-tables/query_metric_log.md b/docs/en/operations/system-tables/query_metric_log.md index 82c76e1e66a..2e6d393c7df 100644 --- a/docs/en/operations/system-tables/query_metric_log.md +++ b/docs/en/operations/system-tables/query_metric_log.md @@ -6,7 +6,7 @@ slug: /en/operations/system-tables/query_metric_log Contains history of memory and metric values from table `system.events` for individual queries, periodically flushed to disk. Once a query starts, data is collected at periodic intervals of `query_metric_log_interval` milliseconds (which is set to 1000 -by default) and when the query finishes. +by default). Columns: - `query_id` ([String](../../sql-reference/data-types/string.md)) — ID of the query. diff --git a/src/Core/Settings.h b/src/Core/Settings.h index 6271a4d25f1..e79a1cb92c7 100644 --- a/src/Core/Settings.h +++ b/src/Core/Settings.h @@ -514,7 +514,7 @@ class IColumn; M(Bool, log_query_threads, false, "Log query threads into system.query_thread_log table. This setting have effect only when 'log_queries' is true.", 0) \ M(Bool, log_query_views, true, "Log query dependent views into system.query_views_log table. This setting have effect only when 'log_queries' is true.", 0) \ M(String, log_comment, "", "Log comment into system.query_log table and server log. It can be set to arbitrary string no longer than max_query_size.", 0) \ - M(UInt64, query_metric_log_interval, 0, "Periodic interval in milliseconds to collect query metric logs.", 0) \ + M(Int64, query_metric_log_interval, -1, "Periodic interval in milliseconds to collect query metric logs.", 0) \ M(LogsLevel, send_logs_level, LogsLevel::fatal, "Send server text logs with specified minimum level to client. Valid values: 'trace', 'debug', 'information', 'warning', 'error', 'fatal', 'none'", 0) \ M(String, send_logs_source_regexp, "", "Send server text logs with specified regexp to match log source name. Empty means all sources.", 0) \ M(Bool, enable_optimize_predicate_expression, true, "If it is set to true, optimize predicates to subqueries.", 0) \ diff --git a/src/Core/SettingsChangesHistory.cpp b/src/Core/SettingsChangesHistory.cpp index aea1960bfcf..17858a4c019 100644 --- a/src/Core/SettingsChangesHistory.cpp +++ b/src/Core/SettingsChangesHistory.cpp @@ -75,6 +75,7 @@ static std::initializer_listgetSettingsRef().query_metric_log_interval; + if (interval_milliseconds < 0) + interval_milliseconds = context->getConfigRef().getUInt64("query_metric_log.collect_interval_milliseconds", 1000); + + return interval_milliseconds; +} QueryLogElement logQueryStart( const std::chrono::time_point & query_start_time, @@ -376,10 +384,9 @@ QueryLogElement logQueryStart( if (auto query_metric_log = context->getQueryMetricLog(); query_metric_log && !internal) { - auto interval_milliseconds = context->getSettingsRef().query_metric_log_interval; - if (interval_milliseconds == 0) - interval_milliseconds = context->getConfigRef().getUInt64("query_metric_log.collect_interval_milliseconds", 1000); - query_metric_log->startQuery(elem.client_info.current_query_id, query_start_time, interval_milliseconds); + auto interval_milliseconds = getQueryMetricLogInterval(context); + if (interval_milliseconds > 0) + query_metric_log->startQuery(elem.client_info.current_query_id, query_start_time, interval_milliseconds); } return elem; @@ -515,7 +522,11 @@ void logQueryFinish( } if (auto query_metric_log = context->getQueryMetricLog(); query_metric_log && !internal) - query_metric_log->finishQuery(elem.client_info.current_query_id); + { + auto interval_milliseconds = getQueryMetricLogInterval(context); + if (interval_milliseconds > 0) + query_metric_log->finishQuery(elem.client_info.current_query_id); + } } void logQueryException( diff --git a/tests/queries/0_stateless/03203_system_query_metric_log.reference b/tests/queries/0_stateless/03203_system_query_metric_log.reference index e1cad05c7b1..dfa1b3d362b 100644 --- a/tests/queries/0_stateless/03203_system_query_metric_log.reference +++ b/tests/queries/0_stateless/03203_system_query_metric_log.reference @@ -1,3 +1,4 @@ 1 1 1 1 1 1 1 1 1 +0 diff --git a/tests/queries/0_stateless/03203_system_query_metric_log.sh b/tests/queries/0_stateless/03203_system_query_metric_log.sh index 86755f7122a..5bfa49bf682 100755 --- a/tests/queries/0_stateless/03203_system_query_metric_log.sh +++ b/tests/queries/0_stateless/03203_system_query_metric_log.sh @@ -10,6 +10,7 @@ readonly query_prefix=$CLICKHOUSE_DATABASE $CLICKHOUSE_CLIENT --query-id="${query_prefix}_1000" -q "SELECT sleep(3) + sleep(2) FORMAT Null" & $CLICKHOUSE_CLIENT --query-id="${query_prefix}_1234" -q "SELECT sleep(3) + sleep(2) SETTINGS query_metric_log_interval=1234 FORMAT Null" & $CLICKHOUSE_CLIENT --query-id="${query_prefix}_123" -q "SELECT sleep(3) + sleep(2) SETTINGS query_metric_log_interval=123 FORMAT Null" & +$CLICKHOUSE_CLIENT --query-id="${query_prefix}_0" -q "SELECT sleep(3) + sleep(2) SETTINGS query_metric_log_interval=0 FORMAT Null" & wait @@ -29,10 +30,13 @@ function check_log() ORDER BY event_time_microseconds OFFSET 1 ) - SELECT count() BETWEEN least(5000 / $interval - 2, 5000 / $interval * 0.9) AND (5000 / $interval - 1) * 1.1, avg(diff) BETWEEN $interval * 0.9 AND $interval * 1.1, stddevPopStable(diff) BETWEEN 0 AND $interval * 0.5 FROM diff + SELECT count() BETWEEN least(5000 / $interval - 2, 5000 / $interval * 0.9) AND (5000 / $interval - 1) * 1.1, avg(diff) BETWEEN $interval * 0.9 AND $interval * 1.1, stddevPopStable(diff) BETWEEN 0 AND $interval * 0.2 FROM diff """ } check_log 1000 check_log 1234 check_log 123 + +# query_metric_log_interval=0 disables the collection altogether +$CLICKHOUSE_CLIENT -m -q """SELECT count() FROM system.query_metric_log WHERE query_id = '${query_prefix}_0'""" \ No newline at end of file