Version: v0.0.0-...-e6da185 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2021 License: MIT Imports: 3 Imported by: 0



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, such as how to display it (e.g. gauge vs. counter) and how difficult or disruptive it is to obtain it (e.g. do you need to stop the world?).

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 encouranged 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.

Supported metrics

	Count of completed GC cycles generated by the Go runtime.

	Count of completed forced GC cycles.

	Count of all completed GC cycles.

	Distribution of all objects allocated by approximate size.

	Distribution of all objects freed by approximate size.

	Heap size target for the end of the GC cycle.

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

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

	Memory that is available for allocation, and may be returned
	to the underlying system.

	Memory occupied by live objects and dead objects that have
	not yet been collected.

	Memory that has been returned to the underlying system.

	Memory allocated from the heap that is occupied by stacks.

	Memory that is unavailable for allocation, but cannot be
	returned to the underlying system.

	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.



This section is empty.


This section is empty.


func Read

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.

Metric values with names not appearing in the value returned by Descriptions will have the value populated as KindBad to indicate that the name is unknown.


type Description

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".
	// 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

	// StopTheWorld is whether or not the metric requires a stop-the-world
	// event in order to collect it.
	StopTheWorld bool

Description describes a runtime metric.

func All

func All() []Description

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

type Float64Histogram

type Float64Histogram struct {
	// Counts contains the weights for each histogram bucket. The length of
	// Counts is equal to the length of Buckets (in the metric description)
	// plus one to account for the implicit minimum bucket.
	// Given N buckets, the following is the mathematical relationship between
	// Counts and Buckets.
	// count[0] is the weight of the range (-inf, bucket[0])
	// count[n] is the weight of the range [bucket[n], bucket[n+1]), for 0 < n < N-1
	// count[N-1] is the weight of the range [bucket[N-1], inf)
	Counts []uint64

	// Buckets contains the boundaries between histogram buckets, in increasing order.
	// Because this slice contains boundaries, there are len(Buckets)+1 counts:
	// a count for all values less than the first boundary, a count covering each
	// [slice[i], slice[i+1]) interval, and a count for all values greater than or
	// equal to the last boundary.
	// For a given metric name, the value of Buckets is guaranteed not to change
	// between calls until program exit.
	Buckets []float64

Float64Histogram represents a distribution of float64 values.

type 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 Descriptions.
	Name string

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

Sample captures a single metric sample.

type Value

type Value struct {
	// contains filtered or unexported fields

Value represents a metric value returned by the runtime.

func (Value) Float64

func (v Value) Float64() float64

Float64 returns the internal float64 value for the metric.

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

func (Value) Float64Histogram

func (v Value) Float64Histogram() *Float64Histogram

Float64Histogram returns the internal *Float64Histogram value for the metric.

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

func (Value) Kind

func (v Value) Kind() ValueKind

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

func (Value) Uint64

func (v Value) Uint64() uint64

Uint64 returns the internal uint64 value for the metric.

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

type ValueKind

type ValueKind int

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

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL