Skip to content

Commit

Permalink
docker: Introduce helper for better labeling in Query Monitor (#39365)
Browse files Browse the repository at this point in the history
* Introduce helper for better labeling in Query Monitor
Due to the paths used it is not trivial for Query Monitor to infer the type and name of the files being monitored.

* Apply indenting / spacing / phpcs fixes to avoid linting errors

---------

Co-authored-by: Jeremy Herve <[email protected]>
  • Loading branch information
oskosk and jeherve authored Sep 12, 2024
1 parent 49715f5 commit 2461714
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions tools/docker/mu-plugins/query-monitor-helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Plugin Name: Monorepo Query Monitor Helper
* Description: Helps with debugging by providing better labels for Query Monitor.
* Version: 1.0
* Author: Automattic
* Author URI: https://automattic.com/
* Text Domain: jetpack
*
* @package automattic/jetpack
*
*
* This file contains hooks for Query Monitor, which is a debugging tool for WordPress.
* It allows you to see all the queries that are being run on your development environment.
* It defines three filter hooks that modify how Query Monitor categorizes and displays components in its output:
*
* * qm/component_type/unknown:
* This filter categorizes files as either 'plugin' or 'other' based on their file path.
* files containing 'projects/plugins/jetpack' are marked as 'plugin', while those with 'projects/packages' are marked as 'other'.
* * qm/component_name/plugin:
* This filter renames components identified as plugins.
* If the file path contains 'projects/plugins/jetpack', it's labeled as 'Plugin: jetpack'.
* * qm/component_name/other:
* This filter renames components identified as 'other'.
* For files in the 'projects/packages/' directory, it extracts the package name from the file path
* and labels it as 'Package: connection'.
*
* @package automattic/jetpack
*/


add_filter(
'qm/component_type/unknown',
function ( $type, $file, $name, $context ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( strpos( $file, 'projects/plugins' ) !== false ) {
return 'plugin';
}
if ( strpos( $file, 'projects/packages' ) !== false ) {
return 'other';
}
return $type;
},
10,
4
);

add_filter(
'qm/component_name/plugin',
function ( $name, $file ) {
if ( strpos( $file, 'projects/plugins/' ) !== false ) {
$parts = explode( '/', $file );
$plugin_index = array_search( 'plugins', $parts, true );
if ( $plugin_index !== false && isset( $parts[ $plugin_index + 1 ] ) ) {
return 'Plugin: ' . $parts[ $plugin_index + 1 ];
}
}
return $name;
},
10,
2
);

add_filter(
'qm/component_name/other',
function ( $name, $file ) {
if ( strpos( $file, 'projects/packages/' ) !== false ) {
$parts = explode( '/', $file );
$package_name = $parts[ array_search( 'packages', $parts, true ) + 1 ] ?? '';
return 'Jetpack Package: ' . $package_name;
}
return $name;
},
10,
2
);

0 comments on commit 2461714

Please sign in to comment.