metrics

package
v0.11.3 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2021 License: Apache-2.0 Imports: 13 Imported by: 122

Documentation

Overview

Package metrics implements data types for probes generated data.

Index

Constants

View Source
const (
	// CUMULATIVE metrics accumulate with time and are usually used to
	// represent counters, e.g. number of requests.
	CUMULATIVE = iota
	// GAUGE metrics are used to represent values at a certain point of
	// time, e.g. pending queries.
	GAUGE
)

Variables

This section is empty.

Functions

func IsString added in v0.10.6

func IsString(v Value) bool

IsString checks if the given value is a string.

Types

type AtomicInt

type AtomicInt struct {

	// If Str is defined, this is method used to convert AtomicInt into a string.
	Str func(int64) string
	// contains filtered or unexported fields
}

AtomicInt implements NumValue with int64 storage and atomic operations. If concurrency-safety is not a requirement, e.g. for use in already mutex protected map, you could use Int.

func NewAtomicInt

func NewAtomicInt(i int64) *AtomicInt

NewAtomicInt returns a new AtomicInt

func (*AtomicInt) Add

func (i *AtomicInt) Add(val Value) error

Add adds a Value to the receiver AtomicInt. If Value is not AtomicInt, an error is returned. It's part of the Value interface.

func (*AtomicInt) AddFloat64

func (i *AtomicInt) AddFloat64(f float64)

AddFloat64 adds a float64 to the receiver Int.

func (*AtomicInt) AddInt64

func (i *AtomicInt) AddInt64(ii int64)

AddInt64 adds an int64 to the receiver Int.

func (*AtomicInt) Clone

func (i *AtomicInt) Clone() Value

Clone returns a copy the receiver AtomicInt

func (*AtomicInt) Float64

func (i *AtomicInt) Float64() float64

Float64 returns the stored int64 as a float64

func (*AtomicInt) Inc

func (i *AtomicInt) Inc()

Inc increments the receiver AtomicInt by one. It's part of the NumValue interface.

func (*AtomicInt) IncBy

func (i *AtomicInt) IncBy(delta NumValue)

IncBy increments the receiver AtomicInt by "delta" NumValue. It's part of the NumValue interface.

func (*AtomicInt) Int64

func (i *AtomicInt) Int64() int64

Int64 returns the stored int64

func (*AtomicInt) String

func (i *AtomicInt) String() string

String returns the string representation of AtomicInt. It's part of the Value interface.

func (*AtomicInt) SubtractCounter added in v0.11.3

func (i *AtomicInt) SubtractCounter(lastVal Value) (bool, error)

SubtractCounter subtracts the provided "lastVal". Note that this function is not fully atomic: we first load the values, compare them, and then update the receiver if required. There is a possibility that either receiver, or lastVal may change between loading of the values and updating them. We should still not get negative values though, as we use the snapshots to finally update the value.

type Distribution

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

Distribution metrics type implements a histogram of values distributed over a set of pre-defined buckets.

func NewDistribution

func NewDistribution(lowerBounds []float64) *Distribution

NewDistribution returns a new distribution container.

func NewDistributionFromProto

func NewDistributionFromProto(distProto *distpb.Dist) (*Distribution, error)

NewDistributionFromProto returns a new distribution based on the provided protobuf.

func NewExponentialDistribution

func NewExponentialDistribution(base, scaleFactor float64, numBuckets int) (*Distribution, error)

NewExponentialDistribution returns a distribution container with exponentially growing bucket sizes. Buckets' lower bounds are determined as follows: -Inf, 0, scale_factor, scale_factor * base, scale_factor * base^2, ... scale_factor * base^(i-1).., ith bucket ... scale_factor * base^(numBuckets), last element (numBuckets+1-th)

func ParseDistFromString

func ParseDistFromString(str string) (*Distribution, error)

ParseDistFromString parses a distribution value from a string that's in a format that's generated by the String() method: Example string: dist:sum:899|count:221|lb:-Inf,0.5,2,7.5|bc:34,54,121,12

func (*Distribution) Add

func (d *Distribution) Add(val Value) error

Add adds a distribution to the receiver distribution. If both distributions don't have the same buckets, an error is returned.

func (*Distribution) AddFloat64

func (d *Distribution) AddFloat64(f float64)

