SoFunction
Updated on 2025-03-03

Discussion and analysis of adding metrics indicators in Go standard library

Quickly learn about runtime/metrics

Here is a quick demo. The code is as follows:

func main() {
    descs := ()
    samples := make([], len(descs))
    for i := range samples {
        samples[i].Name = descs[i].Name
    }
    (samples)
    for _, sample := range samples {
        name, value := , 
        switch () {
        case metrics.KindUint64:
            ("%s: %d\n", name, value.Uint64())
        case metrics.KindFloat64:
            ("%s: %f\n", name, value.Float64())
        case metrics.KindFloat64Histogram:
            ("%s: %f\n", name, medianBucket(value.Float64Histogram()))
         ...
        }
    }
}
func medianBucket(h *metrics.Float64Histogram) float64 {
    total := uint64(0)
    for _, count := range  {
        total += count
    }
    thresh := total / 2
    total = 0
    for i, count := range  {
        total += count
        if total >= thresh {
            return [i]
        }
    }
    panic("should not happen")
}

Output result:

/cgo/go-to-c-calls:calls: 0
/cpu/classes/gc/mark/assist:cpu-seconds: 0.000000
/cpu/classes/gc/mark/dedicated:cpu-seconds: 0.000000
...
/gc/cycles/automatic:gc-cycles: 0
/gc/cycles/forced:gc-cycles: 0
/gc/cycles/total:gc-cycles: 0
/gc/gogc:percent: 100
/gc/gomemlimit:bytes: 9223372036854775807
/gc/heap/allocs-by-size:bytes: 8193.000000
/gc/heap/allocs:bytes: 56832
/gc/heap/allocs:objects: 6
/gc/heap/frees-by-size:bytes: 1.000000
/gc/heap/frees:bytes: 0
/gc/heap/frees:objects: 0
/gc/heap/goal:bytes: 4194304
...

It contains quite a lot of Go system indicators. The complete code run and output can be viewed/play/p/CKASbysqX9x

I sorted out a list of comparisons. Ten of the indicators are as follows:

Serial number index meaning
1 /cgo/go-to-c-calls:calls The number of times the current process calls from Go to C
2 /cpu/classes/gc/mark/assist:cpu-seconds The total CPU time it is expected to be spent executing a GC program to assist the GC and prevent it from falling behind the application
3 /cpu/classes/gc/mark/dedicated:cpu-seconds Total CPU time expected to be spent on a CPU processor dedicated to performing GC tasks (as defined by GOMAXPROCS)
4 /cpu/classes/gc/mark/idle:cpu-seconds Total CPU time spent executing GC tasks on idle CPU resources
5 /cpu/classes/gc/pause:cpu-seconds Total CPU time expected to take for GC to pause an application
6 /gc/cycles/automatic:gc-cycles The number of GC loops that the Go Runtime program has completed.
7 /gc/gogc:percent User-configured heap size target percentage
8 /gc/heap/allocs:objects Application-triggered heap allocation cumulative count
9 /memory/classes/heap/free:bytes Go Runtime Estimate of the available space for physical memory (completely idle and can return memory that is not returned yet)
10 /sched/gomaxprocs:threads The current value, or the number of operating system threads that can execute user-level Go code at the same time.

Those who are interested in the complete indicators can view:

/runtime/metrics#hdr-Supported_metrics

More metrics metrics

Recently, a classmate in the Go community launched a discussion "metrics for the standard library》, hoping to explore and add more metrics metrics to other standard libraries to provide more observability.

What many students expect is network and delay indicators, which are for performance, errors, etc. The following scenarios:

net/http server:

  • Processing delay.
  • Request/response body size.
  • panic, recovery.
  • Error/Warning (triggerednet/All contents of  )
  • An invalid request denied.

net/http client:

  • Call delay.
  • Request/response body size
  • Connection pool related.

database/sql client:

  • Query delay.
  • Response size.
  • Connection pool related.

net network packet related:

  • For example, TCP, UDP, etc., the corresponding number of open connections, connection status (idle, activated, closed), connection errors, etc.
  • For example, TLS, indicators related to the handshake stage, handshake duration, handshake failure count, etc.

Summarize

Overall, we will find that everyone's indicator demands for the Go standard library tend to be more towards the underlying package. Because no matter what open source repository you are using, most of them are based on the packages mentioned above.

At this stage, if your own Go business application records these metrics, it needs to encapsulate another layer, and each package, such as ORM, needs to implement plug-ins, etc.

The third-party library implementation will not directly add non-core functions such as metrics to the initialization implementation. Therefore, the official standard library supports metrics.

At least this way, there is no need for every team to configure and set delayed call indicators such as net/http, database/sql, etc.

The above is the detailed discussion and analysis of the Go standard library's metrics index. For more information about the Go standard library's metrics index, please pay attention to my other related articles!