This is an automated email from the ASF dual-hosted git repository.

djwang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit 00ed85c83821a035eaa2b8ff2fec76fedfed65b2
Author: Maxim Smyatkin <[email protected]>
AuthorDate: Tue May 28 15:25:35 2024 +0300

    [yagp_hooks_collector] Add configurable text field trimming
    
    Trim query text and plan text to max_text_size and
    max_plan_size limits.
---
 src/Config.cpp      | 11 ++++++++++-
 src/Config.h        |  1 +
 src/EventSender.cpp | 12 +++++++++---
 3 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/Config.cpp b/src/Config.cpp
index 1bbad9a6ea3..c07a6948694 100644
--- a/src/Config.cpp
+++ b/src/Config.cpp
@@ -1,7 +1,8 @@
 #include "Config.h"
-#include <unordered_set>
+#include <limits.h>
 #include <memory>
 #include <string>
+#include <unordered_set>
 
 extern "C" {
 #include "postgres.h"
@@ -15,6 +16,7 @@ static bool guc_enable_cdbstats = true;
 static bool guc_enable_collector = true;
 static bool guc_report_nested_queries = true;
 static char *guc_ignored_users = nullptr;
+static int guc_max_text_size = 1024; // in KB
 static std::unique_ptr<std::unordered_set<std::string>> ignored_users = 
nullptr;
 
 void Config::init() {
@@ -47,6 +49,12 @@ void Config::init() {
       "Make yagpcc ignore queries issued by given users", 0LL,
       &guc_ignored_users, "gpadmin,repl,gpperfmon,monitor", PGC_SUSET,
       GUC_NOT_IN_SAMPLE | GUC_GPDB_NEED_SYNC, 0LL, 0LL, 0LL);
+
+  DefineCustomIntVariable(
+      "yagpcc.max_text_size",
+      "Make yagpcc trim plan and query texts longer than configured size", 
NULL,
+      &guc_max_text_size, 1024, 0, INT_MAX / 1024, PGC_SUSET,
+      GUC_NOT_IN_SAMPLE | GUC_GPDB_NEED_SYNC | GUC_UNIT_KB, NULL, NULL, NULL);
 }
 
 std::string Config::uds_path() { return guc_uds_path; }
@@ -54,6 +62,7 @@ bool Config::enable_analyze() { return guc_enable_analyze; }
 bool Config::enable_cdbstats() { return guc_enable_cdbstats; }
 bool Config::enable_collector() { return guc_enable_collector; }
 bool Config::report_nested_queries() { return guc_report_nested_queries; }
+size_t Config::max_text_size() { return guc_max_text_size * 1024; }
 
 bool Config::filter_user(const std::string *username) {
   if (!ignored_users) {
diff --git a/src/Config.h b/src/Config.h
index 15f425be67c..f806bc0dbf5 100644
--- a/src/Config.h
+++ b/src/Config.h
@@ -11,4 +11,5 @@ public:
   static bool enable_collector();
   static bool filter_user(const std::string *username);
   static bool report_nested_queries();
+  static size_t max_text_size();
 };
\ No newline at end of file
diff --git a/src/EventSender.cpp b/src/EventSender.cpp
index 116805d0646..4de5564533b 100644
--- a/src/EventSender.cpp
+++ b/src/EventSender.cpp
@@ -93,11 +93,15 @@ ExplainState get_explain_state(QueryDesc *query_desc, bool 
costs) {
   return es;
 }
 
+inline std::string char_to_trimmed_str(const char *str, size_t len) {
+  return std::string(str, std::min(len, Config::max_text_size()));
+}
+
 void set_plan_text(std::string *plan_text, QueryDesc *query_desc) {
   MemoryContext oldcxt =
       MemoryContextSwitchTo(query_desc->estate->es_query_cxt);
   auto es = get_explain_state(query_desc, true);
-  *plan_text = std::string(es.str->data, es.str->len);
+  *plan_text = char_to_trimmed_str(es.str->data, es.str->len);
   pfree(es.str->data);
   MemoryContextSwitchTo(oldcxt);
 }
@@ -119,9 +123,11 @@ void set_query_plan(yagpcc::SetQueryReq *req, QueryDesc 
*query_desc) {
 void set_query_text(yagpcc::SetQueryReq *req, QueryDesc *query_desc) {
   if (Gp_session_role == GP_ROLE_DISPATCH && query_desc->sourceText) {
     auto qi = req->mutable_query_info();
-    *qi->mutable_query_text() = query_desc->sourceText;
+    *qi->mutable_query_text() = char_to_trimmed_str(
+        query_desc->sourceText, strlen(query_desc->sourceText));
     char *norm_query = gen_normquery(query_desc->sourceText);
-    *qi->mutable_template_query_text() = std::string(norm_query);
+    *qi->mutable_template_query_text() =
+        char_to_trimmed_str(norm_query, strlen(norm_query));
   }
 }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to