attribute

package
Version: v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2021 License: Apache-2.0 Imports: 9 Imported by: 524

Documentation

Overview

Package attribute provides key and value attributes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSetWithFiltered

func NewSetWithFiltered(kvs []KeyValue, filter Filter) (Set, []KeyValue)

NewSetWithFiltered returns a new `Set`. See the documentation for `NewSetWithSortableFiltered` for more details.

This call includes a `Filter` to include/exclude label keys from the return value. Excluded keys are returned as a slice of label values.

func NewSetWithSortableFiltered

func NewSetWithSortableFiltered(kvs []KeyValue, tmp *Sortable, filter Filter) (Set, []KeyValue)

NewSetWithSortableFiltered returns a new `Set`.

Duplicate keys are eliminated by taking the last value. This re-orders the input slice so that unique last-values are contiguous at the end of the slice.

This ensures the following:

- Last-value-wins semantics - Caller sees the reordering, but doesn't lose values - Repeated call preserve last-value wins.

Note that methods are defined on `*Set`, although this returns `Set`. Callers can avoid memory allocations by:

- allocating a `Sortable` for use as a temporary in this method - allocating a `Set` for storing the return value of this

constructor.

The result maintains a cache of encoded labels, by attribute.EncoderID. This value should not be copied after its first use.

The second `[]KeyValue` return value is a list of labels that were excluded by the Filter (if non-nil).

Types

type Distinct

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

Distinct wraps a variable-size array of `KeyValue`, constructed with keys in sorted order. This can be used as a map key or for equality checking between Sets.

func (Distinct) Valid

func (d Distinct) Valid() bool

Valid returns true if this value refers to a valid `*Set`.

type Encoder

type Encoder interface {
	// Encode returns the serialized encoding of the label
	// set using its Iterator.  This result may be cached
	// by a attribute.Set.
	Encode(iterator Iterator) string

	// ID returns a value that is unique for each class of
	// label encoder.  Label encoders allocate these using
	// `NewEncoderID`.
	ID() EncoderID
}

Encoder is a mechanism for serializing a label set into a specific string representation that supports caching, to avoid repeated serialization. An example could be an exporter encoding the label set into a wire representation.

func DefaultEncoder

func DefaultEncoder() Encoder

DefaultEncoder returns a label encoder that encodes labels in such a way that each escaped label's key is followed by an equal sign and then by an escaped label's value. All key-value pairs are separated by a comma.

Escaping is done by prepending a backslash before either a backslash, equal sign or a comma.

type EncoderID

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

EncoderID is used to identify distinct Encoder implementations, for caching encoded results.

func NewEncoderID

func NewEncoderID() EncoderID

NewEncoderID returns a unique label encoder ID. It should be called once per each type of label encoder. Preferably in init() or in var definition.

func (EncoderID) Valid

func (id EncoderID) Valid() bool

Valid returns true if this encoder ID was allocated by `NewEncoderID`. Invalid encoder IDs will not be cached.

type Filter

type Filter func(KeyValue) bool

Filter supports removing certain labels from label sets. When the filter returns true, the label will be kept in the filtered label set. When the filter returns false, the label is excluded from the filtered label set, and the label instead appears in the `removed` list of excluded labels.

type Iterator

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

Iterator allows iterating over the set of labels in order, sorted by key.

func (*Iterator) Attribute

func (i *Iterator) Attribute() KeyValue

Attribute is a synonym for Label().

func (*Iterator) IndexedLabel

func (i *Iterator) IndexedLabel() (int, KeyValue)

IndexedLabel returns current index and attribute. Must be called only after Next returns true.

func (*Iterator) Label

func (i *Iterator) Label() KeyValue

Label returns current KeyValue. Must be called only after Next returns true.

func (*Iterator) Len

func (i *Iterator) Len() int

Len returns a number of labels in the iterator's `*Set`.

func (*Iterator) Next

func (i *Iterator) Next() bool

Next moves the iterator to the next position. Returns false if there are no more labels.

func (*Iterator) ToSlice

func (i *Iterator) ToSlice() []KeyValue

