From a6adfed701bfff720983b5f29235f7cf0cc20ac2 Mon Sep 17 00:00:00 2001 From: Luis Henrique Mulinari Date: Thu, 5 Oct 2023 14:59:09 -0300 Subject: [PATCH] Add potential multi-dataset logger (#4920) * Add potential multi dataset logger * Sanitize fields * Use esc_url_raw since sanitize_url is deprecated * Update prometheus-collectors/class-potential-multi-dataset-queries-collector.php Co-authored-by: Mohammad Jangda * Check if log2logstash is defined before calling it --------- Co-authored-by: Mohammad Jangda --- ...ential-multi-dataset-queries-collector.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/prometheus-collectors/class-potential-multi-dataset-queries-collector.php b/prometheus-collectors/class-potential-multi-dataset-queries-collector.php index 5ce2bc172d..dee8e78bac 100644 --- a/prometheus-collectors/class-potential-multi-dataset-queries-collector.php +++ b/prometheus-collectors/class-potential-multi-dataset-queries-collector.php @@ -4,6 +4,7 @@ use Prometheus\Counter; use Prometheus\RegistryInterface; +use function Automattic\VIP\Logstash\log2logstash; class Potential_Multi_Dataset_Queries_Collector implements CollectorInterface { private Counter $potential_multi_dataset_queries_collector; @@ -49,6 +50,27 @@ public function sql_query_log( $query ): void { $blog_ids_count >= 3 ? '3+' : (string) $blog_ids_count, ] ); + + if ( ! function_exists( '\Automattic\VIP\Logstash\log2logstash' ) ) { + return; + } + + $backtrace = function_exists( 'wp_debug_backtrace_summary' ) ? wp_debug_backtrace_summary( null, 4, false ) : []; // phpcs:ignore + \Automattic\VIP\Logstash\log2logstash( + [ + 'severity' => 'debug', + 'feature' => 'potential_multi_dataset_queries', + 'message' => 'Potential multi dataset query detected', + 'extra' => [ + 'uri' => isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( $_SERVER['REQUEST_URI'] ) : '', + 'http_method' => isset( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( $_SERVER['REQUEST_METHOD'] ) : '', + 'backtrace' => $backtrace, + 'last_global_table' => $last_global_table, + 'last_blog_table' => $last_blog_table, + 'blog_ids_count' => $blog_ids_count, + ], + ] + ); } }