@@ -65,17 +65,44 @@ private function executeWithTiming(string $methodName, array $arguments)
65
65
$ sql = preg_replace ('/\?/ ' , $ value , $ sql , 1 );
66
66
}
67
67
68
+ // Capture the call stack up to the maximum depth
69
+ $ maxStackDepth = $ builderConfig ['maxStackDepth ' ] ?? 5 ;
70
+ $ stackTrace = $ this ->getStackTrace ($ maxStackDepth );
71
+
68
72
// Log the slow Eloquent method execution
69
73
Log::info ('QueryMonitor: Slow Eloquent method detected ' , [
70
74
'method ' => $ methodName ,
71
- 'execution_time ' => $ executionTime . ' ms ' ,
75
+ 'arguments ' => $ arguments ,
72
76
'query ' => $ sql ,
77
+ 'execution_time ' => $ executionTime . ' ms ' ,
78
+ 'stack_trace ' => $ stackTrace ,
73
79
]);
74
80
75
81
// Return the result of the parent method
76
82
return $ results ;
77
83
}
78
84
85
+ /**
86
+ * Captures the stack trace up to the specified depth.
87
+ */
88
+ private function getStackTrace (int $ maxStackDepth ): array
89
+ {
90
+ $ backtrace = debug_backtrace (DEBUG_BACKTRACE_IGNORE_ARGS , $ maxStackDepth + 1 );
91
+ $ stackTrace = [];
92
+
93
+ for ($ i = 1 ; $ i <= $ maxStackDepth && isset ($ backtrace [$ i ]); $ i ++) {
94
+ $ frame = $ backtrace [$ i ];
95
+ $ stackTrace [] = [
96
+ 'file ' => $ frame ['file ' ] ?? '' ,
97
+ 'line ' => $ frame ['line ' ] ?? '' ,
98
+ 'class ' => $ frame ['class ' ] ?? '' ,
99
+ 'function ' => $ frame ['function ' ] ?? '' ,
100
+ ];
101
+ }
102
+
103
+ return $ stackTrace ;
104
+ }
105
+
79
106
/**
80
107
* Overrides the get method to include performance monitoring.
81
108
*
@@ -126,10 +153,10 @@ public function pluck($column, $key = null)
126
153
/**
127
154
* Overrides the paginate method to include performance monitoring.
128
155
*
129
- * @param int|null $perPage Items per page.
130
- * @param array|string $columns The columns to retrieve.
131
- * @param string $pageName The page query string parameter.
132
- * @param int|null $page The current page.
156
+ * @param int|null $perPage Items per page.
157
+ * @param array|string $columns The columns to retrieve.
158
+ * @param string $pageName The page query string parameter.
159
+ * @param int|null $page The current page.
133
160
* @return LengthAwarePaginator The paginator instance.
134
161
*/
135
162
public function paginate ($ perPage = null , $ columns = ['* ' ], $ pageName = 'page ' , $ page = null )
0 commit comments