In this tutorial, we will demonstrates how to write a simple Ylang analyzer to dynamically trace an aribitrary Go program in OpenResty XRay . This custom analyzer adds a dynamic probe to the entry point of a specified Go function and then dump the content of a Go map-typed variable. Thanks to our dynamic tracing technology, the analyzer can work in a truly noninvasive way with minimal performance overhead. No changes are made to the target Go program and process.

Insert two key-value Pairs into a Go Variable

We have a program called animal written in Go.

Screenshot

We use the Vim editor to take a look at the source code of this program.

Screenshot

Look at this program, Animal is a struct.

Screenshot

We can see the Animal struct has two members. The food is a string field, and age is a float32 field.

Screenshot

The showAnimals function displays the values in the map variable.

Screenshot

It has a parameter animals of type map[string]Animal.

Screenshot

map[string]Animal is a map aggregation type. The key type is string and the value type is Animal.

Screenshot

In the main function, insert two key-value pairs into this map variable.

Screenshot

We are using the unmodified Go compiler that comes with the Linux distribution.

Screenshot

Let’s compile this program. As shown, we don’t need to add any special options.

Screenshot

Here is the output of the program.

Screenshot

This program had a very fast execution time, lasting only 1 millisecond.

Screenshot

Let’s write a custom Ylang analyzer to dynamically trace the execution of this Go program.

Screenshot

Write a Customize Ylang Analyzer to Dynamically Trace the Go program

Switch to the OpenResty XRay Web console.

Screenshot

Go to the “Analyzers” page.

Screenshot

Click on the “Add new analyzer”.

Screenshot

Enter the name in the “Analyzer name” textbox.

Screenshot

Select the “YLang” language type.

Screenshot

Delete the sample Ylang program provided by default.

Screenshot

Here we add a dynamic probe onto the entry point of our Go function showAnimals in the main package.

Screenshot

This parameter type is a map with string keys and values of type main.Animal.

Screenshot

The parameter name is animals.

Screenshot

Call _dump_go_val to print the value of the animals variable.

Screenshot

Call _exit() to make the analyzer exit after running this probe handler.

Screenshot

Save the code.

Screenshot

This is a short-lived program, so we choose the “By Executables” mode.

Screenshot

Let’s go back to the terminal window.

Screenshot

Use the readlink -f command to find the exe path.

Screenshot

Copy it.

Screenshot

Paste the executable file path.

Screenshot

Click “Run” to start execution.

Screenshot

Tracing has started.

Screenshot

Test the Results

Switch to the terminal.

Screenshot

Execute the Go program. As shown, the program is operating normally.

Screenshot

The execution time of this program remained swift, still only 1 millisecond.

Screenshot

Go back to the Web console. We can see the XRay analyzer printed the value of the animals variable correctly. The map contains two key-value pairs.

Screenshot

The value that the key cow points to is of type main.Animal.

Screenshot

main.Animal has two members, which are food and age.

Screenshot

The type of member food is a string and its value is “grass”.

Screenshot

The length of this string is 5.

Screenshot

The second field of main.Animal is age.

Screenshot

The type of age is float32.

Screenshot

It is similar to the previous key.

Screenshot

According to actual needs, we can also choose the “By Processes” mode.

Screenshot

Or choose the “By Applications” mode.

Screenshot

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.