Search and commands

↑ ↓ to select · Enter to open · Esc to close

Type to search packages.

attribute

v1.47.0
go.opentelemetry.io/otel/attribute

Documentation

pkg.go.dev ↗

Table of contents

↓ to select · Enter to jump· Shift+Enter for source

130 matches

Top ↑

Package attribute provides key and value attributes.

Functions

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 attribute keys from the return value. Excluded keys are returned as a slice of attribute values.

NewSetWithSortableFiltered

func NewSetWithSortableFiltered(kvs []KeyValue, _ *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 attributes, by attribute.EncoderID. This value should not be copied after its first use.

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

Deprecated: Use NewSetWithFiltered instead.

Types

Distinct

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

Distinct is an identifier of a Set which is very likely to be unique.

Distinct should be used as a map key instead of a Set for to provide better performance for map operations.

Distinct.Valid

func (d Distinct) Valid() bool

Valid reports whether this value refers to a valid Set.

Encoder

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

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

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

DefaultEncoder

func DefaultEncoder() Encoder

DefaultEncoder returns an attribute encoder that encodes attributes in such a way that each escaped attribute's key is followed by an equal sign and then by an escaped attribute'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.

EncoderID

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

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

NewEncoderID

func NewEncoderID() EncoderID

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

EncoderID.Valid

func (id EncoderID) Valid() bool

Valid reports whether this encoder ID was allocated by NewEncoderID. Invalid encoder IDs will not be cached.

Filter

type Filter func(KeyValue) bool

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

NewAllowKeysFilter

func NewAllowKeysFilter(keys ...Key) Filter

NewAllowKeysFilter returns a Filter that only allows attributes with one of the provided keys.

If keys is empty a deny-all filter is returned.

NewDenyKeysFilter

func NewDenyKeysFilter(keys ...Key) Filter

NewDenyKeysFilter returns a Filter that only allows attributes that do not have one of the provided keys.

If keys is empty an allow-all filter is returned.

Hasher

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

Hasher computes a Distinct value from KeyValue attributes supplied with Write.

A Hasher must be obtained from NewHasher. The zero value is not usable and its methods will panic.

NewHasher

func NewHasher() *Hasher

NewHasher returns a new Hasher.

Hasher.Distinct

func (h *Hasher) Distinct() Distinct

Distinct returns the identifier for the attributes written to h. When Write is called as described above, it returns the same value as Set.Equivalent.

Hasher.Reset

func (h *Hasher) Reset()

Reset resets h to its initial state so it can be reused.

Hasher.Write

func (h *Hasher) Write(kv KeyValue)

Write adds kv to the hash.

Write requires attributes to be supplied in ascending key order with no duplicate keys. To produce the same Distinct as Set.Equivalent, write attributes in ascending key order, with no more than one value for each key. If the source contains duplicate keys, retain the last value for each key before calling Write.

Iterator

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

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

Iterator.Attribute

func (i *Iterator) Attribute() KeyValue

Attribute returns the current KeyValue of the Iterator. It must be called only after Next returns true.

Iterator.IndexedAttribute

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

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

Iterator.IndexedLabel

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

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

Deprecated: Use IndexedAttribute instead.

Iterator.Label

func (i *Iterator) Label() KeyValue

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

Deprecated: Use Attribute instead.

Iterator.Len

func (i *Iterator) Len() int

Len returns a number of attributes in the iterated set.

Iterator.Next

func (i *Iterator) Next() bool

Next moves the iterator to the next position. Next reports whether there are more attributes.

Iterator.ToSlice

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

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

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.

Key.Bool

func (k Key) Bool(v bool) KeyValue

Bool returns a KeyValue for a bool value.

If creating both a key and value at the same time, use the package-level Bool function.

Key.BoolSlice

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

BoolSlice returns a KeyValue for a []bool value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

If creating both a key and value at the same time, use the package-level BoolSlice function.

Key.ByteSlice

func (k Key) ByteSlice(v []byte) KeyValue

ByteSlice returns a KeyValue for a []byte value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

If creating both a key and value at the same time, use the package-level ByteSlice function.

Key.Defined

func (k Key) Defined() bool

Defined reports whether the key is not empty.

Key.Float64

func (k Key) Float64(v float64) KeyValue

Float64 returns a KeyValue for a float64 value.

If creating both a key and value at the same time, use the package-level Float64 function.

Key.Float64Slice

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

Float64Slice returns a KeyValue for a []float64 value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

If creating both a key and value at the same time, use the package-level Float64Slice function.

Key.Int

func (k Key) Int(v int) KeyValue

Int returns a KeyValue for an int value.

It is provided as a convenience for Key.Int64.

If creating both a key and value at the same time, use the package-level Int function.

Key.Int64

func (k Key) Int64(v int64) KeyValue

Int64 returns a KeyValue for an int64 value.

If creating both a key and value at the same time, use the package-level Int64 function.

Key.Int64Slice

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

Int64Slice returns a KeyValue for a []int64 value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

If creating both a key and value at the same time, use the package-level Int64Slice function.

Key.IntSlice

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

IntSlice returns a KeyValue for a []int value.

It is provided as a convenience for Key.Int64Slice.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

If creating both a key and value at the same time, use the package-level IntSlice function.

Key.Map

func (k Key) Map(v ...KeyValue) KeyValue

Map returns a KeyValue for a []KeyValue value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

Users should avoid providing duplicate keys; many receivers handle maps containing duplicate keys unpredictably.

The order of v is not preserved.

If creating both a key and value at the same time, use the package-level Map function.

Key.Slice

func (k Key) Slice(v ...Value) KeyValue

Slice returns a KeyValue for a []Value value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

If creating both a key and value at the same time, use the package-level Slice function.

Key.String

func (k Key) String(v string) KeyValue

String returns a KeyValue for a string value.

If creating both a key and value at the same time, use the package-level String function.

Key.StringSlice

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

StringSlice returns a KeyValue for a []string value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

If creating both a key and value at the same time, use the package-level StringSlice function.

KeyValue

type KeyValue struct {
	Key   Key
	Value Value
}

KeyValue holds a key and value pair.

Bool

func Bool(k string, v bool) KeyValue

Bool returns a KeyValue for a bool value.

BoolSlice

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

BoolSlice returns a KeyValue for a []bool value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

ByteSlice

func ByteSlice(k string, v []byte) KeyValue

ByteSlice returns a KeyValue for a []byte value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

Float64

func Float64(k string, v float64) KeyValue

Float64 returns a KeyValue for a float64 value.

Float64Slice

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

Float64Slice returns a KeyValue for a []float64 value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

Int

func Int(k string, v int) KeyValue

Int returns a KeyValue for an int value.

It is provided as a convenience for Int64.

Int64

func Int64(k string, v int64) KeyValue

Int64 returns a KeyValue for an int64 value.

Int64Slice

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

Int64Slice returns a KeyValue for a []int64 value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

IntSlice

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

IntSlice returns a KeyValue for a []int value.

It is provided as a convenience for Int64Slice.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

Map

func Map(k string, v ...KeyValue) KeyValue

Map returns a KeyValue for a []KeyValue value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

Users should avoid providing duplicate keys; many receivers handle maps containing duplicate keys unpredictably.

The order of v is not preserved.

Slice

func Slice(k string, v ...Value) KeyValue

Slice returns a KeyValue for a []Value value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

String

func String(k, v string) KeyValue

String returns a KeyValue for a string value.

StringSlice

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

StringSlice returns a KeyValue for a []string value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

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.

KeyValue.String

func (kv KeyValue) String() string

String returns a string representation of KeyValue using the OpenTelemetry Attribute representation for non-OTLP protocols rules.

The KeyValue is encoded as a JSON object containing a single key-value pair.

KeyValue.Valid

func (kv KeyValue) Valid() bool

Valid reports whether kv is a valid OpenTelemetry attribute.

MergeIterator

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

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

NewMergeIterator

func NewMergeIterator(s1, s2 *Set) MergeIterator

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

MergeIterator.Attribute

func (m *MergeIterator) Attribute() KeyValue

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

MergeIterator.Label

func (m *MergeIterator) Label() KeyValue

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

Deprecated: Use Attribute instead.

MergeIterator.Next

func (m *MergeIterator) Next() bool

Next moves the iterator to the next position. Next reports whether there is another attribute available.

Set

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

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

This type will remain comparable for backwards compatibility. The equivalence of Sets across versions is not guaranteed to be stable. Prior versions may find two Sets to be equal or not when compared directly (i.e. ==), but subsequent versions may not. Users should use the Equals method to ensure stable equivalence checking.

Users should also use the Distinct returned from Equivalent as a map key instead of a Set directly. Set has relatively poor performance when used as a map key compared to Distinct.

EmptySet

func EmptySet() *Set

EmptySet returns a reference to a Set with no elements.

This is a convenience provided for optimized calling utility.

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.

NewSetWithSortable

func NewSetWithSortable(kvs []KeyValue, _ *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.

Deprecated: Use NewSet instead.

Set.Encoded

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

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

Set.Equals

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

Equals reports whether the argument set is equivalent to this set.

Set.Equivalent

func (l *Set) Equivalent() Distinct

Equivalent returns a value that may be used as a map key. Equal Distinct values are very likely to be equivalent attribute Sets. Distinct value of any attribute set with the same elements as this, where sets are made unique by choosing the last value in the input for any given key.

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.

Set.Get

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

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

Set.HasValue

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

HasValue reports whether a key is defined in this set.

Set.Iter

func (l *Set) Iter() Iterator

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

Set.Len

func (l *Set) Len() int

Len returns the number of attributes in this set.

Set.MarshalJSON

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

MarshalJSON returns the JSON encoding of the Set.

Set.MarshalLog

func (l Set) MarshalLog() any

MarshalLog is the marshaling function used by the logging system to represent this Set.

Set.String

func (l *Set) String() string

String returns a string representation of the Set using the OpenTelemetry Attribute Collection representation for non-OTLP protocols rules.

The Set is encoded as a JSON object. A nil or empty Set is encoded as an empty JSON object.

Set.ToSlice

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

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

Set.Value

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

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

Sortable

type Sortable []KeyValue

Sortable implements sort.Interface, used for sorting KeyValue.

Deprecated: This type is no longer used. It was added as a performance optimization for Go < 1.21 that is no longer needed (Go < 1.21 is no longer supported by the module).

Sortable.Len

func (l *Sortable) Len() int

Len implements sort.Interface.

Sortable.Less

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

Less implements sort.Interface.

Sortable.Swap

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

Swap implements sort.Interface.

Type

type Type int // nolint: revive  // redefines builtin Type.

Type describes the kind of data a Value holds.

EMPTY (13 declarations)

const (
	// EMPTY identifies a Value with no data.
	EMPTY Type = iota
	// BOOL identifies a Value containing a bool.
	BOOL
	// INT64 identifies a Value containing an int64.
	INT64
	// FLOAT64 identifies a Value containing a float64.
	FLOAT64
	// STRING identifies a Value containing a string.
	STRING
	// BOOLSLICE identifies a Value containing a []bool.
	BOOLSLICE
	// INT64SLICE identifies a Value containing a []int64.
	INT64SLICE
	// FLOAT64SLICE identifies a Value containing a []float64.
	FLOAT64SLICE
	// STRINGSLICE identifies a Value containing a []string.
	STRINGSLICE
	// BYTESLICE identifies a Value containing a []byte.
	BYTESLICE
	// SLICE identifies a Value containing a []Value.
	SLICE
	// MAP identifies a Value containing a []KeyValue representation of a map.
	//
	// Note that MAP values may contain duplicate keys if duplicate keys are
	// provided when creating the value.
	MAP
	// INVALID is used for a Value with no value set.
	//
	// Deprecated: Use EMPTY instead as an empty value is a valid value.
	INVALID = EMPTY
)

Type.String

func (i Type) String() string

Value

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

Value represents the value in key-value pairs.

Note that the zero value is a valid empty value.

BoolSliceValue

func BoolSliceValue(v []bool) Value

BoolSliceValue returns a Value for a []bool value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

BoolValue

func BoolValue(v bool) Value

BoolValue returns a Value for a bool value.

ByteSliceValue

func ByteSliceValue(v []byte) Value

ByteSliceValue returns a Value for a []byte value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

Float64SliceValue

func Float64SliceValue(v []float64) Value

Float64SliceValue returns a Value for a []float64 value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

Float64Value

func Float64Value(v float64) Value

Float64Value returns a Value for a float64 value.

Int64SliceValue

func Int64SliceValue(v []int64) Value

Int64SliceValue returns a Value for a []int64 value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

Int64Value

func Int64Value(v int64) Value

Int64Value returns a Value for an int64 value.

IntSliceValue

func IntSliceValue(v []int) Value

IntSliceValue returns a Value for a []int value.

It is provided as a convenience for Int64SliceValue.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

IntValue

func IntValue(v int) Value

IntValue returns a Value for an int value.

It is provided as a convenience for Int64Value.

MapValue

func MapValue(v ...KeyValue) Value

MapValue returns a Value for a []KeyValue value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

Users should avoid providing duplicate keys; many receivers handle maps containing duplicate keys unpredictably.

The order of v is not preserved.

SliceValue

func SliceValue(v ...Value) Value

SliceValue returns a Value for a []Value value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

StringSliceValue

func StringSliceValue(v []string) Value

StringSliceValue returns a Value for a []string value.

Note that many observability backends are not optimized to query, index, or aggregate complex attribute values. Complex values may also carry additional performance overhead. Prefer primitive values when possible.

StringValue

func StringValue(v string) Value

StringValue returns a Value for a string value.

Value.AsBool

func (v Value) AsBool() bool

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

Value.AsBoolSlice

func (v Value) AsBoolSlice() []bool

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

Value.AsByteSlice

func (v Value) AsByteSlice() []byte

AsByteSlice returns the bytes value. Make sure that the Value's type is BYTESLICE.

Value.AsFloat64

func (v Value) AsFloat64() float64

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

Value.AsFloat64Slice

func (v Value) AsFloat64Slice() []float64

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

Value.AsInt64

func (v Value) AsInt64() int64

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

Value.AsInt64Slice

func (v Value) AsInt64Slice() []int64

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

Value.AsInterface

func (v Value) AsInterface() any

AsInterface returns Value's data as any.

Value.AsMap

func (v Value) AsMap() []KeyValue

AsMap returns the []KeyValue value. Make sure that the Value's type is MAP.

The returned slice is sorted by key and may differ from the order provided when creating the map value.

The returned slice may contain duplicate keys if duplicate keys were provided when creating the map value. Callers should not assume the returned keys are unique.

Value.AsSlice

func (v Value) AsSlice() []Value

AsSlice returns the []Value value. Make sure that the Value's type is SLICE.

Value.AsString

func (v Value) AsString() string

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

Value.AsStringSlice

func (v Value) AsStringSlice() []string

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

Value.Emit

func (v Value) Emit() string

Emit returns a string representation of Value's data.

Deprecated: Use Value.String instead.

Value.MarshalJSON

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

MarshalJSON returns the JSON encoding of the Value.

Value.String

func (v Value) String() string

String returns a string representation of Value using the OpenTelemetry AnyValue representation for non-OTLP protocols rules.

Strings are returned as-is without JSON quoting, booleans and integers use JSON literals, floating-point values use JSON numbers except that NaN and ±Inf are rendered as NaN, Infinity, and -Infinity, byte slices are base64-encoded, empty values are the empty string, slices are encoded as JSON arrays, and maps are encoded as JSON objects. String, byte, and special floating-point values inside arrays and maps are encoded as JSON strings, and empty values inside arrays and maps are encoded as null.

Value.Type

func (v Value) Type() Type

Type returns v's type.

Dependency graph