From 51b83bb5a6eb5d6300a7bfe2ed008f9acd9b164d Mon Sep 17 00:00:00 2001 From: Luis Henrique Mulinari Date: Wed, 4 Oct 2023 17:21:53 -0300 Subject: [PATCH 1/5] Add potential multi dataset logger --- ...potential-multi-dataset-queries-collector.php | 16 ++++++++++++++++ 1 file changed, 16 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..86f00ca5e0 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,21 @@ public function sql_query_log( $query ): void { $blog_ids_count >= 3 ? '3+' : (string) $blog_ids_count, ] ); + + $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', + 'blog_id' => get_current_blog_id(), + 'extra' => [ + 'uri' => $_SERVER['REQUEST_URI'] ?? '', + 'method' => $_SERVER['REQUEST_METHOD'] ?? '', + 'backtrace' => $backtrace, + ], + ] + ); } } From a1c4e4fc6619c5333e40a6bf1d2aa6700ab62fb3 Mon Sep 17 00:00:00 2001 From: Luis Henrique Mulinari Date: Wed, 4 Oct 2023 17:53:25 -0300 Subject: [PATCH 2/5] Sanitize fields --- .../class-potential-multi-dataset-queries-collector.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prometheus-collectors/class-potential-multi-dataset-queries-collector.php b/prometheus-collectors/class-potential-multi-dataset-queries-collector.php index 86f00ca5e0..3055b99ff2 100644 --- a/prometheus-collectors/class-potential-multi-dataset-queries-collector.php +++ b/prometheus-collectors/class-potential-multi-dataset-queries-collector.php @@ -59,8 +59,8 @@ public function sql_query_log( $query ): void { 'message' => 'Potential multi dataset query detected', 'blog_id' => get_current_blog_id(), 'extra' => [ - 'uri' => $_SERVER['REQUEST_URI'] ?? '', - 'method' => $_SERVER['REQUEST_METHOD'] ?? '', + 'uri' => isset( $_SERVER['REQUEST_URI'] ) ? sanitize_url( $_SERVER['REQUEST_URI'] ) : '', + 'method' => isset( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( $_SERVER['REQUEST_METHOD'] ) : '', 'backtrace' => $backtrace, ], ] From 74c7171f1bf684c5e7cc9af9310f8e720ec579f2 Mon Sep 17 00:00:00 2001 From: Luis Henrique Mulinari Date: Thu, 5 Oct 2023 10:27:14 -0300 Subject: [PATCH 3/5] Use esc_url_raw since sanitize_url is deprecated --- .../class-potential-multi-dataset-queries-collector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prometheus-collectors/class-potential-multi-dataset-queries-collector.php b/prometheus-collectors/class-potential-multi-dataset-queries-collector.php index 3055b99ff2..aeaed1b378 100644 --- a/prometheus-collectors/class-potential-multi-dataset-queries-collector.php +++ b/prometheus-collectors/class-potential-multi-dataset-queries-collector.php @@ -59,7 +59,7 @@ public function sql_query_log( $query ): void { 'message' => 'Potential multi dataset query detected', 'blog_id' => get_current_blog_id(), 'extra' => [ - 'uri' => isset( $_SERVER['REQUEST_URI'] ) ? sanitize_url( $_SERVER['REQUEST_URI'] ) : '', + 'uri' => isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( $_SERVER['REQUEST_URI'] ) : '', 'method' => isset( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( $_SERVER['REQUEST_METHOD'] ) : '', 'backtrace' => $backtrace, ], From b11533266464f17ccec7bfd2ccb64cab24bf9611 Mon Sep 17 00:00:00 2001 From: Luis Henrique Mulinari Date: Thu, 5 Oct 2023 13:57:38 +0000 Subject: [PATCH 4/5] Update prometheus-collectors/class-potential-multi-dataset-queries-collector.php Co-authored-by: Mohammad Jangda --- .../class-potential-multi-dataset-queries-collector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prometheus-collectors/class-potential-multi-dataset-queries-collector.php b/prometheus-collectors/class-potential-multi-dataset-queries-collector.php index aeaed1b378..b58566d9da 100644 --- a/prometheus-collectors/class-potential-multi-dataset-queries-collector.php +++ b/prometheus-collectors/class-potential-multi-dataset-queries-collector.php @@ -60,7 +60,7 @@ public function sql_query_log( $query ): void { 'blog_id' => get_current_blog_id(), 'extra' => [ 'uri' => isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( $_SERVER['REQUEST_URI'] ) : '', - 'method' => isset( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( $_SERVER['REQUEST_METHOD'] ) : '', + 'http_method' => isset( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( $_SERVER['REQUEST_METHOD'] ) : '', 'backtrace' => $backtrace, ], ] From f75d518c54f32f1b0207276e5214ca25498e6f0c Mon Sep 17 00:00:00 2001 From: Luis Henrique Mulinari Date: Thu, 5 Oct 2023 11:11:30 -0300 Subject: [PATCH 5/5] Check if log2logstash is defined before calling it --- ...s-potential-multi-dataset-queries-collector.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/prometheus-collectors/class-potential-multi-dataset-queries-collector.php b/prometheus-collectors/class-potential-multi-dataset-queries-collector.php index b58566d9da..dee8e78bac 100644 --- a/prometheus-collectors/class-potential-multi-dataset-queries-collector.php +++ b/prometheus-collectors/class-potential-multi-dataset-queries-collector.php @@ -51,17 +51,23 @@ public function sql_query_log( $query ): void { ] ); + 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', - 'blog_id' => get_current_blog_id(), '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, + '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, ], ] );