package metrics
import "tailscale.com/metrics"
Package metrics contains expvar & Prometheus types and code used by Tailscale for monitoring.
Index ¶
- func CurrentFDs() int
- func LabelString(k any) string
- type Histogram
- func NewHistogram(buckets []float64) *Histogram
- func (h *Histogram) Do(f func(expvar.KeyValue))
- func (h *Histogram) Observe(v float64)
- func (h *Histogram) PromExport(w io.Writer, name string)
- func (h *Histogram) String() string
- type KeyValue
- type LabelMap
- func (m *LabelMap) Add(key string, delta int64)
- func (m *LabelMap) Get(key string) *expvar.Int
- func (m *LabelMap) GetFloat(key string) *expvar.Float
- func (m *LabelMap) GetIncrFunc(key string) func(delta int64)
- func (m *LabelMap) GetShardedInt(key string) *syncs.ShardedInt
- func (m *LabelMap) SetInt64(key string, v int64)
- type MultiLabelMap
- func NewMultiLabelMap[T comparable](name string, promType, helpText string) *MultiLabelMap[T]
- func (v *MultiLabelMap[T]) Add(key T, delta int64)
- func (v *MultiLabelMap[T]) AddFloat(key T, delta float64)
- func (v *MultiLabelMap[T]) Delete(key T)
- func (v *MultiLabelMap[T]) Do(f func(KeyValue[T]))
- func (v *MultiLabelMap[T]) Get(key T) expvar.Var
- func (v *MultiLabelMap[T]) Init() *MultiLabelMap[T]
- func (v *MultiLabelMap[T]) ResetAllForTest()
- func (v *MultiLabelMap[T]) Set(key T, val expvar.Var)
- func (v *MultiLabelMap[T]) SetFloat(key T, val float64)
- func (v *MultiLabelMap[T]) SetInt(key T, val int64)
- func (v *MultiLabelMap[T]) String() string
- func (v *MultiLabelMap[T]) WritePrometheus(w io.Writer, name string)
- type Set
Functions ¶
func CurrentFDs ¶
func CurrentFDs() int
CurrentFDs reports how many file descriptors are currently open.
It only works on Linux. It returns zero otherwise.
func LabelString ¶
LabelString returns a Prometheus-formatted label string for the given key. k must be a struct type with scalar fields, as required by MultiLabelMap, if k is not a struct, it will panic.
Types ¶
type Histogram ¶
type Histogram struct {
// contains filtered or unexported fields
}
Histogram is a histogram of values. It should be created with NewHistogram.
func NewHistogram ¶
NewHistogram returns a new histogram that reports to the given expvar map under the given name.
The buckets are the boundaries of the histogram buckets, in increasing order. The last bucket is +Inf.
func (*Histogram) Do ¶
Do calls f for each bucket in the histogram.
func (*Histogram) Observe ¶
Observe records a new observation in the histogram.
func (*Histogram) PromExport ¶
PromExport writes the histogram to w in Prometheus exposition format.
func (*Histogram) String ¶
String returns a JSON representation of the histogram. This is used to satisfy the expvar.Var interface.
type KeyValue ¶
type KeyValue[T comparable] struct { Key T Value expvar.Var }
KeyValue represents a single entry in a MultiLabelMap.
type LabelMap ¶
LabelMap is a string-to-Var map variable that satisfies the expvar.Var interface.
Semantically, this is mapped by tsweb's Prometheus exporter as a collection of variables with the same name, with a varying label value. Use this to export things that are intuitively breakdowns into different buckets.
func (*LabelMap) Add ¶
Add adds delta to the any int-like value stored under the given map key.
func (*LabelMap) Get ¶
Get returns a direct pointer to the expvar.Int for key, creating it if necessary.
func (*LabelMap) GetFloat ¶
GetFloat returns a direct pointer to the expvar.Float for key, creating it if necessary.
func (*LabelMap) GetIncrFunc ¶
GetIncrFunc returns a function that increments the expvar.Int named by key.
Most callers should not need this; it exists to satisfy an interface elsewhere.
func (*LabelMap) GetShardedInt ¶
func (m *LabelMap) GetShardedInt(key string) *syncs.ShardedInt
GetShardedInt returns a direct pointer to the syncs.ShardedInt for key, creating it if necessary.
func (*LabelMap) SetInt64 ¶
SetInt64 sets the *Int value stored under the given map key.
type MultiLabelMap ¶
type MultiLabelMap[T comparable] struct { Type string // optional Prometheus type ("counter", "gauge") Help string // optional Prometheus help string // contains filtered or unexported fields }
MultiLabelMap is a struct-value-to-Var map variable that satisfies the expvar.Var interface but also allows for multiple Prometheus labels to be associated with each value.
T must be a struct type with scalar fields. The struct field names (lowercased) are used as the labels, unless a "prom" struct tag is present. The struct fields must all be strings, and the string values must be valid Prometheus label values without requiring quoting.
func NewMultiLabelMap ¶
func NewMultiLabelMap[T comparable](name string, promType, helpText string) *MultiLabelMap[T]
NewMultiLabelMap creates and publishes (via expvar.Publish) a new MultiLabelMap[T] variable with the given name and returns it.
func (*MultiLabelMap[T]) Add ¶
func (v *MultiLabelMap[T]) Add(key T, delta int64)
Add adds delta to the *expvar.Int value stored under the given map key, creating it if it doesn't exist yet. It does nothing if key exists but is of the wrong type.
func (*MultiLabelMap[T]) AddFloat ¶
func (v *MultiLabelMap[T]) AddFloat(key T, delta float64)
Add adds delta to the *expvar.Float value stored under the given map key, creating it if it doesn't exist yet. It does nothing if key exists but is of the wrong type.
func (*MultiLabelMap[T]) Delete ¶
func (v *MultiLabelMap[T]) Delete(key T)
Delete deletes the given key from the map.
This is not optimized for highly concurrent usage; it's presumed to only be used rarely, at startup.
func (*MultiLabelMap[T]) Do ¶
func (v *MultiLabelMap[T]) Do(f func(KeyValue[T]))
Do calls f for each entry in the map. The map is locked during the iteration, but existing entries may be concurrently updated.
func (*MultiLabelMap[T]) Get ¶
func (v *MultiLabelMap[T]) Get(key T) expvar.Var
Get returns the expvar for the given key, or nil if it doesn't exist.
func (*MultiLabelMap[T]) Init ¶
func (v *MultiLabelMap[T]) Init() *MultiLabelMap[T]
Init removes all keys from the map.
Think of it as "Reset", but it's named Init to match expvar.Map.Init.
func (*MultiLabelMap[T]) ResetAllForTest ¶
func (v *MultiLabelMap[T]) ResetAllForTest()
ResetAllForTest resets all values for metrics to zero. Should only be used in tests.
func (*MultiLabelMap[T]) Set ¶
func (v *MultiLabelMap[T]) Set(key T, val expvar.Var)
Set sets key to val.
This is not optimized for highly concurrent usage; it's presumed to only be used rarely, at startup.
func (*MultiLabelMap[T]) SetFloat ¶
func (v *MultiLabelMap[T]) SetFloat(key T, val float64)
SetFloat sets val to the *expvar.Float value stored under the given map key, creating it if it doesn't exist yet. It does nothing if key exists but is of the wrong type.
func (*MultiLabelMap[T]) SetInt ¶
func (v *MultiLabelMap[T]) SetInt(key T, val int64)
SetInt sets val to the *expvar.Int value stored under the given map key, creating it if it doesn't exist yet. It does nothing if key exists but is of the wrong type.
func (*MultiLabelMap[T]) String ¶
func (v *MultiLabelMap[T]) String() string
func (*MultiLabelMap[T]) WritePrometheus ¶
func (v *MultiLabelMap[T]) WritePrometheus(w io.Writer, name string)
WritePrometheus writes v to w in Prometheus exposition format. The name argument is the metric name.
type Set ¶
Set is a string-to-Var map variable that satisfies the expvar.Var interface.
Semantically, this is mapped by tsweb's Prometheus exporter as a collection of unrelated variables exported with a common prefix.
This lets us have tsweb recognize *expvar.Map for different purposes in the future. (Or perhaps all uses of expvar.Map will require explicit types like this one, declaring how we want tsweb to export it to Prometheus.)
Source Files ¶
fds_linux.go metrics.go multilabelmap.go
- Version
- v1.84.0 (latest)
- Published
- May 21, 2025
- Platform
- linux/amd64
- Imports
- 12 packages
- Last checked
- 1 day ago –
Tools for package owners.