When a Go process pins a CPU core above 100%, the cause is usually a hot code path burning cycles. We profiled a live, unmodified Go chat service with OpenResty XRay and traced its high CPU usage to regular expression compilation — the standard regexp library functions, called from the business function CheckMessage at line 17 of prev_processor.go, accounted for 36.8% of all CPU time. Compiling regular expressions is expensive and should be avoided in hot code paths.

This walkthrough shows the complete profiling process — no code changes, no restarts required — from observing the Go high CPU usage to pinpointing the exact source file and line number responsible. Worried about running this on a production box? We measured OpenResty XRay’s overhead on a live Go service — throughput, latency, and CPU while sampling.

Observing High CPU Usage in a Go Process

The top command shows a Go process named chat-service consuming more than 100% of a CPU core. (If you have the opposite symptom — CPU usage stays low while requests pile up — see tracing a Go process stuck at 2% CPU instead.)

top command showing the Go chat-service process consuming more than 100% of a CPU core

Profiling Go CPU Usage with OpenResty XRay

We use OpenResty XRay to analyze this unmodified process in real time — no special modules, no code changes, no restarts required. In the web console, navigate to Guided Analysis and select High CPU Usage as the problem type. OpenResty XRay auto-discovers running applications on the target machine. Select the Go application from the dropdown.

OpenResty XRay auto-discovering running applications, with the Go application selected

Select the specific process consuming 96% CPU — the same one we observed in top.

Selecting the Go chat-service process at 96% CPU for analysis

Set the language level to Go and keep the maximum analysis time at the default 300 seconds. After starting the analysis, the system performs several rounds; two rounds are enough for this case.

Language level set to Go with maximum analysis time of 300 seconds

CPU Flame Graph Analysis

OpenResty XRay automatically creates a report.

Automatically generated CPU analysis report for the Go process

This report shows the most critical Go-land code paths that consume the most CPU time. The top one is regular expression compilation, which takes up 36.8% of the CPU time.

Report showing regular expression compilation as the top CPU consumer at 36.8%

These two functions are responsible for compiling regular expressions. They are part of the standard regexp library in Go’s runtime.

Two standard regexp library functions responsible for compiling regular expressions

This CheckMessage function is where our business logic happens. It calls the regex compilation functions we just saw.

CheckMessage business-logic function calling the regex compilation functions

Pinpointing the Go Source File and Line Number

To learn more about this code path, click the More link.

Clicking the More link to see detailed analysis of the hottest code path

This opens a detailed view of the code path, derived from this Go-land CPU flame graph.

Go-level CPU flame graph generated by OpenResty XRay

Here we see explanations and suggestions on how to improve the performance of this code path.

Detailed explanations and suggestions for the CPU hotspot

For example, it tells us that the regular expression compilation functions are very expensive and should be avoided if possible.

Report noting that regular expression compilation functions are very expensive

Next, it explains the business-level function CheckMessage and that it uses and compiles regular expressions.

Report explaining the CheckMessage business function and its regex compilation

It also covers the compiled regular expressions.

Report describing the compiled regular expressions

Back on the code path, hover the mouse over the green box for the Go function named CheckMessage. We can see the Go source file of the CheckMessage function, and its full path for the prev_processor.go file in the tooltip.

Tooltip showing the prev_processor.go source file path for the CheckMessage function

The Go source line number is 17.

Go source line number 17 shown in the flame graph tooltip

Click the icon to copy the full Go source file path for this function.

Copying the full Go source file path from the flame graph

Use the vim editor and paste the path we just copied to look at the corresponding business Go code. You can use any editor you like.

Opening the Go source file in the vim editor

Go to line 17 as shown in the report tooltip.

Viewing line 17 of the Go source file

We can see that it does the regular expression compilation, calling the regexp.MustCompile function.

Go source code at line 17 calling regexp.MustCompile

It is also in the function CheckMessage as shown in the report. It’s now easy to optimize the Go code!

The CheckMessage function definition in the Go source file

Automatic Monitoring and Reports

For production workloads, OpenResty XRay monitors processes continuously and generates daily and weekly reports on the Insights page — no manual guided analysis required.

Insights page showing automatic daily and weekly CPU reports for the Go application

Beyond high CPU, OpenResty XRay can likewise trace Go panics on a running process without restarts or code changes.

FAQ

Why is my Go program using high CPU?

A common cause of Go high CPU usage is an expensive operation in a hot code path. In this case, OpenResty XRay traced the CPU bottleneck to regular expression compilation — the standard regexp library functions accounted for 36.8% of the CPU time — called from the business function CheckMessage at line 17 of prev_processor.go. CPU flame graphs reveal the full call chain from the business logic down to the runtime library functions.

Is compiling regular expressions expensive in Go?

Yes. Compiling a regular expression builds a matching automaton, which is far more costly than executing an already-compiled pattern. OpenResty XRay’s report explicitly flags the regexp compilation functions as very expensive and recommends avoiding them in hot code paths. The standard practice is to compile each pattern once and reuse the compiled result rather than recompiling it repeatedly.

How do I find the Go code causing high CPU?

Use OpenResty XRay’s guided analysis to generate a Go-land CPU flame graph. The flame graph shows stacked function calls where width represents CPU time. Hover over a function box to see the source file path and line number, then open that file in any editor to inspect the exact code responsible — no instrumentation or code changes required.

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.