AddFloat64 adds an float64 to the receiver distribution.

func (*Distribution) AddInt64

func (d *Distribution) AddInt64(i int64)

AddInt64 adds an int64 to the receiver distribution.

func (*Distribution) AddSample

func (d *Distribution) AddSample(sample float64)

AddSample adds a sample to the receiver distribution.

func (*Distribution) Clone

func (d *Distribution) Clone() Value

Clone returns a copy of the receiver distribution.

func (*Distribution) Data

func (d *Distribution) Data() *DistributionData

Data returns a DistributionData object, built using Distribution's current state.

func (*Distribution) StackdriverTypedValue

func (d *Distribution) StackdriverTypedValue() *monitoring.TypedValue

StackdriverTypedValue returns a Stackdriver typed value corresponding to the receiver distribution. This routine is used by stackdriver surfacer.

func (*Distribution) String

func (d *Distribution) String() string

String returns a string representation of the distribution: "dist:sum:<sum>|count:<count>|lb:<lower bounds>|bc:<bucket counts>" For example for a distribution with lower bounds 0.5, 2.0, 7.5 and bucket counts 34, 54, 121, 12, string representation will look like the following: dist:sum:899|count:221|lb:-Inf,0.5,2,7.5|bc:34,54,121,12

func (*Distribution) SubtractCounter added in v0.11.3

func (d *Distribution) SubtractCounter(lastVal Value) (bool, error)

SubtractCounter subtracts the provided "lastVal", assuming that value represents a counter, i.e. if "value" is less than "lastVal", we assume that counter has been reset and don't subtract.

func (*Distribution) Verify

func (d *Distribution) Verify() error

Verify verifies that the distribution is valid.

type DistributionData

type DistributionData struct {
	LowerBounds  []float64 // bucket lower bounds
	BucketCounts []int64
	Count        int64   // count of all values
	Sum          float64 // sum of all samples.
}

DistributionData stuct, along with Data() function, provides a way to readily share the Distribution data with other packages.

type EventMetrics

type EventMetrics struct {
	Timestamp time.Time
	Kind      Kind

	LatencyUnit time.Duration
	// contains filtered or unexported fields
}

EventMetrics respresents metrics associated with a particular time event.

func NewEventMetrics

func NewEventMetrics(ts time.Time) *EventMetrics

NewEventMetrics return a new EventMetrics object with internals maps initialized.

func (*EventMetrics) AddLabel

func (em *EventMetrics) AddLabel(name string, val string) *EventMetrics

AddLabel adds a label (name & value) into the receiver EventMetrics. If a label with the same name exists already, new label is ignored. AddLabel returns the receiver EventMetrics to allow for the chaining of these calls, for example:

em := metrics.NewEventMetrics(time.Now()).
	AddMetric("sent", &prr.sent).
	AddLabel("ptype", "http").
	AddLabel("dst", target)

func (*EventMetrics) AddMetric

func (em *EventMetrics) AddMetric(name string, val Value) *EventMetrics

AddMetric adds a metric (name & value) into the receiver EventMetric. If a metric with the same name exists already, new metric is ignored. AddMetric returns the receiver EventMetrics to allow for the chaining of these calls, for example:

em := metrics.NewEventMetrics(time.Now()).
	AddMetric("sent", &prr.sent).
	AddMetric("rcvd", &prr.rcvd).
	AddMetric("rtt", &prr.rtt)

func (*EventMetrics) Clone

func (em *EventMetrics) Clone() *EventMetrics

Clone clones the underlying fields. This is useful for creating copies of the EventMetrics objects.

func (*EventMetrics) Key added in v0.11.3

func (em *EventMetrics) Key() string

Key returns a string key that uniquely identifies an eventmetrics.

func (*EventMetrics) Label

func (em *EventMetrics) Label(name string) string

Label returns an EventMetrics label value by name. Label will return a zero-string ("") for a non-existent label.

func (*EventMetrics) LabelsKeys

func (em *EventMetrics) LabelsKeys() []string

LabelsKeys returns the list of all label keys.

func (*EventMetrics) Metric

func (em *EventMetrics) Metric(name string) Value

Metric returns an EventMetrics metric value by name. Metric will return nil for a non-existent metric.

func (*EventMetrics) MetricsKeys

func (em *EventMetrics) MetricsKeys() []string

