【Golang】性能调优 - via Prometheus

Posted by 西维蜀黍 on 2021-07-18, Last Modified on 2021-10-17
// NewGoCollector returns a collector that exports metrics about the current Go
// process. This includes memory stats. To collect those, runtime.ReadMemStats
// is called. This requires to “stop the world”, which usually only happens for
// garbage collection (GC). Take the following implications into account when
// deciding whether to use the Go collector:
//
//...
func NewGoCollector() Collector {
	return &goCollector{
		goroutinesDesc: NewDesc(
			"go_goroutines",
			"Number of goroutines that currently exist.",
			nil, nil),
		threadsDesc: NewDesc(
			"go_threads",
			"Number of OS threads created.",
			nil, nil),
		gcDesc: NewDesc(
			"go_gc_duration_seconds",
			"A summary of the pause duration of garbage collection cycles.",
			nil, nil),
		goInfoDesc: NewDesc(
			"go_info",
			"Information about the Go environment.",
			nil, Labels{"version": runtime.Version()}),
		msLast:    &runtime.MemStats{},
		msRead:    runtime.ReadMemStats,
		msMaxWait: time.Second,
		msMaxAge:  5 * time.Minute,
		msMetrics: memStatsMetrics{
			{
				desc: NewDesc(
					memstatNamespace("alloc_bytes"),
					"Number of bytes allocated and still in use.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.Alloc) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("alloc_bytes_total"),
					"Total number of bytes allocated, even if freed.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.TotalAlloc) },
				valType: CounterValue,
			}, {
				desc: NewDesc(
					memstatNamespace("sys_bytes"),
					"Number of bytes obtained from system.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.Sys) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("lookups_total"),
					"Total number of pointer lookups.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.Lookups) },
				valType: CounterValue,
			}, {
				desc: NewDesc(
					memstatNamespace("mallocs_total"),
					"Total number of mallocs.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.Mallocs) },
				valType: CounterValue,
			}, {
				desc: NewDesc(
					memstatNamespace("frees_total"),
					"Total number of frees.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.Frees) },
				valType: CounterValue,
			}, {
				desc: NewDesc(
					memstatNamespace("heap_alloc_bytes"),
					"Number of heap bytes allocated and still in use.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.HeapAlloc) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("heap_sys_bytes"),
					"Number of heap bytes obtained from system.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.HeapSys) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("heap_idle_bytes"),
					"Number of heap bytes waiting to be used.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.HeapIdle) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("heap_inuse_bytes"),
					"Number of heap bytes that are in use.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.HeapInuse) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("heap_released_bytes"),
					"Number of heap bytes released to OS.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.HeapReleased) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("heap_objects"),
					"Number of allocated objects.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.HeapObjects) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("stack_inuse_bytes"),
					"Number of bytes in use by the stack allocator.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.StackInuse) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("stack_sys_bytes"),
					"Number of bytes obtained from system for stack allocator.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.StackSys) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("mspan_inuse_bytes"),
					"Number of bytes in use by mspan structures.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.MSpanInuse) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("mspan_sys_bytes"),
					"Number of bytes used for mspan structures obtained from system.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.MSpanSys) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("mcache_inuse_bytes"),
					"Number of bytes in use by mcache structures.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.MCacheInuse) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("mcache_sys_bytes"),
					"Number of bytes used for mcache structures obtained from system.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.MCacheSys) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("buck_hash_sys_bytes"),
					"Number of bytes used by the profiling bucket hash table.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.BuckHashSys) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("gc_sys_bytes"),
					"Number of bytes used for garbage collection system metadata.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.GCSys) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("other_sys_bytes"),
					"Number of bytes used for other system allocations.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.OtherSys) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("next_gc_bytes"),
					"Number of heap bytes when next garbage collection will take place.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.NextGC) },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("last_gc_time_seconds"),
					"Number of seconds since 1970 of last garbage collection.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return float64(ms.LastGC) / 1e9 },
				valType: GaugeValue,
			}, {
				desc: NewDesc(
					memstatNamespace("gc_cpu_fraction"),
					"The fraction of this program's available CPU time used by the GC since the program started.",
					nil, nil,
				),
				eval:    func(ms *runtime.MemStats) float64 { return ms.GCCPUFraction },
				valType: GaugeValue,
			},
		},
	}
}
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 3.3748e-05
go_gc_duration_seconds{quantile="0.25"} 5.2645e-05
go_gc_duration_seconds{quantile="0.5"} 7.6294e-05
go_gc_duration_seconds{quantile="0.75"} 0.000134934
go_gc_duration_seconds{quantile="1"} 0.034202112
go_gc_duration_seconds_sum 1.819829603
go_gc_duration_seconds_count 4587
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 147
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.14.6"} 1
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
go_memstats_alloc_bytes 2.0952968e+07
# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
# TYPE go_memstats_alloc_bytes_total counter
go_memstats_alloc_bytes_total 4.6497514336e+10
# HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table.
# TYPE go_memstats_buck_hash_sys_bytes gauge
go_memstats_buck_hash_sys_bytes 4.33568e+06
# HELP go_memstats_frees_total Total number of frees.
# TYPE go_memstats_frees_total counter
go_memstats_frees_total 5.27104449e+08
# HELP go_memstats_gc_cpu_fraction The fraction of this program's available CPU time used by the GC since the program started.
# TYPE go_memstats_gc_cpu_fraction gauge
go_memstats_gc_cpu_fraction 9.56712916477841e-06
# HELP go_memstats_gc_sys_bytes Number of bytes used for garbage collection system metadata.
# TYPE go_memstats_gc_sys_bytes gauge
go_memstats_gc_sys_bytes 3.61908e+06
# HELP go_memstats_heap_alloc_bytes Number of heap bytes allocated and still in use.
# TYPE go_memstats_heap_alloc_bytes gauge
go_memstats_heap_alloc_bytes 2.0952968e+07
# HELP go_memstats_heap_idle_bytes Number of heap bytes waiting to be used.
# TYPE go_memstats_heap_idle_bytes gauge
go_memstats_heap_idle_bytes 3.895296e+07
# HELP go_memstats_heap_inuse_bytes Number of heap bytes that are in use.
# TYPE go_memstats_heap_inuse_bytes gauge
go_memstats_heap_inuse_bytes 2.4911872e+07
# HELP go_memstats_heap_objects Number of allocated objects.
# TYPE go_memstats_heap_objects gauge
go_memstats_heap_objects 109025
# HELP go_memstats_heap_released_bytes Number of heap bytes released to OS.
# TYPE go_memstats_heap_released_bytes gauge
go_memstats_heap_released_bytes 2.8131328e+07
# HELP go_memstats_heap_sys_bytes Number of heap bytes obtained from system.
# TYPE go_memstats_heap_sys_bytes gauge
go_memstats_heap_sys_bytes 6.3864832e+07
# HELP go_memstats_last_gc_time_seconds Number of seconds since 1970 of last garbage collection.
# TYPE go_memstats_last_gc_time_seconds gauge
go_memstats_last_gc_time_seconds 1.6266241951728766e+09
# HELP go_memstats_lookups_total Total number of pointer lookups.
# TYPE go_memstats_lookups_total counter
go_memstats_lookups_total 0
# HELP go_memstats_mallocs_total Total number of mallocs.
# TYPE go_memstats_mallocs_total counter
go_memstats_mallocs_total 5.27213474e+08
# HELP go_memstats_mcache_inuse_bytes Number of bytes in use by mcache structures.
# TYPE go_memstats_mcache_inuse_bytes gauge
go_memstats_mcache_inuse_bytes 83328
# HELP go_memstats_mcache_sys_bytes Number of bytes used for mcache structures obtained from system.
# TYPE go_memstats_mcache_sys_bytes gauge
go_memstats_mcache_sys_bytes 98304
# HELP go_memstats_mspan_inuse_bytes Number of bytes in use by mspan structures.
# TYPE go_memstats_mspan_inuse_bytes gauge
go_memstats_mspan_inuse_bytes 562496
# HELP go_memstats_mspan_sys_bytes Number of bytes used for mspan structures obtained from system.
# TYPE go_memstats_mspan_sys_bytes gauge
go_memstats_mspan_sys_bytes 704512
# HELP go_memstats_next_gc_bytes Number of heap bytes when next garbage collection will take place.
# TYPE go_memstats_next_gc_bytes gauge
go_memstats_next_gc_bytes 3.1846608e+07
# HELP go_memstats_other_sys_bytes Number of bytes used for other system allocations.
# TYPE go_memstats_other_sys_bytes gauge
go_memstats_other_sys_bytes 5.74252e+06
# HELP go_memstats_stack_inuse_bytes Number of bytes in use by the stack allocator.
# TYPE go_memstats_stack_inuse_bytes gauge
go_memstats_stack_inuse_bytes 3.244032e+06
# HELP go_memstats_stack_sys_bytes Number of bytes obtained from system for stack allocator.
# TYPE go_memstats_stack_sys_bytes gauge
go_memstats_stack_sys_bytes 3.244032e+06
# HELP go_memstats_sys_bytes Number of bytes obtained from system.
# TYPE go_memstats_sys_bytes gauge
go_memstats_sys_bytes 8.160896e+07
# HELP go_threads Number of OS threads created.
# TYPE go_threads gauge
go_threads 54
# HELP my_appgo_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE my_appgo_gc_duration_seconds summary
my_appgo_gc_duration_seconds{service_name="app1",quantile="0"} 3.3748e-05
my_appgo_gc_duration_seconds{service_name="app1",quantile="0.25"} 5.2645e-05
my_appgo_gc_duration_seconds{service_name="app1",quantile="0.5"} 7.6294e-05
my_appgo_gc_duration_seconds{service_name="app1",quantile="0.75"} 0.000134934
my_appgo_gc_duration_seconds{service_name="app1",quantile="1"} 0.034202112
my_appgo_gc_duration_seconds_sum{service_name="app1"} 1.819829603
my_appgo_gc_duration_seconds_count{service_name="app1"} 4587
# HELP my_appgo_goroutines Number of goroutines that currently exist.
# TYPE my_appgo_goroutines gauge
my_appgo_goroutines{service_name="app1"} 149
# HELP my_appgo_info Information about the Go environment.
# TYPE my_appgo_info gauge
my_appgo_info{service_name="app1",version="go1.14.6"} 1
# HELP my_appgo_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE my_appgo_memstats_alloc_bytes gauge
my_appgo_memstats_alloc_bytes{service_name="app1"} 2.0872168e+07
# HELP my_appgo_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
# TYPE my_appgo_memstats_alloc_bytes_total counter
my_appgo_memstats_alloc_bytes_total{service_name="app1"} 4.6497433536e+10
# HELP my_appgo_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table.
# TYPE my_appgo_memstats_buck_hash_sys_bytes gauge
my_appgo_memstats_buck_hash_sys_bytes{service_name="app1"} 4.33568e+06
# HELP my_appgo_memstats_frees_total Total number of frees.
# TYPE my_appgo_memstats_frees_total counter
my_appgo_memstats_frees_total{service_name="app1"} 5.27104026e+08
# HELP my_appgo_memstats_gc_cpu_fraction The fraction of this program's available CPU time used by the GC since the program started.
# TYPE my_appgo_memstats_gc_cpu_fraction gauge
my_appgo_memstats_gc_cpu_fraction{service_name="app1"} 9.56712916477841e-06
# HELP my_appgo_memstats_gc_sys_bytes Number of bytes used for garbage collection system metadata.
# TYPE my_appgo_memstats_gc_sys_bytes gauge
my_appgo_memstats_gc_sys_bytes{service_name="app1"} 3.61908e+06
# HELP my_appgo_memstats_heap_alloc_bytes Number of heap bytes allocated and still in use.
# TYPE my_appgo_memstats_heap_alloc_bytes gauge
my_appgo_memstats_heap_alloc_bytes{service_name="app1"} 2.0872168e+07
# HELP my_appgo_memstats_heap_idle_bytes Number of heap bytes waiting to be used.
# TYPE my_appgo_memstats_heap_idle_bytes gauge
my_appgo_memstats_heap_idle_bytes{service_name="app1"} 3.9002112e+07
# HELP my_appgo_memstats_heap_inuse_bytes Number of heap bytes that are in use.
# TYPE my_appgo_memstats_heap_inuse_bytes gauge
my_appgo_memstats_heap_inuse_bytes{service_name="app1"} 2.486272e+07
# HELP my_appgo_memstats_heap_objects Number of allocated objects.
# TYPE my_appgo_memstats_heap_objects gauge
my_appgo_memstats_heap_objects{service_name="app1"} 107864
# HELP my_appgo_memstats_heap_released_bytes Number of heap bytes released to OS.
# TYPE my_appgo_memstats_heap_released_bytes gauge
my_appgo_memstats_heap_released_bytes{service_name="app1"} 2.8131328e+07
# HELP my_appgo_memstats_heap_sys_bytes Number of heap bytes obtained from system.
# TYPE my_appgo_memstats_heap_sys_bytes gauge
my_appgo_memstats_heap_sys_bytes{service_name="app1"} 6.3864832e+07
# HELP my_appgo_memstats_last_gc_time_seconds Number of seconds since 1970 of last garbage collection.
# TYPE my_appgo_memstats_last_gc_time_seconds gauge
my_appgo_memstats_last_gc_time_seconds{service_name="app1"} 1.6266241951728766e+09
# HELP my_appgo_memstats_lookups_total Total number of pointer lookups.
# TYPE my_appgo_memstats_lookups_total counter
my_appgo_memstats_lookups_total{service_name="app1"} 0
# HELP my_appgo_memstats_mallocs_total Total number of mallocs.
# TYPE my_appgo_memstats_mallocs_total counter
my_appgo_memstats_mallocs_total{service_name="app1"} 5.2721189e+08
# HELP my_appgo_memstats_mcache_inuse_bytes Number of bytes in use by mcache structures.
# TYPE my_appgo_memstats_mcache_inuse_bytes gauge
my_appgo_memstats_mcache_inuse_bytes{service_name="app1"} 83328
# HELP my_appgo_memstats_mcache_sys_bytes Number of bytes used for mcache structures obtained from system.
# TYPE my_appgo_memstats_mcache_sys_bytes gauge
my_appgo_memstats_mcache_sys_bytes{service_name="app1"} 98304
# HELP my_appgo_memstats_mspan_inuse_bytes Number of bytes in use by mspan structures.
# TYPE my_appgo_memstats_mspan_inuse_bytes gauge
my_appgo_memstats_mspan_inuse_bytes{service_name="app1"} 553792
# HELP my_appgo_memstats_mspan_sys_bytes Number of bytes used for mspan structures obtained from system.
# TYPE my_appgo_memstats_mspan_sys_bytes gauge
my_appgo_memstats_mspan_sys_bytes{service_name="app1"} 704512
# HELP my_appgo_memstats_next_gc_bytes Number of heap bytes when next garbage collection will take place.
# TYPE my_appgo_memstats_next_gc_bytes gauge
my_appgo_memstats_next_gc_bytes{service_name="app1"} 3.1846608e+07
# HELP my_appgo_memstats_other_sys_bytes Number of bytes used for other system allocations.
# TYPE my_appgo_memstats_other_sys_bytes gauge
my_appgo_memstats_other_sys_bytes{service_name="app1"} 5.74252e+06
# HELP my_appgo_memstats_stack_inuse_bytes Number of bytes in use by the stack allocator.
# TYPE my_appgo_memstats_stack_inuse_bytes gauge
my_appgo_memstats_stack_inuse_bytes{service_name="app1"} 3.244032e+06
# HELP my_appgo_memstats_stack_sys_bytes Number of bytes obtained from system for stack allocator.
# TYPE my_appgo_memstats_stack_sys_bytes gauge
my_appgo_memstats_stack_sys_bytes{service_name="app1"} 3.244032e+06
# HELP my_appgo_memstats_sys_bytes Number of bytes obtained from system.
# TYPE my_appgo_memstats_sys_bytes gauge
my_appgo_memstats_sys_bytes{service_name="app1"} 8.160896e+07
# HELP my_appgo_threads Number of OS threads created.
# TYPE my_appgo_threads gauge
my_appgo_threads{service_name="app1"} 54

Reference


TOC