Skip to content

Commit 7e35165

Browse files
committed
MAGE-1105 Address potential profiler mismatches
1 parent a8b6686 commit 7e35165

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

Exception/DiagnosticsException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Exception;
4+
5+
use Magento\Framework\Exception\LocalizedException;
6+
7+
class DiagnosticsException extends LocalizedException
8+
{
9+
10+
}

Helper/Entity/ProductHelper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,13 +511,14 @@ public function getObject(Product $product)
511511
*/
512512
protected function getSubProducts(Product $product): array
513513
{
514-
$this->logger->startProfiling(__METHOD__);
515514
$type = $product->getTypeId();
516515

517516
if (!in_array($type, ['bundle', 'grouped', 'configurable'], true)) {
518517
return [];
519518
}
520519

520+
$this->logger->startProfiling(__METHOD__);
521+
521522
$storeId = $product->getStoreId();
522523
$typeInstance = $product->getTypeInstance();
523524

Logger/DiagnosticsLogger.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Algolia\AlgoliaSearch\Logger;
44

5-
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
5+
use Algolia\AlgoliaSearch\Exception\DiagnosticsException;
66
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
77
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
88
use Magento\Framework\Exception\NoSuchEntityException;
@@ -20,11 +20,15 @@ class DiagnosticsLogger
2020
protected bool $isLoggerEnabled = false;
2121
protected bool $isProfilerEnabled = false;
2222

23+
/** @var string[] */
24+
private array $timerStack = [];
25+
2326
public function __construct(
2427
protected ConfigHelper $config,
2528
protected TimedLogger $logger,
2629
protected StoreNameFetcher $storeNameFetcher
27-
) {
30+
)
31+
{
2832
$this->isLoggerEnabled = $this->config->isLoggingEnabled();
2933
$this->isProfilerEnabled = $this->config->isProfilerEnabled();
3034
}
@@ -63,7 +67,7 @@ public function start(string $action, bool $profileMethod = self::PROFILE_LOG_ME
6367

6468

6569
/**
66-
* @throws AlgoliaException
70+
* @throws DiagnosticsException
6771
*/
6872
public function stop(string $action, bool $profileMethod = self::PROFILE_LOG_MESSAGES_DEFAULT): void
6973
{
@@ -83,13 +87,26 @@ public function startProfiling(string $timerName): void
8387
{
8488
if (!$this->isProfilerEnabled) return;
8589

90+
$timerName = $this->simplifyMethodName($timerName);
91+
$this->timerStack[] = $timerName;
92+
8693
Profiler::start($timerName, self::ALGOLIA_TAGS);
8794
}
8895

96+
/**
97+
* @throws DiagnosticsException
98+
*/
8999
public function stopProfiling(string $timerName): void
90100
{
91101
if (!$this->isProfilerEnabled) return;
92102

103+
$lastTimerName = array_pop($this->timerStack); //$this->timerStack[count($this->timerStack) - 1];
104+
$timerName = $this->simplifyMethodName($timerName);
105+
106+
if ($lastTimerName !== $timerName) {
107+
throw new DiagnosticsException(__("Profiling on %1 was stopped before nested operation %2 completed.", $timerName, $lastTimerName));
108+
}
109+
93110
Profiler::setDefaultTags(self::ALGOLIA_TAGS);
94111
Profiler::stop($timerName);
95112
Profiler::setDefaultTags([]);
@@ -102,7 +119,8 @@ public function log(string $message, int $logLevel = Logger::INFO): void
102119
}
103120
}
104121

105-
public function error(string $message): void {
122+
public function error(string $message): void
123+
{
106124
if ($this->isLoggerEnabled) {
107125
$this->logger->log($message, Logger::ERROR);
108126
}
@@ -111,6 +129,7 @@ public function error(string $message): void {
111129
/**
112130
* Gets the name of the method that called the diagnostics
113131
*
132+
* @param int $level
114133
* @return string|null
115134
*/
116135
protected function getCallingMethodName(int $level = 2): ?string
@@ -120,4 +139,21 @@ protected function getCallingMethodName(int $level = 2): ?string
120139
? $backtrace[$level]['class'] . "::" . $backtrace[$level]['function']
121140
: null;
122141
}
142+
143+
protected function simplifyMethodName(string $methodName, int $namespaceDepth = 2): string
144+
{
145+
$separator = '\\';
146+
$parts = explode($separator, $methodName);
147+
148+
if (count($parts) <= $namespaceDepth) {
149+
return $methodName;
150+
}
151+
152+
$simplified = implode($separator, array_slice($parts, $namespaceDepth));
153+
if (!count($this->timerStack)) {
154+
$simplified = "ALGOLIA:" . $simplified;
155+
}
156+
return $simplified;
157+
}
158+
123159
}

Logger/TimedLogger.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Algolia\AlgoliaSearch\Logger;
44

5-
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
5+
use Algolia\AlgoliaSearch\Exception\DiagnosticsException;
66
use Monolog\Logger;
77
use Psr\Log\LoggerInterface;
88

@@ -25,12 +25,12 @@ public function start($action): void
2525
}
2626

2727
/**
28-
* @throws AlgoliaException
28+
* @throws DiagnosticsException
2929
*/
3030
public function stop($action): void
3131
{
3232
if (false === isset($this->timers[$action])) {
33-
throw new AlgoliaException('Algolia Logger => non existing action');
33+
throw new DiagnosticsException(__('Algolia Logger => non existing action'));
3434
}
3535

3636
$this->log('<<<<< END ' . $action . ' (' . $this->formatTime($this->timers[$action], microtime(true)) . ')');

Setup/.DS_Store

-6 KB
Binary file not shown.

0 commit comments

Comments
 (0)