Go: Instrumentation in Go

Vincent
A Journey With Go
Published in
3 min readSep 9, 2019

--

Illustration created for “A Journey With Go”, made from the original Go Gopher, created by Renee French.

ℹ️ This article is based on Go 1.13.

go test command provides many great features like code coverage, CPU and memory profiling. Providing those stats will require Go to have a way to track the CPU usage or when a function is being used for the code coverage.

Instrumentation

Go uses many ways to produce those stats:

  • It dynamically inserts instrumentation statements that allow it to track when the code enters in a function or a condition. This strategy is used through code coverage.
  • Records a sample of the programs about multiple times per second. This strategy is used in CPU profiling.
  • Using static hooks in the code in order to call, during the execution, the functions it needs. This strategy is used in memory profiling.

Let’s write a simple program and review all of them. Here is the code we will use in the next sections:

Code coverage

The SSA code generation, via the command GOSSAFUNC=run go test -cover, will allow…

--

--