How to Trace Exceptions inside PHP Applications (using OpenResty XRay)
In this tutorial, we will walk through how to use OpenResty XRay to detect and analyze the exceptions in the PHP applications online. OpenResty XRay offers the capability to detect and analyze exceptions or panics in online PHP processes, including caught ones that do not lead to process crashes. Notably, OpenResty XRay can even detect exceptions that have been caught and handled by the application itself, greatly enhancing problem diagnosis capabilities. The code paths are obtained by OpenResty XRay automatically analyzing and interpreting the PHP code-level Exception Flame Graphs. It will minimize the performance impact. This makes it ideal for production environments sensitive to performance overhead and latency.
How to detect and analyze exceptions inside PHP applications
First, run the ps
command to see the full command line for this process.
We can see it is the standard PHP binary executable shipped with the Linux distribution.
Let’s use OpenResty XRay to check out this unmodified process. We can analyze it in real time and figure out what’s going on.
Switich to the OpenResty XRay Web console.
Make sure it is the right machine you are watching.
Go to the “Guided Analysis” page.
Here you can see different types of problems that you can diagnose.
Let’s select “Errors & exceptions”.
Click on “Next”.
Select the PHP application.
Select “Whole application”.
Make sure that the application type is right. Usually, the default should be correct.
OpenResty XRay can analyze multiple language levels at the same time. We’ll keep both Perl and C/C++ selected.
We can also set the maximum analyzing time. We’ll leave it as 300 seconds, which is the default value.
Let’s start analyzing.
The system will keep performing different rounds of analysis. Now, it’s executing the first round.
The first round is done and it’s already on the second one. That’s enough for this case.
Stop analyzing.
It automatically generated an analysis report.
This is the type of problem we diagnose. It’s Errors & Exceptions.
This refers to PHP code paths. We can see it displays the code paths that throw the most exceptions.
First, we can observe an error showing an out-of-bound array access.
This error occurred within the business code in the function getOrder
.
The Laravel routing component automatically calls the controller method callAction to execute the corresponding business code.
Click on “More”.
This hot code path is automatically inferred from this PHP-land exception flame graph.
Below are more detailed explanations regarding the current issue.
It mentions the exceptions above.
It points out the location of the PHP error occurrence.
We can avoid this error by following the suggestions given here. OpenResty XRay also provides a code example that can fix this error.
Let’s go back to the original hot code path.
Hover the mouse over the green box for the getOrder
function.
We can see the PHP source file of this function in the tooltip.
The source line number is 54.
Click the icon to copy the source file path.
Use the vim editor to open the source file. Paste the file path we just copied. You can use any editor you like.
Go to line 54, as OpenResty XRay suggested.
We can see the access to PHP array elements. The value of the orderId variable sometimes clearly exceeds the array boundaries. The user needs to check the value of this array index before this line of source code, but hasn’t done so yet.
Although this exception is caught by the application itself, OpenResty XRay can still detect it.
Next, check the second code path that throws out exceptions.
PHP business code actively throws this exception using the throw statement.
This exception was also triggered by the getOrder
function in this code path.
Automatic analysis and reports
OpenResty XRay can also monitor online processes automatically and generate analysis reports.
Go to the “Insights” page.
You can find the reports on the “Insights” page for daily and weekly periods. For this reason, you don’t have to use the “Guided Analysis” feature.
Though “Guided analysis” is useful for application development and demonstration purposes.
What is OpenResty XRay
OpenResty XRay is a dynamic-tracing product that automatically analyzes your running applications to troubleshoot performance problems, behavioral issues, and security vulnerabilities with actionable suggestions. Under the hood, OpenResty XRay is powered by our Y language targeting various runtimes like Stap+, eBPF+, GDB, and ODB, depending on the contexts.
If you like this tutorial, please subscribe to this blog site and/or our YouTube channel. Thank you!
About The Author
Yichun Zhang (Github handle: agentzh), is the original creator of the OpenResty® open-source project and the CEO of OpenResty Inc..
Yichun is one of the earliest advocates and leaders of “open-source technology”. He worked at many internationally renowned tech companies, such as Cloudflare, Yahoo!. He is a pioneer of “edge computing”, “dynamic tracing” and “machine coding”, with over 22 years of programming and 16 years of open source experience. Yichun is well-known in the open-source space as the project leader of OpenResty®, adopted by more than 40 million global website domains.
OpenResty Inc., the enterprise software start-up founded by Yichun in 2017, has customers from some of the biggest companies in the world. Its flagship product, OpenResty XRay, is a non-invasive profiling and troubleshooting tool that significantly enhances and utilizes dynamic tracing technology. And its OpenResty Edge product is a powerful distributed traffic management and private CDN software product.
As an avid open-source contributor, Yichun has contributed more than a million lines of code to numerous open-source projects, including Linux kernel, Nginx, LuaJIT, GDB, SystemTap, LLVM, Perl, etc. He has also authored more than 60 open-source software libraries.