Package metrics

import "runtime/metrics"

Overview ▾

Package metrics provides a stable interface to access implementation-defined metrics exported by the Go runtime. This package is similar to existing functions like runtime.ReadMemStats and debug.ReadGCStats, but significantly more general.

The set of metrics defined by this package may evolve as the runtime itself evolves, and also enables variation across Go implementations, whose relevant metric sets may not intersect.


Metrics are designated by a string key, rather than, for example, a field name in a struct. The full list of supported metrics is always available in the slice of Descriptions returned by All. Each Description also includes useful information about the metric.

Thus, users of this API are encouraged to sample supported metrics defined by the slice returned by All to remain compatible across Go versions. Of course, situations arise where reading specific metrics is critical. For these cases, users are encouraged to use build tags, and although metrics may be deprecated and removed, users should consider this to be an exceptional and rare event, coinciding with a very large change in a particular Go implementation.

Each metric key also has a "kind" that describes the format of the metric's value. In the interest of not breaking users of this package, the "kind" for a given metric is guaranteed not to change. If it must change, then a new metric will be introduced with a new key and a new "kind."

Metric key format

As mentioned earlier, metric keys are strings. Their format is simple and well-defined, designed to be both human and machine readable. It is split into two components, separated by a colon: a rooted path and a unit. The choice to include the unit in the key is motivated by compatibility: if a metric's unit changes, its semantics likely did also, and a new key should be introduced.

For more details on the precise definition of the metric key's path and unit formats, see the documentation of the Name field of the Description struct.

A note about floats

This package supports metrics whose values have a floating-point representation. In order to improve ease-of-use, this package promises to never produce the following classes of floating-point values: NaN, infinity.

Supported metrics

Below is the full list of supported metrics, ordered lexicographically.

	Count of completed GC cycles generated by the Go runtime.

	Count of completed GC cycles forced by the application.

	Count of all completed GC cycles.

	Distribution of heap allocations by approximate size.
	Note that this does not include tiny objects as defined by /gc/heap/tiny/allocs:objects,
	only tiny blocks.

	Cumulative sum of memory allocated to the heap by the application.

	Cumulative count of heap allocations triggered by the application.
	Note that this does not include tiny objects as defined by /gc/heap/tiny/allocs:objects,
	only tiny blocks.

	Distribution of freed heap allocations by approximate size.
	Note that this does not include tiny objects as defined by /gc/heap/tiny/allocs:objects,
	only tiny blocks.

	Cumulative sum of heap memory freed by the garbage collector.

	Cumulative count of heap allocations whose storage was freed by the garbage collector.
	Note that this does not include tiny objects as defined by /gc/heap/tiny/allocs:objects,
	only tiny blocks.

	Heap size target for the end of the GC cycle.

	Number of objects, live or unswept, occupying heap memory.

	Count of small allocations that are packed together into blocks.
	These allocations are counted separately from other allocations
	because each individual allocation is not tracked by the runtime,
	only their block. Each block is already accounted for in
	allocs-by-size and frees-by-size.

	Distribution individual GC-related stop-the-world pause latencies.

	Memory that is completely free and eligible to be returned to
	the underlying system, but has not been. This metric is the
	runtime's estimate of free address space that is backed by
	physical memory.

	Memory occupied by live objects and dead objects that have
	not yet been marked free by the garbage collector.

	Memory that is completely free and has been returned to
	the underlying system. This metric is the runtime's estimate of
	free address space that is still mapped into the process, but
	is not backed by physical memory.

	Memory allocated from the heap that is reserved for stack
	space, whether or not it is currently in-use.

	Memory that is reserved for heap objects but is not currently
	used to hold heap objects.

	Memory that is reserved for runtime mcache structures, but
	not in-use.

	Memory that is occupied by runtime mcache structures that
	are currently being used.

	Memory that is reserved for runtime mspan structures, but
	not in-use.

	Memory that is occupied by runtime mspan structures that are
	currently being used.

	Memory that is reserved for or used to hold runtime

	Stack memory allocated by the underlying operating system.

	Memory used by execution trace buffers, structures for
	debugging the runtime, finalizer and profiler specials, and

	Memory that is used by the stack trace hash map used for

	All memory mapped by the Go runtime into the current process
	as read-write. Note that this does not include memory mapped
	by code called via cgo or via the syscall package.
	Sum of all metrics in /memory/classes.

	Count of live goroutines.

	Distribution of the time goroutines have spent in the scheduler
	in a runnable state before actually running.

