metric

package
v3.8.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2024 License: Apache-2.0 Imports: 11 Imported by: 70

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNonNumeric        = errors.New("non-numeric value for rate/delta")
	ErrNoStoreToCalcDiff = errors.New("cannot use deltas nor rates without persistent store")
	ErrTooCloseSamples   = errors.New("samples too close in time, skipping")
	ErrNegativeDiff      = errors.New("source was reset, skipping")
	ErrOverrideSetAttrs  = errors.New("cannot overwrite metric-set attributes")
	ErrDeltaWithNoAttrs  = errors.New("delta/rate metrics should be attached to an attribute identified metric-set")
)

Errors

View Source
var SourcesNameToType = map[string]SourceType{
	"gauge":     GAUGE,
	"rate":      RATE,
	"delta":     DELTA,
	"prate":     PRATE,
	"pdelta":    PDELTA,
	"attribute": ATTRIBUTE,
}

SourcesNameToType metric sources list mapping its name to type.

View Source
var SourcesTypeToName = map[SourceType]string{
	GAUGE:     "gauge",
	RATE:      "rate",
	DELTA:     "delta",
	ATTRIBUTE: "attribute",
}

SourcesTypeToName metric sources list mapping its type to readable name.

Functions

func AddCustomAttributes

func AddCustomAttributes(metricSet *Set, customAttributes []attribute.Attribute)

AddCustomAttributes add customAttributes to MetricSet

Types

type Set

type Set struct {
	Metrics map[string]interface{}
	// contains filtered or unexported fields
}

Set is the basic structure for storing metrics.

func NewSet

func NewSet(eventType string, storer persist.Storer, attributes ...attribute.Attribute) (s *Set)

NewSet creates new metrics set, optionally related to a list of attributes. These attributes makes the metric-set unique. If related attributes are used, then new attributes are added.

func (*Set) AddNamespaceAttributes

func (ms *Set) AddNamespaceAttributes(attributes ...attribute.Attribute)

AddNamespaceAttributes add attributes to MetricSet namespace.

func (*Set) MarshalJSON

func (ms *Set) MarshalJSON() ([]byte, error)

MarshalJSON adapts the internal structure of the metrics Set to the payload that is compliant with the protocol

func (*Set) MarshalMetrics

func (ms *Set) MarshalMetrics(v interface{}) error

MarshalMetrics creates metrics for primitive values of v.

MarshalMetrics traverses the value of v recursively. Once a non-struct or non-pointer value is reached that has the accepted tags, SetMetric is then called with the field's value.

Pointers are dereferenced until a base value is found or nil. If nil, the field is skipped regardless of if it had the appropriate struct field tags.

Needed struct field tags are "metric_name" and "source_type". The value of "metric_name" will be the name argument to SetMetric. The value of "source_type" is case insensitively matched against values below to a SourceType and passed as the sourceType argument to SetMetric. If the value does not match one of the values below an error will be returned.

  • gauge
  • rate
  • prate
  • delta
  • pdelta
  • attribute

If one of the required tags is missing an error will be returned. If both are missing SetMetric will not be called for the given field.

Examples of struct field tags:

type Data struct {
   Gauge     int     `metric_name:"metric.gauge" source_type:"Gauge"`
   Attribute string  `metric_name:"metric.attribute" source_type:"attribute"`
   Rate      float64 `metric_name:"metric.rate" source_type:"RATE"`
   Delta     float64 `metric_name:"metric.delta" source_type:"delta"`
   PRate     float64 `metric_name:"metric.prate" source_type:"prate"`
   PDelta    float64 `metric_name:"metric.pdelta" source_type:"pdelta"`
}

Any non-struct/non-pointer value that has the correct struct field tags will be passed to SetMetric. If the value causes an error to be returned from SetMetric this will be bubbled up and returned by MarshalMetrics.

If a cyclic data structure is passed in this will result in infinite recursion.

func (*Set) SetMetric

func (ms *Set) SetMetric(name string, value interface{}, sourceType SourceType) (err error)

SetMetric adds a metric to the Set object or updates the metric value if the metric already exists. It calculates elapsed difference for RATE and DELTA types.

func (*Set) UnmarshalJSON

func (ms *Set) UnmarshalJSON(data []byte) error

UnmarshalJSON unserializes protocol compliant JSON metrics into the metric set.

type SourceType

type SourceType int

SourceType defines the kind of data source. Based on this SourceType, metric package performs some calculations with it. Check below the description for each one.

const (
	// GAUGE is a value that may increase and decrease. It is stored as-is.
	GAUGE SourceType = iota
	// RATE is an ever-growing value which might be reset. The package calculates the change rate.
	RATE SourceType = iota
	// DELTA is an ever-growing value which might be reset. The package calculates the difference between samples.
	DELTA SourceType = iota
	// ATTRIBUTE is any string value
	ATTRIBUTE SourceType = iota
	// PRATE is a version of RATE that only allows positive values.
	PRATE SourceType = iota
	// PDELTA is a version of DELTA that only allows positive values.
	PDELTA SourceType = iota
)

Source types If any more SourceTypes are added update maps: SourcesTypeToName & SourcesNameToType.

func SourceTypeForName

func SourceTypeForName(sourceTypeTag string) (SourceType, error)

SourceTypeForName does a case insensitive conversion from a string to a SourceType. An error will be returned if no valid SourceType matched.

func (SourceType) IsPositive

func (t SourceType) IsPositive() bool

IsPositive checks that the `SourceType` belongs to the positive only list of `SourceType`s

func (SourceType) String

func (t SourceType) String() string

String fulfills stringer interface, returning empty string on invalid source types.

Jump to

Keyboard shortcuts

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