MetricsKeys returns the list of all metric keys.

func (*EventMetrics) String

func (em *EventMetrics) String() string

String returns the string representation of the EventMetrics. Note that this is compatible with what vmwatcher understands. Example output string: 1519084040 labels=ptype=http sent=62 rcvd=52 resp-code=map:code,200:44,204:8

func (*EventMetrics) SubtractLast added in v0.11.3

func (em *EventMetrics) SubtractLast(lastEM *EventMetrics) (*EventMetrics, error)

SubtractLast subtracts the provided (last) EventMetrics from the receiver EventMetrics and return the result as a GAUGE EventMetrics.

func (*EventMetrics) Update

func (em *EventMetrics) Update(in *EventMetrics) error

Update updates the receiver EventMetrics with the incoming one.

type Float

type Float struct {

	// If Str is defined, this is method used to convert Float into a string.
	Str func(float64) string
	// contains filtered or unexported fields
}

Float implements NumValue with float64 storage. Note that Float is not concurrency safe.

func NewFloat

func NewFloat(f float64) *Float

NewFloat returns a new Float.

func (*Float) Add

func (f *Float) Add(val Value) error

Add adds a Value to the receiver Float. If Value is not Float, an error is returned. It's part of the Value interface.

func (*Float) AddFloat64

func (f *Float) AddFloat64(ff float64)

AddFloat64 adds a float64 to the receiver Float.

func (*Float) AddInt64

func (f *Float) AddInt64(i int64)

AddInt64 adds an int64 to the receiver Float.

func (*Float) Clone

func (f *Float) Clone() Value

Clone returns a copy the receiver Float

func (*Float) Float64

func (f *Float) Float64() float64

Float64 returns the stored float64.

func (*Float) Inc

func (f *Float) Inc()

Inc increments the receiver Float by one. It's part of the NumValue interface.

func (*Float) IncBy

func (f *Float) IncBy(delta NumValue)

IncBy increments the receiver Float by "delta" NumValue. It's part of the NumValue interface.

func (*Float) Int64

func (f *Float) Int64() int64

Int64 returns the stored float64 as int64.

func (*Float) String

func (f *Float) String() string

String returns the string representation of Float. It's part of the Value interface.

func (*Float) SubtractCounter added in v0.11.3

func (f *Float) SubtractCounter(lastVal Value) (bool, error)

SubtractCounter subtracts the provided "lastVal", assuming that value represents a counter, i.e. if "value" is less than "lastVal", we assume that counter has been reset and don't subtract.

type Int

type Int struct {

	// If Str is defined, this is method used to convert Int into a string.
	Str func(int64) string
	// contains filtered or unexported fields
}

Int implements NumValue with int64 storage. Note that Int is not concurrency safe, if you want a concurrency safe integer NumValue, use AtomicInt.

func NewInt

func NewInt(i int64) *Int

NewInt returns a new Int

func (*Int) Add

func (i *Int) Add(val Value) error

Add adds a Value to the receiver Int. If Value is not Int, an error is returned. It's part of the Value interface.

func (*Int) AddFloat64

func (i *Int) AddFloat64(f float64)

AddFloat64 adds a float64 to the receiver Int.

func (*Int) AddInt64

func (i *Int) AddInt64(ii int64)

AddInt64 adds an int64 to the receiver Int.

func (*Int) Clone

func (i *Int) Clone() Value

Clone returns a copy the receiver Int

func (*Int) Float64

func (i *Int) Float64() float64

Float64 returns the stored int64 as a float64

func (*Int) Inc

func (i *Int) Inc()

Inc increments the receiver Int by one. It's part of the NumValue interface.

func (*Int) IncBy

func (i *Int) IncBy(delta NumValue)

IncBy increments the receiver Int by "delta" NumValue. It's part of the NumValue interface.

func (*Int) Int64

func (i *Int) Int64() int64

Int64 returns the stored int64

func (*Int) String

func (i *Int) String() string

String returns the string representation of Int. It's part of the Value interface.

func (*Int) SubtractCounter added in v0.11.3

func (i *Int) SubtractCounter(lastVal Value) (bool, error)

SubtractCounter subtracts the provided "lastVal", assuming that value represents a counter, i.e. if "value" is less than "lastVal", we assume that counter has been reset and don't subtract.