func Read 1.16

func Read(m []Sample)

Read populates each Value field in the given slice of metric samples.

Desired metrics should be present in the slice with the appropriate name. The user of this API is encouraged to re-use the same slice between calls for efficiency, but is not required to do so.

Note that re-use has some caveats. Notably, Values should not be read or manipulated while a Read with that value is outstanding; that is a data race. This property includes pointer-typed Values (for example, Float64Histogram) whose underlying storage will be reused by Read when possible. To safely use such values in a concurrent setting, all data must be deep-copied.

It is safe to execute multiple Read calls concurrently, but their arguments must share no underlying memory. When in doubt, create a new []Sample from scratch, which is always safe, though may be inefficient.

Sample values with names not appearing in All will have their Value populated as KindBad to indicate that the name is unknown.

Example (ReadingAllMetrics)


// Get descriptions for all supported metrics.
descs := metrics.All()

// Create a sample for each metric.
samples := make([]metrics.Sample, len(descs))
for i := range samples {
		samples[i].Name = descs[i].Name

// Sample the metrics. Re-use the samples slice if you can!

// Iterate over all results.
for _, sample := range samples {
		// Pull out the name and value.
		name, value := sample.Name, sample.Value

		// Handle each sample.
		switch value.Kind() {
		case metrics.KindUint64:
				fmt.Printf("%s: %d\n", name, value.Uint64())
		case metrics.KindFloat64:
				fmt.Printf("%s: %f\n", name, value.Float64())
		case metrics.KindFloat64Histogram:
				// The histogram may be quite large, so let's just pull out
				// a crude estimate for the median for the sake of this example.
				fmt.Printf("%s: %f\n", name, medianBucket(value.Float64Histogram()))
		case metrics.KindBad:
				// This should never happen because all metrics are supported
				// by construction.
				panic("bug in runtime/metrics package!")
				// This may happen as new metrics get added.
				// The safest thing to do here is to simply log it somewhere
				// as something to look into, but ignore it for now.
				// In the worst case, you might temporarily miss out on a new metric.
				fmt.Printf("%s: unexpected metric Kind: %v\n", name, value.Kind())
Example (ReadingOneMetric)


// Name of the metric we want to read.
const myMetric = "/memory/classes/heap/free:bytes"

// Create a sample for the metric.
sample := make([]metrics.Sample, 1)
sample[0].Name = myMetric

// Sample the metric.

// Check if the metric is actually supported.
// If it's not, the resulting value will always have
// kind KindBad.
if sample[0].Value.Kind() == metrics.KindBad {
		panic(fmt.Sprintf("metric %q no longer supported", myMetric))

// Handle the result.
// It's OK to assume a particular Kind for a metric;
// they're guaranteed not to change.
freeBytes := sample[0].Value.Uint64()

fmt.Printf("free but not released memory: %d\n", freeBytes)

type Description 1.16

Description describes a runtime metric.

type Description struct {
		// Name is the full name of the metric which includes the unit.
		// The format of the metric may be described by the following regular expression.
		// 	^(?P<name>/[^:]+):(?P<unit>[^:*/]+(?:[*/][^:*/]+)*)$
		// The format splits the name into two components, separated by a colon: a path which always
		// starts with a /, and a machine-parseable unit. The name may contain any valid Unicode
		// codepoint in between / characters, but by convention will try to stick to lowercase
		// characters and hyphens. An example of such a path might be "/memory/heap/free".
		// The unit is by convention a series of lowercase English unit names (singular or plural)
		// without prefixes delimited by '*' or '/'. The unit names may contain any valid Unicode
		// codepoint that is not a delimiter.
		// Examples of units might be "seconds", "bytes", "bytes/second", "cpu-seconds",
		// "byte*cpu-seconds", and "bytes/second/second".
		// For histograms, multiple units may apply. For instance, the units of the buckets and
		// the count. By convention, for histograms, the units of the count are always "samples"
		// with the type of sample evident by the metric's name, while the unit in the name
		// specifies the buckets' unit.
		// A complete name might look like "/memory/heap/free:bytes".
		Name string

		// Description is an English language sentence describing the metric.
		Description string

		// Kind is the kind of value for this metric.
		// The purpose of this field is to allow users to filter out metrics whose values are
		// types which their application may not understand.
		Kind ValueKind

		// Cumulative is whether or not the metric is cumulative. If a cumulative metric is just
		// a single number, then it increases monotonically. If the metric is a distribution,
		// then each bucket count increases monotonically.
		// This flag thus indicates whether or not it's useful to compute a rate from this value.
		Cumulative bool

func All 1.16

func All() []Description

All returns a slice of containing metric descriptions for all supported metrics.

type Float64Histogram 1.16

Float64Histogram represents a distribution of float64 values.

type Float64Histogram struct {
		// Counts contains the weights for each histogram bucket.
		// Given N buckets, Count[n] is the weight of the range
		// [bucket[n], bucket[n+1]), for 0 <= n < N.
		Counts []uint64

		// Buckets contains the boundaries of the histogram buckets, in increasing order.
		// Buckets[0] is the inclusive lower bound of the minimum bucket while
		// Buckets[len(Buckets)-1] is the exclusive upper bound of the maximum bucket.
		// Hence, there are len(Buckets)-1 counts. Furthermore, len(Buckets) != 1, always,
		// since at least two boundaries are required to describe one bucket (and 0
		// boundaries are used to describe 0 buckets).
		// Buckets[0] is permitted to have value -Inf and Buckets[len(Buckets)-1] is
		// permitted to have value Inf.
		// For a given metric name, the value of Buckets is guaranteed not to change
		// between calls until program exit.
		// This slice value is permitted to alias with other Float64Histograms' Buckets
		// fields, so the values within should only ever be read. If they need to be
		// modified, the user must make a copy.
		Buckets []float64

type Sample 1.16

Sample captures a single metric sample.

type Sample struct {
		// Name is the name of the metric sampled.
		// It must correspond to a name in one of the metric descriptions
		// returned by All.
		Name string

		// Value is the value of the metric sample.
		Value Value

type Value 1.16

Value represents a metric value returned by the runtime.

type Value struct {
		// contains filtered or unexported fields

func (Value) Float64 1.16

func (v Value) Float64() float64

Float64 returns the internal float64 value for the metric.

If v.Kind() != KindFloat64, this method panics.

func (Value) Float64Histogram 1.16

func (v Value) Float64Histogram() *Float64Histogram

Float64Histogram returns the internal *Float64Histogram value for the metric.

If v.Kind() != KindFloat64Histogram, this method panics.

func (Value) Kind 1.16

func (v Value) Kind() ValueKind

Kind returns the tag representing the kind of value this is.

func (Value) Uint64 1.16

func (v Value) Uint64() uint64

Uint64 returns the internal uint64 value for the metric.

If v.Kind() != KindUint64, this method panics.

type ValueKind 1.16

ValueKind is a tag for a metric Value which indicates its type.

type ValueKind int
const (
		// KindBad indicates that the Value has no type and should not be used.
		KindBad ValueKind = iota

		// KindUint64 indicates that the type of the Value is a uint64.

		// KindFloat64 indicates that the type of the Value is a float64.

		// KindFloat64Histogram indicates that the type of the Value is a *Float64Histogram.