From 03cc0c2d0e9502044f2452221ca42c6da1231da0 Mon Sep 17 00:00:00 2001 From: Xiao Yu Date: Mon, 24 Jul 2023 16:45:50 +0000 Subject: [PATCH] WordPress AutoInstrument: Skip WP Core With the current WordPress auto-instrumentation implementation most of the spans in APM traces are filled with WP core functions. For the most part this is not super useful for the majority of WordPress developers / users because there's not much they can do to change how core works. It's also relatively unlikely performance issues are caused by WP core functions as they are used by all WP users and have over the decades been optimized and looked at closely. It's much more likely performance issues arise due to the themes or plugins that are installed on a site. So it would be more useful to only expose WordPress theme and plugin callbacks as span by default. These callbacks are how WP developers normally go about modifying and adding functionality. (No "core hacks" has been a core tenet of WP developers for quite a long time now.) This PR makes it so that we default skip instrumentation of WP core callbacks unless a `WP_DEBUG` constant is set which is the WP convention for turning on more verbose logging: https://wordpress.org/documentation/article/debugging-in-wordpress/ --- .../WordPressFilterCallbackWrapper.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/agent/php/ElasticApm/Impl/AutoInstrument/WordPressFilterCallbackWrapper.php b/agent/php/ElasticApm/Impl/AutoInstrument/WordPressFilterCallbackWrapper.php index 247972c2e..70fbd21ca 100644 --- a/agent/php/ElasticApm/Impl/AutoInstrument/WordPressFilterCallbackWrapper.php +++ b/agent/php/ElasticApm/Impl/AutoInstrument/WordPressFilterCallbackWrapper.php @@ -91,6 +91,11 @@ public function getWrappedCallback() */ public function __invoke() { + if ( ! $this->shouldInstrument() ) { + /** @phpstan-assert callable(mixed ...): mixed $this->callback */ + return call_user_func_array( $this->callback, func_get_args() ); + } + /** @phpstan-assert callable(mixed ...): mixed $this->callback */ return AutoInstrumentationUtil::captureCurrentSpan( $this->hookName . ' - ' . ($this->callbackGroupName ?? WordPressAutoInstrumentation::SPAN_NAME_PART_FOR_CORE) /* <- name */, @@ -102,4 +107,18 @@ public function __invoke() 1 /* <- numberOfStackFramesToSkip - 1 because we don't want the current method (i.e., WordPressFilterCallbackWrapper->__invoke) to be kept */ ); } + + private function shouldInstrument() + { + if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { + return true; + } + + if ( null !== $this->callbackGroupName && WordPressAutoInstrumentation::SPAN_NAME_PART_FOR_CORE !== $this->callbackGroupName ) { + return true; + } + + return false; + } + }