How does the metrics package record the number of non default behavior occurrences
目录
Go1.21 Forward Looking
Software translation
How does the metrics package record the number of non default behavior occurrences
Go1.21 is currently under development, draft release noteIt is mentioned that it is expected to be released in August 2023.
In the development branch of go1.21 In, metrics package, a series of indicators in the format /godebug/non-default-behavior/(godebug-name):events ,(godebug name) is a series of key value pairs for GODEBUG environment variables, and the names of certain keys, such as the execerrdot introduced in go1.19 (hereinafter referred to as godebug indicator). Through these newly added indicators, the number of non default behavior occurrences caused by setting these environmental variables can be obtained. This article analyzes how this is achieved by studying the source code.
Firstly, this article uses the go standard library source code (installed through gotip), with a specific version number (go version devel go1.21-8d68b388d4 Wed Apr 5 21:45:24 2023+0000 windows/amd64).
The internal/godebug package in the Go standard library has the following APIs:
type Setting struct
func New(name string) *Setting
func (s *Setting) IncNonDefault()
func (s *Setting) Name() string
func (s *Setting) String() string
func (s *Setting) Value() string
In the Setting structure, there is a non default behavior counter of type atomic.Uint64, which is an atomic type introduced in go1.19. The API is as follows
type Uint64 struct
func (x *Uint64) Add(delta uint64) (new uint64)
func (x *Uint64) CompareAndSwap(old, new uint64) (swapped bool)
func (x *Uint64) Load() uint64
func (x *Uint64) Store(val uint64)
func (x *Uint64) Swap(new uint64) (old uint64)
The IncNonDefault method of this structure will atomically add 1 to the non default behavior counter every time it is called. On the first call, the Load method of the non default behavior counter will also be debugged through the runtime package’s godebug_ The registerMetric function is set to obtain the value corresponding to the godebug indicator.
By calling the IncNonDefault method every time a non default behavior occurs, the number of non default behavior occurrences can be recorded in the Setting structure.
godebug_registerMetric The signature of the function is as follows:
func godebug_registerMetric(name string, read func() uint64)
This function sets the value of the name parameter to represent the indicator, and the function to obtain the corresponding value of the indicator as the parameter passed by the read parameter.
The ability of the metrics package to obtain the value corresponding to the godebug indicator comes from the runtime package.
In this way, the order in which non default behavior occurrences are recorded and obtained is:
-
Each godebug option in the go standard library is created through the New() function of the internal/codebug package, and the return value contains non default behavior counters.
-
The IncNonDefault method of the Setting structure is called every time a non default behavior occurs.
-
The IncNonDefault method sets the function that obtains the value corresponding to the godebug indicator as the Load method of the non default behavior counter.
-
The IncNonDefault method Atomic plus 1 to the non default behavior counter atom.
-
Every time the value corresponding to the godebug indicator is obtained, the Load method of the non default behavior counter is called, which implements the phenomenon of the metrics package recording the number of non default behavior occurrences (actually recorded in the Setting structure, but the metrics package shows this phenomenon through the runtime package, which is demonstrated by calling the Load method of the non default behavior counter of the Setting structure).