ToSlice is a convenience function that creates a slice of labels from the passed iterator. The iterator is set up to start from the beginning before creating the slice.

type Key

type Key string

Key represents the key part in key-value pairs. It's a string. The allowed character set in the key depends on the use of the key.

func (Key) Bool

func (k Key) Bool(v bool) KeyValue

Bool creates a KeyValue instance with a BOOL Value.

If creating both a key and value at the same time, use the provided convenience function instead -- Bool(name, value).

func (Key) BoolSlice added in v1.0.0

func (k Key) BoolSlice(v []bool) KeyValue

BoolSlice creates a KeyValue instance with a BOOLSLICE Value.

If creating both a key and value at the same time, use the provided convenience function instead -- BoolSlice(name, value).

func (Key) Defined

func (k Key) Defined() bool

Defined returns true for non-empty keys.

func (Key) Float64

func (k Key) Float64(v float64) KeyValue

Float64 creates a KeyValue instance with a FLOAT64 Value.

If creating both a key and value at the same time, use the provided convenience function instead -- Float64(name, value).

func (Key) Float64Slice added in v1.0.0

func (k Key) Float64Slice(v []float64) KeyValue

Float64Slice creates a KeyValue instance with a FLOAT64SLICE Value.

If creating both a key and value at the same time, use the provided convenience function instead -- Float64(name, value).

func (Key) Int

func (k Key) Int(v int) KeyValue

Int creates a KeyValue instance with an INT64 Value.

If creating both a key and value at the same time, use the provided convenience function instead -- Int(name, value).

func (Key) Int64

func (k Key) Int64(v int64) KeyValue

Int64 creates a KeyValue instance with an INT64 Value.

If creating both a key and value at the same time, use the provided convenience function instead -- Int64(name, value).

func (Key) Int64Slice added in v1.0.0

func (k Key) Int64Slice(v []int64) KeyValue

Int64Slice creates a KeyValue instance with an INT64SLICE Value.

If creating both a key and value at the same time, use the provided convenience function instead -- Int64Slice(name, value).

func (Key) IntSlice added in v1.0.0

func (k Key) IntSlice(v []int) KeyValue

IntSlice creates a KeyValue instance with an INT64SLICE Value.

If creating both a key and value at the same time, use the provided convenience function instead -- IntSlice(name, value).

func (Key) String

func (k Key) String(v string) KeyValue

String creates a KeyValue instance with a STRING Value.

If creating both a key and value at the same time, use the provided convenience function instead -- String(name, value).

func (Key) StringSlice added in v1.0.0

func (k Key) StringSlice(v []string) KeyValue

StringSlice creates a KeyValue instance with a STRINGSLICE Value.

If creating both a key and value at the same time, use the provided convenience function instead -- StringSlice(name, value).

type KeyValue

type KeyValue struct {
	Key   Key
	Value Value
}

KeyValue holds a key and value pair.

func Bool

func Bool(k string, v bool) KeyValue

Bool creates a KeyValue with a BOOL Value type.

func BoolSlice added in v1.0.0

func BoolSlice(k string, v []bool) KeyValue

BoolSlice creates a KeyValue with a BOOLSLICE Value type.

func Float64

func Float64(k string, v float64) KeyValue

Float64 creates a KeyValue with a FLOAT64 Value type.

func Float64Slice added in v1.0.0

func Float64Slice(k string, v []float64) KeyValue

Float64Slice creates a KeyValue with a FLOAT64SLICE Value type.

func Int

func Int(k string, v int) KeyValue

Int creates a KeyValue with an INT64 Value type.

func Int64

func Int64(k string, v int64) KeyValue

Int64 creates a KeyValue with an INT64 Value type.

func Int64Slice added in v1.0.0

func Int64Slice(k string, v []int64) KeyValue

Int64Slice creates a KeyValue with an INT64SLICE Value type.

func IntSlice added in v1.0.0

func IntSlice(k string, v []int) KeyValue

IntSlice creates a KeyValue with an INT64SLICE Value type.

func String

func String(k, v string) KeyValue

