2
2
3
3
namespace Algolia \AlgoliaSearch \Logger ;
4
4
5
- use Algolia \AlgoliaSearch \Exceptions \ AlgoliaException ;
5
+ use Algolia \AlgoliaSearch \Exception \ DiagnosticsException ;
6
6
use Algolia \AlgoliaSearch \Helper \ConfigHelper ;
7
7
use Algolia \AlgoliaSearch \Service \StoreNameFetcher ;
8
8
use Magento \Framework \Exception \NoSuchEntityException ;
@@ -20,11 +20,15 @@ class DiagnosticsLogger
20
20
protected bool $ isLoggerEnabled = false ;
21
21
protected bool $ isProfilerEnabled = false ;
22
22
23
+ /** @var string[] */
24
+ private array $ timerStack = [];
25
+
23
26
public function __construct (
24
27
protected ConfigHelper $ config ,
25
28
protected TimedLogger $ logger ,
26
29
protected StoreNameFetcher $ storeNameFetcher
27
- ) {
30
+ )
31
+ {
28
32
$ this ->isLoggerEnabled = $ this ->config ->isLoggingEnabled ();
29
33
$ this ->isProfilerEnabled = $ this ->config ->isProfilerEnabled ();
30
34
}
@@ -63,7 +67,7 @@ public function start(string $action, bool $profileMethod = self::PROFILE_LOG_ME
63
67
64
68
65
69
/**
66
- * @throws AlgoliaException
70
+ * @throws DiagnosticsException
67
71
*/
68
72
public function stop (string $ action , bool $ profileMethod = self ::PROFILE_LOG_MESSAGES_DEFAULT ): void
69
73
{
@@ -83,13 +87,26 @@ public function startProfiling(string $timerName): void
83
87
{
84
88
if (!$ this ->isProfilerEnabled ) return ;
85
89
90
+ $ timerName = $ this ->simplifyMethodName ($ timerName );
91
+ $ this ->timerStack [] = $ timerName ;
92
+
86
93
Profiler::start ($ timerName , self ::ALGOLIA_TAGS );
87
94
}
88
95
96
+ /**
97
+ * @throws DiagnosticsException
98
+ */
89
99
public function stopProfiling (string $ timerName ): void
90
100
{
91
101
if (!$ this ->isProfilerEnabled ) return ;
92
102
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
+
93
110
Profiler::setDefaultTags (self ::ALGOLIA_TAGS );
94
111
Profiler::stop ($ timerName );
95
112
Profiler::setDefaultTags ([]);
@@ -102,7 +119,8 @@ public function log(string $message, int $logLevel = Logger::INFO): void
102
119
}
103
120
}
104
121
105
- public function error (string $ message ): void {
122
+ public function error (string $ message ): void
123
+ {
106
124
if ($ this ->isLoggerEnabled ) {
107
125
$ this ->logger ->log ($ message , Logger::ERROR );
108
126
}
@@ -111,6 +129,7 @@ public function error(string $message): void {
111
129
/**
112
130
* Gets the name of the method that called the diagnostics
113
131
*
132
+ * @param int $level
114
133
* @return string|null
115
134
*/
116
135
protected function getCallingMethodName (int $ level = 2 ): ?string
@@ -120,4 +139,21 @@ protected function getCallingMethodName(int $level = 2): ?string
120
139
? $ backtrace [$ level ]['class ' ] . ":: " . $ backtrace [$ level ]['function ' ]
121
140
: null ;
122
141
}
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
+
123
159
}
0 commit comments