metrics

package
v0.0.0-...-088ea19 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: GPL-3.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PrometheusFormatRequirement = "^" + prometheusBaseFormt + "$"
)

PrometheusFormatRequirement is required format defined by prometheus for metric and label names.

Variables

View Source
var (
	CfgOptionInstanceKey = "core/metrics/instance"

	CfgOptionCommentKey = "core/metrics/comment"

	CfgOptionPushKey = "core/metrics/push"
)

Configuration Keys.

View Source
var (

	// ErrAlreadyStarted is returned when an operation is only valid before the
	// first metric is registered, and is called after.
	ErrAlreadyStarted = errors.New("can only be changed before first metric is registered")

	// ErrAlreadyRegistered is returned when a metric with the same ID is
	// registered again.
	ErrAlreadyRegistered = errors.New("metric already registered")

	// ErrAlreadySet is returned when a value is already set and cannot be changed.
	ErrAlreadySet = errors.New("already set")

	// ErrInvalidOptions is returned when invalid options where provided.
	ErrInvalidOptions = errors.New("invalid options")
)
View Source
var (

	// ErrAlreadyInitialized is returned when trying to initialize an option
	// more than once or if the time window for initializing is over.
	ErrAlreadyInitialized = errors.New("already initialized")
)

Functions

func AddGlobalLabel

func AddGlobalLabel(name, value string) error

AddGlobalLabel adds a global label to all metrics. Global labels must be added before any metric is registered. Does not affect golang runtime metrics.

func DiskFree

func DiskFree() (free uint64, ok bool)