String creates a KeyValue with a STRING Value type.

func StringSlice added in v1.0.0

func StringSlice(k string, v []string) KeyValue

StringSlice creates a KeyValue with a STRINGSLICE Value type.

func Stringer

func Stringer(k string, v fmt.Stringer) KeyValue

Stringer creates a new key-value pair with a passed name and a string value generated by the passed Stringer interface.

func (KeyValue) Valid added in v0.19.0

func (kv KeyValue) Valid() bool

Valid returns if kv is a valid OpenTelemetry attribute.

type MergeIterator

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

MergeIterator supports iterating over two sets of labels while eliminating duplicate values from the combined set. The first iterator value takes precedence.

func NewMergeIterator

func NewMergeIterator(s1, s2 *Set) MergeIterator

NewMergeIterator returns a MergeIterator for merging two label sets Duplicates are resolved by taking the value from the first set.

func (*MergeIterator) Label

func (m *MergeIterator) Label() KeyValue

Label returns the current value after Next() returns true.

func (*MergeIterator) Next

func (m *MergeIterator) Next() bool

Next returns true if there is another label available.

type Set

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

Set is the representation for a distinct label set. It manages an immutable set of labels, with an internal cache for storing label encodings.

This type supports the `Equivalent` method of comparison using values of type `Distinct`.

This type is used to implement: 1. Metric labels 2. Resource sets 3. Correlation map (TODO)

func EmptySet

func EmptySet() *Set

EmptySet returns a reference to a Set with no elements.

This is a convenience provided for optimized calling utility.

func NewSet

func NewSet(kvs ...KeyValue) Set

NewSet returns a new `Set`. See the documentation for `NewSetWithSortableFiltered` for more details.

Except for empty sets, this method adds an additional allocation compared with calls that include a `*Sortable`.

func NewSetWithSortable

func NewSetWithSortable(kvs []KeyValue, tmp *Sortable) Set

NewSetWithSortable returns a new `Set`. See the documentation for `NewSetWithSortableFiltered` for more details.

This call includes a `*Sortable` option as a memory optimization.

func (*Set) Encoded

func (l *Set) Encoded(encoder Encoder) string

Encoded returns the encoded form of this set, according to `encoder`.

func (*Set) Equals

func (l *Set) Equals(o *Set) bool

Equals returns true if the argument set is equivalent to this set.

func (*Set) Equivalent

func (l *Set) Equivalent() Distinct

Equivalent returns a value that may be used as a map key. The Distinct type guarantees that the result will equal the equivalent Distinct value of any label set with the same elements as this, where sets are made unique by choosing the last value in the input for any given key.

func (*Set) Filter

func (l *Set) Filter(re Filter) (Set, []KeyValue)

Filter returns a filtered copy of this `Set`. See the documentation for `NewSetWithSortableFiltered` for more details.

func (*Set) Get

func (l *Set) Get(idx int) (KeyValue, bool)

Get returns the KeyValue at ordered position `idx` in this set.

func (*Set) HasValue

func (l *Set) HasValue(k Key) bool

HasValue tests whether a key is defined in this set.

func (*Set) Iter

func (l *Set) Iter() Iterator

Iter returns an iterator for visiting the labels in this set.

func (*Set) Len

func (l *Set) Len() int

Len returns the number of labels in this set.

func (*Set) MarshalJSON

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

MarshalJSON returns the JSON encoding of the `*Set`.

func (*Set) ToSlice

func (l *Set) ToSlice() []KeyValue

ToSlice returns the set of labels belonging to this set, sorted, where keys appear no more than once.

func (*Set) Value

func (l *Set) Value(k Key) (Value, bool)

Value returns the value of a specified key in this set.

type Sortable

type Sortable []KeyValue

Sortable implements `sort.Interface`, used for sorting `KeyValue`. This is an exported type to support a memory optimization. A pointer to one of these is needed for the call to `sort.Stable()`, which the caller may provide in order to avoid an allocation. See `NewSetWithSortable()`.

func (*Sortable) Len

func (l *Sortable) Len() int

