From dffd15f6433c5d8539a0b4b82655ae8281171b90 Mon Sep 17 00:00:00 2001 From: Nikita Orlov Date: Thu, 21 May 2020 21:01:25 +0300 Subject: [PATCH] issue-7224 Add failed Insert and Select query counters to ProfileEvents --- src/Common/ProfileEvents.cpp | 3 +++ src/Interpreters/executeQuery.cpp | 34 +++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/Common/ProfileEvents.cpp b/src/Common/ProfileEvents.cpp index aa7affda243..a75339a644d 100644 --- a/src/Common/ProfileEvents.cpp +++ b/src/Common/ProfileEvents.cpp @@ -8,6 +8,9 @@ M(Query, "Number of queries to be interpreted and potentially executed. Does not include queries that failed to parse or were rejected due to AST size limits, quota limits or limits on the number of simultaneously running queries. May include internal queries initiated by ClickHouse itself. Does not count subqueries.") \ M(SelectQuery, "Same as Query, but only for SELECT queries.") \ M(InsertQuery, "Same as Query, but only for INSERT queries.") \ + M(FailedQuery, "Number of failed queries.") \ + M(FailedSelectQuery, "Same as FailedQuery, but only for SELECT queries.") \ + M(FailedInsertQuery, "Same as FailedQuery, but only for INSERT queries.") \ M(FileOpen, "Number of files opened.") \ M(Seek, "Number of times the 'lseek' function was called.") \ M(ReadBufferFromFileDescriptorRead, "Number of reads (read/pread) from a file descriptor. Does not include sockets.") \ diff --git a/src/Interpreters/executeQuery.cpp b/src/Interpreters/executeQuery.cpp index 4d609395c3a..62543249d58 100644 --- a/src/Interpreters/executeQuery.cpp +++ b/src/Interpreters/executeQuery.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -45,6 +46,9 @@ namespace ProfileEvents { extern const Event QueryMaskingRulesMatch; + extern const Event FailedQuery; + extern const Event FailedInsertQuery; + extern const Event FailedSelectQuery; } namespace DB @@ -146,7 +150,7 @@ static void logException(Context & context, QueryLogElement & elem) } -static void onExceptionBeforeStart(const String & query_for_logging, Context & context, time_t current_time) +static void onExceptionBeforeStart(const String & query_for_logging, Context & context, time_t current_time, ASTPtr ast) { /// Exception before the query execution. if (auto quota = context.getQuota()) @@ -178,6 +182,20 @@ static void onExceptionBeforeStart(const String & query_for_logging, Context & c if (settings.log_queries && elem.type >= settings.log_queries_min_type) if (auto query_log = context.getQueryLog()) query_log->add(elem); + + ProfileEvents::increment(ProfileEvents::FailedQuery); + + if (ast) + { + if (ast->as() || ast->as()) + { + ProfileEvents::increment(ProfileEvents::FailedSelectQuery); + } + else if (ast->as()) + { + ProfileEvents::increment(ProfileEvents::FailedInsertQuery); + } + } } static void setQuerySpecificSettings(ASTPtr & ast, Context & context) @@ -249,7 +267,7 @@ static std::tuple executeQueryImpl( logQuery(query_for_logging, context, internal); if (!internal) - onExceptionBeforeStart(query_for_logging, context, current_time); + onExceptionBeforeStart(query_for_logging, context, current_time, ast); throw; } @@ -501,7 +519,7 @@ static std::tuple executeQueryImpl( } }; - auto exception_callback = [elem, &context, log_queries, log_queries_min_type = settings.log_queries_min_type, quota(quota)] () mutable + auto exception_callback = [elem, &context, ast, log_queries, log_queries_min_type = settings.log_queries_min_type, quota(quota)] () mutable { if (quota) quota->used(Quota::ERRORS, 1, /* check_exceeded = */ false); @@ -544,6 +562,14 @@ static std::tuple executeQueryImpl( if (auto query_log = context.getQueryLog()) query_log->add(elem); } + + ProfileEvents::increment(ProfileEvents::FailedQuery); + if (ast->as()) { + ProfileEvents::increment(ProfileEvents::FailedInsertQuery); + } else if (ast->as() || ast->as()) { + ProfileEvents::increment(ProfileEvents::FailedSelectQuery); + } + }; res.finish_callback = std::move(finish_callback); @@ -565,7 +591,7 @@ static std::tuple executeQueryImpl( if (query_for_logging.empty()) query_for_logging = prepareQueryForLogging(query, context); - onExceptionBeforeStart(query_for_logging, context, current_time); + onExceptionBeforeStart(query_for_logging, context, current_time, ast); } throw;