DiskFree returns the available disk space (from the program's data root).

func DiskTotal

func DiskTotal() (total uint64, ok bool)

DiskTotal returns the total disk space (from the program's data root).

func DiskUsed

func DiskUsed() (used uint64, ok bool)

DiskUsed returns the used disk space (from the program's data root).

func DiskUsedPercent

func DiskUsedPercent() (usedPercent float64, ok bool)

DiskUsedPercent returns the percent of used disk space (from the program's data root).

func EnableMetricPersistence

func EnableMetricPersistence(key string) error

EnableMetricPersistence enables metric persistence for metrics that opted for it. They given key is the database key where the metric data will be persisted. This call also directly loads the stored data from the database. The returned error is only about loading the metrics, not about enabling persistence. May only be called once.

func ExportValues

func ExportValues(requestPermission api.Permission, internalOnly bool) map[string]any

ExportValues exports the values of all supported metrics.

func LoadAvg1

func LoadAvg1() (loadAvg float64, ok bool)

LoadAvg1 returns the 1-minute average system load.

func LoadAvg15

func LoadAvg15() (loadAvg float64, ok bool)

LoadAvg15 returns the 15-minute average system load.

func LoadAvg5

func LoadAvg5() (loadAvg float64, ok bool)

LoadAvg5 returns the 5-minute average system load.

func MemAvailable

func MemAvailable() (available uint64, ok bool)

MemAvailable returns the available system memory.

func MemTotal

func MemTotal() (total uint64, ok bool)

MemTotal returns the total system memory.

func MemUsed

func MemUsed() (used uint64, ok bool)

MemUsed returns the used system memory.

func MemUsedPercent

func MemUsedPercent() (usedPercent float64, ok bool)

MemUsedPercent returns the percent of used system memory.

func SetNamespace

func SetNamespace(namespace string) error

SetNamespace sets the namespace for all metrics. It is prefixed to all metric IDs. It must be set before any metric is registered. Does not affect golang runtime metrics.

func WriteMetrics

func WriteMetrics(w io.Writer, permission api.Permission, expertiseLevel config.ExpertiseLevel)

WriteMetrics writes all metrics that match the given permission and expertiseLevel to the given writer.

Types

type Counter

type Counter struct {
	*vm.Counter
	// contains filtered or unexported fields
}

Counter is a counter metric.

func NewCounter

func NewCounter(id string, labels map[string]string, opts *Options) (*Counter, error)

NewCounter registers a new counter metric.

func (*Counter) CurrentValue

func (c *Counter) CurrentValue() uint64

CurrentValue returns the current counter value.

func (Counter) ID

func (m Counter) ID() string

ID returns the given ID of the metric.

func (Counter) LabeledID

func (m Counter) LabeledID() string

LabeledID returns the Prometheus-compatible labeled ID of the metric.

func (Counter) Opts

func (m Counter) Opts() *Options

Opts returns the metric options. They may not be modified.

func (Counter) WritePrometheus

func (m Counter) WritePrometheus(w io.Writer)

WritePrometheus writes the metric in the prometheus format to the given writer.

type FetchingCounter

type FetchingCounter struct {
	// contains filtered or unexported fields
}

FetchingCounter is a counter metric that fetches the values via a function call.

func NewFetchingCounter

func NewFetchingCounter(id string, labels map[string]string, fn func() uint64, opts *Options) (*FetchingCounter, error)

NewFetchingCounter registers a new fetching counter metric.

func (*FetchingCounter) CurrentValue

func (fc *FetchingCounter) CurrentValue() uint64

CurrentValue returns the current counter value.

func (FetchingCounter) ID

func (m FetchingCounter) ID() string

ID returns the given ID of the metric.

func (FetchingCounter) LabeledID

func (m FetchingCounter) LabeledID() string

LabeledID returns the Prometheus-compatible labeled ID of the metric.

func (FetchingCounter) Opts

func (m FetchingCounter) Opts() *Options

Opts returns the metric options. They may not be modified.

func (*FetchingCounter) WritePrometheus

func (fc *FetchingCounter) WritePrometheus(w io.Writer)

WritePrometheus writes the metric in the prometheus format to the given writer.

type FloatMetric

type FloatMetric interface {
	CurrentValue() float64
}

FloatMetric is an interface for special functions of float metrics.

type Gauge

type Gauge struct {
	*vm.Gauge
	// contains filtered or unexported fields
}

Gauge is a gauge metric.

func NewGauge

func NewGauge(id string, labels map[string]string, fn func() float64, opts *Options) (*Gauge, error)

NewGauge registers a new gauge metric.

func (*Gauge) CurrentValue

func (g *Gauge) CurrentValue() float64

CurrentValue returns the current gauge value.

func (Gauge) ID

func (m Gauge) ID() string

ID returns the given ID of the metric.

func (Gauge) LabeledID

func (m Gauge) LabeledID() string

LabeledID returns the Prometheus-compatible labeled ID of the metric.

func (Gauge) Opts

func (m Gauge) Opts() *Options

Opts returns the metric options. They may not be modified.

func (Gauge) WritePrometheus

func (m Gauge) WritePrometheus(w io.Writer)

WritePrometheus writes the metric in the prometheus format to the given writer.

type Histogram

type Histogram struct {
	*vm.Histogram
	// contains filtered or unexported fields
}

Histogram is a histogram metric.

func NewHistogram

func NewHistogram(id string, labels map[string]string, opts *Options) (*Histogram, error)

NewHistogram registers a new histogram metric.

func (Histogram) ID

func (m Histogram) ID() string

ID returns the given ID of the metric.

func (Histogram) LabeledID

func (m Histogram) LabeledID() string

LabeledID returns the Prometheus-compatible labeled ID of the metric.

func (Histogram) Opts

func (m Histogram) Opts() *Options

Opts returns the metric options. They may not be modified.

func (Histogram) WritePrometheus

func (m Histogram) WritePrometheus(w io.Writer)

WritePrometheus writes the metric in the prometheus format to the given writer.

type Metric

type Metric interface {
	ID() string
	LabeledID() string
	Opts() *Options
	WritePrometheus(w io.Writer)
}

Metric represents one or more metrics.

type MetricExport

type MetricExport struct {
	Metric
	CurrentValue any
}

MetricExport is used to export a metric and its current value.

func ExportMetrics

func ExportMetrics(requestPermission api.Permission) []*MetricExport

ExportMetrics exports all registered metrics.

type Options

type Options struct {
	// Name defines an optional human readable name for the metric.
	Name string

	// InternalID specifies an alternative internal ID that will be used when
	// exposing the metric via the API in a structured format.
	InternalID string

	// AlertLimit defines an upper limit that triggers an alert.
	AlertLimit float64

	// AlertTimeframe defines an optional timeframe in seconds for which the
	// AlertLimit should be interpreted in.
	AlertTimeframe float64

	// Permission defines the permission that is required to read the metric.
	Permission api.Permission

	// ExpertiseLevel defines the expertise level that the metric is meant for.
	ExpertiseLevel config.ExpertiseLevel

	// Persist enabled persisting the metric on shutdown and loading the previous
	// value at start. This is only supported for counters.
	Persist bool
}

Options can be used to set advanced metric settings.

type UIntMetric

type UIntMetric interface {
	CurrentValue() uint64
}

UIntMetric is an interface for special functions of uint metrics.

Jump to

Keyboard shortcuts

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