Len implements `sort.Interface`.

func (*Sortable) Less

func (l *Sortable) Less(i, j int) bool

Less implements `sort.Interface`.

func (*Sortable) Swap

func (l *Sortable) Swap(i, j int)

Swap implements `sort.Interface`.

type Type

type Type int

Type describes the type of the data Value holds.

const (
	// INVALID is used for a Value with no value set.
	INVALID Type = iota
	// BOOL is a boolean Type Value.
	BOOL
	// INT64 is a 64-bit signed integral Type Value.
	INT64
	// FLOAT64 is a 64-bit floating point Type Value.
	FLOAT64
	// STRING is a string Type Value.
	STRING
	// BOOLSLICE is a slice of booleans Type Value.
	BOOLSLICE
	// INT64SLICE is a slice of 64-bit signed integral numbers Type Value.
	INT64SLICE
	// FLOAT64SLICE is a slice of 64-bit floating point numbers Type Value.
	FLOAT64SLICE
	// STRINGSLICE is a slice of strings Type Value.
	STRINGSLICE
)

func (Type) String

func (i Type) String() string

type Value

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

Value represents the value part in key-value pairs.

func BoolSliceValue added in v1.0.0

func BoolSliceValue(v []bool) Value

BoolSliceValue creates a BOOLSLICE Value.

func BoolValue

func BoolValue(v bool) Value

BoolValue creates a BOOL Value.

func Float64SliceValue added in v1.0.0

func Float64SliceValue(v []float64) Value

Float64SliceValue creates a FLOAT64SLICE Value.

func Float64Value

func Float64Value(v float64) Value

Float64Value creates a FLOAT64 Value.

func Int64SliceValue added in v1.0.0

func Int64SliceValue(v []int64) Value

Int64SliceValue creates an INT64SLICE Value.

func Int64Value

func Int64Value(v int64) Value

Int64Value creates an INT64 Value.

func IntSliceValue added in v1.0.0

func IntSliceValue(v []int) Value

IntSliceValue creates an INTSLICE Value.

func IntValue

func IntValue(v int) Value

IntValue creates an INT64 Value.

func StringSliceValue added in v1.0.0

func StringSliceValue(v []string) Value

StringSliceValue creates a STRINGSLICE Value.

func StringValue

func StringValue(v string) Value

StringValue creates a STRING Value.

func (Value) AsBool

func (v Value) AsBool() bool

AsBool returns the bool value. Make sure that the Value's type is BOOL.

func (Value) AsBoolSlice added in v1.0.0

func (v Value) AsBoolSlice() []bool

AsBoolSlice returns the []bool value. Make sure that the Value's type is BOOLSLICE.

func (Value) AsFloat64

func (v Value) AsFloat64() float64

AsFloat64 returns the float64 value. Make sure that the Value's type is FLOAT64.

func (Value) AsFloat64Slice added in v1.0.0

func (v Value) AsFloat64Slice() []float64

AsFloat64Slice returns the []float64 value. Make sure that the Value's type is INT64SLICE.

func (Value) AsInt64

func (v Value) AsInt64() int64

AsInt64 returns the int64 value. Make sure that the Value's type is INT64.

func (Value) AsInt64Slice added in v1.0.0

func (v Value) AsInt64Slice() []int64

AsInt64Slice returns the []int64 value. Make sure that the Value's type is INT64SLICE.

func (Value) AsInterface

func (v Value) AsInterface() interface{}

AsInterface returns Value's data as interface{}.

func (Value) AsString

func (v Value) AsString() string

AsString returns the string value. Make sure that the Value's type is STRING.

func (Value) AsStringSlice added in v1.0.0

func (v Value) AsStringSlice() []string

AsStringSlice returns the []string value. Make sure that the Value's type is INT64SLICE.

func (Value) Emit

func (v Value) Emit() string

Emit returns a string representation of Value's data.

func (Value) MarshalJSON

func (v Value) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON encoding of the Value.

func (Value) Type

func (v Value) Type() Type

Type returns a type of the Value.

Jump to

Keyboard shortcuts

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