type Kind

type Kind int

Kind represents EventMetrics type. There are currently only two kinds of EventMetrics supported: CUMULATIVE and GAUGE

type Map

type Map struct {
	MapName string // Map key name
	// contains filtered or unexported fields
}

Map implements a key-value store where keys are of type string and values are of type NumValue. It satisfies the Value interface.

func NewMap

func NewMap(mapName string, defaultValue NumValue) *Map

NewMap returns a new Map

func ParseMapFromString added in v0.10.3

func ParseMapFromString(mapValue string) (*Map, error)

ParseMapFromString parses a map value string into a map object. Note that the values are always parsed as floats, so even a map with integer values will become a float map. For example: "map:code,200:10123,404:21" will be parsed as: "map:code 200:10123.000 404:21.000".

func (*Map) Add

func (m *Map) Add(val Value) error

Add adds a value (type Value) to the receiver Map. A non-Map value returns an error. This is part of the Value interface.

func (*Map) AddFloat64

func (m *Map) AddFloat64(f float64)

AddFloat64 generates a panic for the Map type. This is added only to satisfy the Value interface.

func (*Map) AddInt64

func (m *Map) AddInt64(i int64)

AddInt64 generates a panic for the Map type. This is added only to satisfy the Value interface.

func (*Map) Clone

func (m *Map) Clone() Value

Clone creates a clone of the Map. Clone makes sure that underlying data storage is properly cloned.

func (*Map) GetKey

func (m *Map) GetKey(key string) NumValue

GetKey returns the given key's value.

func (*Map) IncKey

func (m *Map) IncKey(key string)

IncKey increments the given key's value by one.

func (*Map) IncKeyBy

func (m *Map) IncKeyBy(key string, delta NumValue)

IncKeyBy increments the given key's value by NumValue.

func (*Map) Keys

func (m *Map) Keys() []string

Keys returns the list of keys

func (*Map) String

func (m *Map) String() string

String returns the string representation of the receiver Map. This is part of the Value interface. map:key,k1:v1,k2:v2

func (*Map) SubtractCounter added in v0.11.3

func (m *Map) SubtractCounter(lastVal Value) (bool, error)

SubtractCounter subtracts the provided "lastVal", assuming that value represents a counter, i.e. if "value" is less than "lastVal", we assume that counter has been reset and don't subtract.

type NumValue

type NumValue interface {
	Value
	Inc()
	Int64() int64
	Float64() float64
	IncBy(delta NumValue)
}

NumValue represents any numerical metric value, e.g. Int, Float. It's a superset of Value interface.

type String

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

String implements a value type with string storage. It satisfies the Value interface.

func NewString

func NewString(s string) String

NewString returns a new String with the given string value.

func (String) Add

func (s String) Add(val Value) error

Add isn't supported for the String type, this is only to satisfy the Value interface.

func (String) AddFloat64

func (s String) AddFloat64(f float64)

AddFloat64 generates a panic for the String type. This is added only to satisfy the Value interface.

func (String) AddInt64

func (s String) AddInt64(i int64)

AddInt64 generates a panic for the String type. This is added only to satisfy the Value interface.

func (String) Clone

func (s String) Clone() Value

Clone returns the copy of receiver String.

func (String) String

func (s String) String() string

String simply returns the stored string.

func (String) SubtractCounter added in v0.11.3

func (s String) SubtractCounter(val Value) (bool, error)

SubtractCounter isn't supported for the String type, this is only to satisfy the Value interface.

type Value

type Value interface {
	Clone() Value
	Add(delta Value) error
	AddInt64(i int64)
	AddFloat64(f float64)
	String() string

	// SubtractCounter subtracts the provided "lastVal", assuming that value
	// represents a counter, i.e. if "value" is less than the "lastVal", we
	// assume that counter has been reset and don't subtract.
	SubtractCounter(last Value) (wasReset bool, err error)
}

Value represents any metric value

func ParseValueFromString added in v0.10.6

func ParseValueFromString(val string) (Value, error)

ParseValueFromString parses a value from its string representation

Directories

Path Synopsis
Package payload provides utilities to work with the metrics in payload.
Package payload provides utilities to work with the metrics in payload.
Package testutils provides utilities for tests.
Package testutils provides utilities for tests.

Jump to

Keyboard shortcuts

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