From 1fa81a2f5181cccc3cdfae92f1f8a4b817a7e075 Mon Sep 17 00:00:00 2001 From: Sergey Kleyman Date: Mon, 13 Oct 2025 11:28:48 +0300 Subject: [PATCH 1/6] Log interceptRegistrationId on elasticApmInterceptCallsToInternalMethod exit interceptRegistrationId is already logged on elasticApmInterceptCallsToInternalFunctionEx exit --- agent/native/ext/elastic_apm_API.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agent/native/ext/elastic_apm_API.cpp b/agent/native/ext/elastic_apm_API.cpp index 78eabf42a..6debe3aa3 100644 --- a/agent/native/ext/elastic_apm_API.cpp +++ b/agent/native/ext/elastic_apm_API.cpp @@ -198,7 +198,7 @@ ResultCode elasticApmInterceptCallsToInternalMethod( String className, String me finally: - ELASTIC_APM_LOG_DEBUG_RESULT_CODE_FUNCTION_EXIT(); + ELASTIC_APM_LOG_DEBUG_RESULT_CODE_FUNCTION_EXIT_MSG( "interceptRegistrationId: %u", *interceptRegistrationId ); return resultCode; failure: From 70b8418e89377d3a9b755183bce6521853b2f858 Mon Sep 17 00:00:00 2001 From: Sergey Kleyman Date: Mon, 13 Oct 2025 12:01:06 +0300 Subject: [PATCH 2/6] Test calling DB commit/rollback in shutdown function --- .../MySQLiAutoInstrumentationTest.php | 14 +++--- .../PDOAutoInstrumentationTest.php | 18 +++---- .../DbAutoInstrumentationUtilForTests.php | 47 +++++++++++++++---- 3 files changed, 54 insertions(+), 25 deletions(-) diff --git a/tests/ElasticApmTests/ComponentTests/MySQLiAutoInstrumentationTest.php b/tests/ElasticApmTests/ComponentTests/MySQLiAutoInstrumentationTest.php index 258ef816f..826ea6a6b 100644 --- a/tests/ElasticApmTests/ComponentTests/MySQLiAutoInstrumentationTest.php +++ b/tests/ElasticApmTests/ComponentTests/MySQLiAutoInstrumentationTest.php @@ -323,7 +323,8 @@ public static function extractSharedArgs( ?string &$workDbName /* <- out */, ?string &$queryKind /* <- out */, ?bool &$wrapInTx /* <- out */, - ?bool &$rollback /* <- out */ + ?bool &$rollback /* <- out */, + ?bool &$callEndTxInShutdownFunction /* <- out */ ): void { $isOOPApi = $args->getBool(self::IS_OOP_API_KEY); $connectDbName = $args->getNullableString(self::CONNECT_DB_NAME_KEY); @@ -331,6 +332,7 @@ public static function extractSharedArgs( $queryKind = $args->getString(self::QUERY_KIND_KEY); $wrapInTx = $args->getBool(DbAutoInstrumentationUtilForTests::WRAP_IN_TX_KEY); $rollback = $args->getBool(DbAutoInstrumentationUtilForTests::ROLLBACK_KEY); + $callEndTxInShutdownFunction = $args->getBool(DbAutoInstrumentationUtilForTests::CALL_END_TX_IN_SHUTDOWN_FUNCTION_KEY); } public static function appCodeForTestAutoInstrumentation(MixedMap $appCodeArgs): void @@ -342,7 +344,8 @@ public static function appCodeForTestAutoInstrumentation(MixedMap $appCodeArgs): /* out */ $workDbName, /* out */ $queryKind, /* out */ $wrapInTx, - /* out */ $rollback + /* out */ $rollback, + /* out */ $callEndTxInShutdownFunction ); $host = $appCodeArgs->getString(DbAutoInstrumentationUtilForTests::HOST_KEY); $port = $appCodeArgs->getInt(DbAutoInstrumentationUtilForTests::PORT_KEY); @@ -397,9 +400,7 @@ public static function appCodeForTestAutoInstrumentation(MixedMap $appCodeArgs): } $queryResult->close(); - if ($wrapInTx) { - self::assertTrue($rollback ? $mySQLi->rollback() : $mySQLi->commit()); - } + DbAutoInstrumentationUtilForTests::endTx($mySQLi, $wrapInTx, $rollback, $callEndTxInShutdownFunction); self::resetDbState($mySQLi, $queryKind); self::assertTrue($mySQLi->close()); @@ -436,7 +437,8 @@ private function implTestAutoInstrumentation(MixedMap $testArgs): void /* out */ $workDbName, /* out */ $queryKind, /* out */ $wrapInTx, - /* out */ $rollback + /* out */ $rollback, + /* out */ $callEndTxInShutdownFunction ); $testCaseHandle = $this->getTestCaseHandle(); diff --git a/tests/ElasticApmTests/ComponentTests/PDOAutoInstrumentationTest.php b/tests/ElasticApmTests/ComponentTests/PDOAutoInstrumentationTest.php index cc692508d..e1eeff0c2 100644 --- a/tests/ElasticApmTests/ComponentTests/PDOAutoInstrumentationTest.php +++ b/tests/ElasticApmTests/ComponentTests/PDOAutoInstrumentationTest.php @@ -181,21 +181,24 @@ public function dataProviderForTestAutoInstrumentation(): iterable * @param ?string &$dbName * @param ?bool &$wrapInTx * @param ?bool &$rollback + * @param ?bool &$callEndTxInShutdownFunction * * @param-out string $dbName * @param-out bool $wrapInTx * @param-out bool $rollback + * @param-out bool $callEndTxInShutdownFunction */ - public static function extractSharedArgs(MixedMap $args, /* out */ ?string &$dbName, /* out */ ?bool &$wrapInTx, /* out */ ?bool &$rollback): void + public static function extractSharedArgs(MixedMap $args, /* out */ ?string &$dbName, /* out */ ?bool &$wrapInTx, /* out */ ?bool &$rollback, /* out */ ?bool &$callEndTxInShutdownFunction): void { $dbName = $args->getString(DbAutoInstrumentationUtilForTests::DB_NAME_KEY); $wrapInTx = $args->getBool(DbAutoInstrumentationUtilForTests::WRAP_IN_TX_KEY); $rollback = $args->getBool(DbAutoInstrumentationUtilForTests::ROLLBACK_KEY); + $rollback = $args->getBool(DbAutoInstrumentationUtilForTests::CALL_END_TX_IN_SHUTDOWN_FUNCTION_KEY); } public static function appCodeForTestAutoInstrumentation(MixedMap $appCodeArgs): void { - self::extractSharedArgs($appCodeArgs, /* out */ $dbName, /* out */ $wrapInTx, /* out */ $rollback); + self::extractSharedArgs($appCodeArgs, /* out */ $dbName, /* out */ $wrapInTx, /* out */ $rollback, /* out */ $callEndTxInShutdownFunction); $pdo = new PDO(self::buildConnectionString($dbName)); self::assertTrue($pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)); @@ -225,9 +228,7 @@ public static function appCodeForTestAutoInstrumentation(MixedMap $appCodeArgs): self::assertEqualsEx(self::MESSAGES[$msgText], $row['time'], $dbgCtx); } - if ($wrapInTx) { - self::assertTrue($rollback ? $pdo->rollback() : $pdo->commit()); - } + DbAutoInstrumentationUtilForTests::endTx($pdo, $wrapInTx, $rollback, $callEndTxInShutdownFunction); } /** @@ -248,12 +249,7 @@ private function implTestAutoInstrumentation(MixedMap $testArgs): void $disableInstrumentationsOptVal = $testArgs->getString(AutoInstrumentationUtilForTests::DISABLE_INSTRUMENTATIONS_KEY); $isInstrumentationEnabled = $testArgs->getBool(AutoInstrumentationUtilForTests::IS_INSTRUMENTATION_ENABLED_KEY); - self::extractSharedArgs( - $testArgs, - /* out */ $dbNameArg, - /* out */ $wrapInTx, - /* out */ $rollback - ); + self::extractSharedArgs($testArgs, /* out */ $dbNameArg, /* out */ $wrapInTx, /* out */ $rollback, /* out */ $callEndTxInShutdownFunction); $testCaseHandle = $this->getTestCaseHandle(); diff --git a/tests/ElasticApmTests/ComponentTests/Util/DbAutoInstrumentationUtilForTests.php b/tests/ElasticApmTests/ComponentTests/Util/DbAutoInstrumentationUtilForTests.php index 7993ce668..fbcb3c552 100644 --- a/tests/ElasticApmTests/ComponentTests/Util/DbAutoInstrumentationUtilForTests.php +++ b/tests/ElasticApmTests/ComponentTests/Util/DbAutoInstrumentationUtilForTests.php @@ -24,7 +24,9 @@ namespace ElasticApmTests\ComponentTests\Util; use Elastic\Apm\Impl\Util\StaticClassTrait; +use ElasticApmTests\ComponentTests\MySQLi\MySQLiWrapped; use ElasticApmTests\Util\IterableUtilForTests; +use PDO; final class DbAutoInstrumentationUtilForTests { @@ -36,9 +38,9 @@ final class DbAutoInstrumentationUtilForTests public const PASSWORD_KEY = 'PASSWORD'; public const DB_NAME_KEY = 'DB_NAME'; - public const USE_SELECT_DB_KEY = 'USE_SELECT_DB'; public const WRAP_IN_TX_KEY = 'WRAP_IN_TX'; public const ROLLBACK_KEY = 'ROLLBACK'; + public const CALL_END_TX_IN_SHUTDOWN_FUNCTION_KEY = 'CALL_END_TX_IN_SHUTDOWN_FUNCTION'; /** * @return callable(array): iterable> @@ -53,16 +55,45 @@ public static function wrapTxRelatedArgsDataProviderGenerator(): callable return function (array $resultSoFar): iterable { foreach (IterableUtilForTests::ALL_BOOL_VALUES as $wrapInTx) { $rollbackValues = $wrapInTx ? [false, true] : [false]; + $callEndTxInShutdownFunctionValues = $wrapInTx ? [false, true] : [false]; foreach ($rollbackValues as $rollback) { - yield array_merge( - $resultSoFar, - [ - self::WRAP_IN_TX_KEY => $wrapInTx, - self::ROLLBACK_KEY => $rollback, - ] - ); + foreach ($callEndTxInShutdownFunctionValues as $callEndTxInShutdownFunction) { + yield array_merge( + $resultSoFar, + [ + self::WRAP_IN_TX_KEY => $wrapInTx, + self::ROLLBACK_KEY => $rollback, + self::CALL_END_TX_IN_SHUTDOWN_FUNCTION_KEY => $callEndTxInShutdownFunction, + ] + ); + } } } }; } + + /** + * @param PDO|MySQLiWrapped $dbObj + * @param bool $wrapInTx + * @param bool $rollback + * @param bool $callEndTxInShutdownFunction + * + * @return void + */ + public static function endTx($dbObj, bool $wrapInTx, bool $rollback, bool $callEndTxInShutdownFunction): void + { + if (!$wrapInTx) { + return; + } + + $endTxCallFunc = function () use ($rollback, $dbObj) { + ComponentTestCaseBase::assertTrue($rollback ? $dbObj->rollback() : $dbObj->commit()); + }; + + if ($callEndTxInShutdownFunction) { + register_shutdown_function($endTxCallFunc); + } else { + $endTxCallFunc(); + } + } } From 633f81c3dc33fa2df98019b4e433aeef4474a6d0 Mon Sep 17 00:00:00 2001 From: Sergey Kleyman Date: Mon, 13 Oct 2025 15:20:50 +0300 Subject: [PATCH 3/6] Fixed $callEndTxInShutdownFunction not assigned --- .../ComponentTests/PDOAutoInstrumentationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ElasticApmTests/ComponentTests/PDOAutoInstrumentationTest.php b/tests/ElasticApmTests/ComponentTests/PDOAutoInstrumentationTest.php index e1eeff0c2..657184209 100644 --- a/tests/ElasticApmTests/ComponentTests/PDOAutoInstrumentationTest.php +++ b/tests/ElasticApmTests/ComponentTests/PDOAutoInstrumentationTest.php @@ -193,7 +193,7 @@ public static function extractSharedArgs(MixedMap $args, /* out */ ?string &$dbN $dbName = $args->getString(DbAutoInstrumentationUtilForTests::DB_NAME_KEY); $wrapInTx = $args->getBool(DbAutoInstrumentationUtilForTests::WRAP_IN_TX_KEY); $rollback = $args->getBool(DbAutoInstrumentationUtilForTests::ROLLBACK_KEY); - $rollback = $args->getBool(DbAutoInstrumentationUtilForTests::CALL_END_TX_IN_SHUTDOWN_FUNCTION_KEY); + $callEndTxInShutdownFunction = $args->getBool(DbAutoInstrumentationUtilForTests::CALL_END_TX_IN_SHUTDOWN_FUNCTION_KEY); } public static function appCodeForTestAutoInstrumentation(MixedMap $appCodeArgs): void From 165b26d748edb04e0f269b2b4f97bebd5ec05a1d Mon Sep 17 00:00:00 2001 From: Sergey Kleyman Date: Mon, 13 Oct 2025 16:02:07 +0300 Subject: [PATCH 4/6] Prepend interceptRegistration when logging --- .../Impl/AutoInstrument/InterceptionManager.php | 15 +++++++-------- agent/php/ElasticApm/Impl/Log/Logger.php | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/agent/php/ElasticApm/Impl/AutoInstrument/InterceptionManager.php b/agent/php/ElasticApm/Impl/AutoInstrument/InterceptionManager.php index 6f795d796..063b756d4 100644 --- a/agent/php/ElasticApm/Impl/AutoInstrument/InterceptionManager.php +++ b/agent/php/ElasticApm/Impl/AutoInstrument/InterceptionManager.php @@ -89,26 +89,25 @@ public function internalFuncCallPreHook( array $interceptedCallArgs ): bool { $localLogger = $this->logger->inherit()->addAllContext( - [ - 'interceptRegistrationId' => $interceptRegistrationId, - 'thisObj type' => DbgUtil::getType($thisObj), + compact('interceptRegistrationId') + + [ + 'thisObj type' => DbgUtil::getType($thisObj), 'interceptedCallArgs count' => count($interceptedCallArgs), - 'thisObj' => $this->logger->possiblySecuritySensitive($thisObj), - 'interceptedCallArgs' => $this->logger->possiblySecuritySensitive($interceptedCallArgs), + 'thisObj' => $this->logger->possiblySecuritySensitive($thisObj), + 'interceptedCallArgs' => $this->logger->possiblySecuritySensitive($interceptedCallArgs), ] ); $loggerProxyTrace = $localLogger->ifTraceLevelEnabledNoLine(__FUNCTION__); $loggerProxyTrace && $loggerProxyTrace->log(__LINE__, 'Entered'); - $interceptRegistration - = ArrayUtil::getValueIfKeyExistsElse($interceptRegistrationId, $this->interceptedCallRegistrations, null); + $interceptRegistration = ArrayUtil::getValueIfKeyExistsElse($interceptRegistrationId, $this->interceptedCallRegistrations, null); if ($interceptRegistration === null) { ($loggerProxy = $localLogger->ifErrorLevelEnabled(__LINE__, __FUNCTION__)) && $loggerProxy->log('There is no registration with the given interceptRegistrationId'); return false; } - $localLogger->addContext('interceptRegistration', $interceptRegistration); + $localLogger->prependAllToContext(compact('interceptRegistration')); $loggerProxyTrace && $loggerProxyTrace->log(__LINE__, 'Calling preHook...'); try { diff --git a/agent/php/ElasticApm/Impl/Log/Logger.php b/agent/php/ElasticApm/Impl/Log/Logger.php index fd393d70a..5f888f20e 100644 --- a/agent/php/ElasticApm/Impl/Log/Logger.php +++ b/agent/php/ElasticApm/Impl/Log/Logger.php @@ -83,9 +83,18 @@ public function addContext(string $key, $value): self */ public function addAllContext(array $keyValuePairs): self { - foreach ($keyValuePairs as $key => $value) { - $this->addContext($key, $value); - } + $this->data->context = array_merge($this->data->context, $keyValuePairs); + return $this; + } + + /** + * @param array $keyValuePairs + * + * @return Logger + */ + public function prependAllToContext(array $keyValuePairs): self + { + $this->data->context = $keyValuePairs + $this->data->context; return $this; } From 527f571505ce955f747c9d25d712b7d6580f9ba5 Mon Sep 17 00:00:00 2001 From: Sergey Kleyman Date: Mon, 13 Oct 2025 16:35:35 +0300 Subject: [PATCH 5/6] Log pre/post hooks at DEBUG level --- agent/native/ext/lifecycle.cpp | 6 ++-- .../AutoInstrument/InterceptionManager.php | 32 +++++++++---------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/agent/native/ext/lifecycle.cpp b/agent/native/ext/lifecycle.cpp index e4fb860b8..a7e8c6bf5 100644 --- a/agent/native/ext/lifecycle.cpp +++ b/agent/native/ext/lifecycle.cpp @@ -400,6 +400,8 @@ auto buildPeriodicTaskExecutor() { return periodicTaskExecutor; } +static const char* gs_custom_build_log_msg_str = "(CUSTOM BUILD 2025-10-13)"; + void elasticApmRequestInit() { if (!ELASTICAPM_G(globals)->sapi_.isSupported()) { @@ -442,7 +444,7 @@ void elasticApmRequestInit() g_pidOnRequestInit = getCurrentProcessId(); - ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY_MSG( "parent PID: %d", (int)(getParentProcessId()) ); + ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY_MSG( "%s parent PID: %d", gs_custom_build_log_msg_str, (int)(getParentProcessId()) ); ResultCode resultCode; @@ -560,7 +562,7 @@ void elasticApmRequestShutdown() return; } - ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY(); + ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY_MSG( "%s", gs_custom_build_log_msg_str ); Tracer* const tracer = getGlobalTracer(); const ConfigSnapshot* const config = getTracerCurrentConfigSnapshot( tracer ); diff --git a/agent/php/ElasticApm/Impl/AutoInstrument/InterceptionManager.php b/agent/php/ElasticApm/Impl/AutoInstrument/InterceptionManager.php index 063b756d4..de2adc273 100644 --- a/agent/php/ElasticApm/Impl/AutoInstrument/InterceptionManager.php +++ b/agent/php/ElasticApm/Impl/AutoInstrument/InterceptionManager.php @@ -97,24 +97,24 @@ public function internalFuncCallPreHook( 'interceptedCallArgs' => $this->logger->possiblySecuritySensitive($interceptedCallArgs), ] ); - $loggerProxyTrace = $localLogger->ifTraceLevelEnabledNoLine(__FUNCTION__); + $loggerProxy = $localLogger->ifDebugLevelEnabledNoLine(__FUNCTION__); - $loggerProxyTrace && $loggerProxyTrace->log(__LINE__, 'Entered'); + $loggerProxy && $loggerProxy->log(__LINE__, 'Entered'); $interceptRegistration = ArrayUtil::getValueIfKeyExistsElse($interceptRegistrationId, $this->interceptedCallRegistrations, null); if ($interceptRegistration === null) { - ($loggerProxy = $localLogger->ifErrorLevelEnabled(__LINE__, __FUNCTION__)) - && $loggerProxy->log('There is no registration with the given interceptRegistrationId'); + ($loggerProxyError = $localLogger->ifErrorLevelEnabled(__LINE__, __FUNCTION__)) + && $loggerProxyError->log('There is no registration with the given interceptRegistrationId'); return false; } $localLogger->prependAllToContext(compact('interceptRegistration')); - $loggerProxyTrace && $loggerProxyTrace->log(__LINE__, 'Calling preHook...'); + $loggerProxy && $loggerProxy->log(__LINE__, 'Calling preHook...'); try { $preHookRetVal = ($interceptRegistration->preHook)($thisObj, $interceptedCallArgs); } catch (Throwable $throwable) { - ($loggerProxy = $localLogger->ifErrorLevelEnabled(__LINE__, __FUNCTION__)) - && $loggerProxy->logThrowable( + ($loggerProxyError = $localLogger->ifErrorLevelEnabled(__LINE__, __FUNCTION__)) + && $loggerProxyError->logThrowable( $throwable, 'preHook has let a Throwable to escape' ); @@ -128,7 +128,7 @@ public function internalFuncCallPreHook( $this->interceptedCallInProgressPreHookRetVal = $preHookRetVal; } - $loggerProxyTrace && $loggerProxyTrace->log(__LINE__, 'preHook completed successfully', ['shouldCallPostHook' => $shouldCallPostHook]); + $loggerProxy && $loggerProxy->log(__LINE__, 'preHook completed successfully', ['shouldCallPostHook' => $shouldCallPostHook]); return $shouldCallPostHook; } @@ -149,28 +149,28 @@ public function internalFuncCallPostHook( 'interceptRegistration' => $this->interceptedCallInProgressRegistration, ] ); - $loggerProxyTrace = $localLogger->ifTraceLevelEnabledNoLine(__FUNCTION__); - $loggerProxyTrace && $loggerProxyTrace->log(__LINE__, 'Entered'); + $loggerProxy = $localLogger->ifDebugLevelEnabledNoLine(__FUNCTION__); + $loggerProxy && $loggerProxy->log(__LINE__, 'Entered'); if ($this->interceptedCallInProgressRegistrationId === null) { - ($loggerProxy = $this->logger->ifErrorLevelEnabled(__LINE__, __FUNCTION__)) - && $loggerProxy->log('There is no intercepted call in progress'); + ($loggerProxyError = $this->logger->ifErrorLevelEnabled(__LINE__, __FUNCTION__)) + && $loggerProxyError->log('There is no intercepted call in progress'); return; } assert($this->interceptedCallInProgressRegistration !== null); assert($this->interceptedCallInProgressPreHookRetVal !== null); - $loggerProxyTrace && $loggerProxyTrace->log(__LINE__, 'Calling postHook...'); + $loggerProxy && $loggerProxy->log(__LINE__, 'Calling postHook...'); try { ($this->interceptedCallInProgressPreHookRetVal)( $numberOfStackFramesToSkip + 1, $hasExitedByException, $returnValueOrThrown ); - $loggerProxyTrace && $loggerProxyTrace->log(__LINE__, 'postHook completed without throwing'); + $loggerProxy && $loggerProxy->log(__LINE__, 'postHook completed without throwing'); } catch (Throwable $throwable) { - ($loggerProxy = $localLogger->ifErrorLevelEnabled(__LINE__, __FUNCTION__)) - && $loggerProxy->logThrowable($throwable, 'postHook has thrown'); + ($loggerProxyError = $localLogger->ifErrorLevelEnabled(__LINE__, __FUNCTION__)) + && $loggerProxyError->logThrowable($throwable, 'postHook has thrown'); } $this->interceptedCallInProgressRegistrationId = null; From b9eeb05d98be57559d5bac11a55643bfea4bcd4e Mon Sep 17 00:00:00 2001 From: Sergey Kleyman Date: Mon, 13 Oct 2025 17:21:07 +0300 Subject: [PATCH 6/6] Fixed failing tests --- .../MySQLiAutoInstrumentationTest.php | 16 ++++++++--- .../PDOAutoInstrumentationTest.php | 12 ++++++++- .../DbAutoInstrumentationUtilForTests.php | 27 ------------------- 3 files changed, 23 insertions(+), 32 deletions(-) diff --git a/tests/ElasticApmTests/ComponentTests/MySQLiAutoInstrumentationTest.php b/tests/ElasticApmTests/ComponentTests/MySQLiAutoInstrumentationTest.php index 826ea6a6b..1b1cfa4b6 100644 --- a/tests/ElasticApmTests/ComponentTests/MySQLiAutoInstrumentationTest.php +++ b/tests/ElasticApmTests/ComponentTests/MySQLiAutoInstrumentationTest.php @@ -400,10 +400,18 @@ public static function appCodeForTestAutoInstrumentation(MixedMap $appCodeArgs): } $queryResult->close(); - DbAutoInstrumentationUtilForTests::endTx($mySQLi, $wrapInTx, $rollback, $callEndTxInShutdownFunction); - - self::resetDbState($mySQLi, $queryKind); - self::assertTrue($mySQLi->close()); + $endTxCallFunc = function () use ($wrapInTx, $rollback, $mySQLi, $queryKind) { + if ($wrapInTx) { + self::assertTrue($rollback ? $mySQLi->rollback() : $mySQLi->commit()); + } + self::resetDbState($mySQLi, $queryKind); + self::assertTrue($mySQLi->close()); + }; + if ($callEndTxInShutdownFunction) { + register_shutdown_function($endTxCallFunc); + } else { + $endTxCallFunc(); + } } /** diff --git a/tests/ElasticApmTests/ComponentTests/PDOAutoInstrumentationTest.php b/tests/ElasticApmTests/ComponentTests/PDOAutoInstrumentationTest.php index 657184209..52eb420d1 100644 --- a/tests/ElasticApmTests/ComponentTests/PDOAutoInstrumentationTest.php +++ b/tests/ElasticApmTests/ComponentTests/PDOAutoInstrumentationTest.php @@ -228,7 +228,17 @@ public static function appCodeForTestAutoInstrumentation(MixedMap $appCodeArgs): self::assertEqualsEx(self::MESSAGES[$msgText], $row['time'], $dbgCtx); } - DbAutoInstrumentationUtilForTests::endTx($pdo, $wrapInTx, $rollback, $callEndTxInShutdownFunction); + if ($wrapInTx) { + $endTxCallFunc = function () use ($rollback, $pdo) { + self::assertTrue($rollback ? $pdo->rollBack() : $pdo->commit()); + }; + + if ($callEndTxInShutdownFunction) { + register_shutdown_function($endTxCallFunc); + } else { + $endTxCallFunc(); + } + } } /** diff --git a/tests/ElasticApmTests/ComponentTests/Util/DbAutoInstrumentationUtilForTests.php b/tests/ElasticApmTests/ComponentTests/Util/DbAutoInstrumentationUtilForTests.php index fbcb3c552..eb5fb72cc 100644 --- a/tests/ElasticApmTests/ComponentTests/Util/DbAutoInstrumentationUtilForTests.php +++ b/tests/ElasticApmTests/ComponentTests/Util/DbAutoInstrumentationUtilForTests.php @@ -24,9 +24,7 @@ namespace ElasticApmTests\ComponentTests\Util; use Elastic\Apm\Impl\Util\StaticClassTrait; -use ElasticApmTests\ComponentTests\MySQLi\MySQLiWrapped; use ElasticApmTests\Util\IterableUtilForTests; -use PDO; final class DbAutoInstrumentationUtilForTests { @@ -71,29 +69,4 @@ public static function wrapTxRelatedArgsDataProviderGenerator(): callable } }; } - - /** - * @param PDO|MySQLiWrapped $dbObj - * @param bool $wrapInTx - * @param bool $rollback - * @param bool $callEndTxInShutdownFunction - * - * @return void - */ - public static function endTx($dbObj, bool $wrapInTx, bool $rollback, bool $callEndTxInShutdownFunction): void - { - if (!$wrapInTx) { - return; - } - - $endTxCallFunc = function () use ($rollback, $dbObj) { - ComponentTestCaseBase::assertTrue($rollback ? $dbObj->rollback() : $dbObj->commit()); - }; - - if ($callEndTxInShutdownFunction) { - register_shutdown_function($endTxCallFunc); - } else { - $endTxCallFunc(); - } - } }