2022-04-06 23:20:29 +00:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-05-07 10:19:58 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2022-04-06 23:20:29 +00:00
|
|
|
|
|
|
|
package attribute // import "go.opentelemetry.io/otel/attribute"
|
|
|
|
|
|
|
|
import (
|
2024-05-07 10:19:58 +00:00
|
|
|
"cmp"
|
2022-04-06 23:20:29 +00:00
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
2024-05-07 10:19:58 +00:00
|
|
|
"slices"
|
2022-04-06 23:20:29 +00:00
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2023-12-11 11:58:19 +00:00
|
|
|
// Set is the representation for a distinct attribute set. It manages an
|
|
|
|
// immutable set of attributes, with an internal cache for storing
|
|
|
|
// attribute encodings.
|
2022-04-06 23:20:29 +00:00
|
|
|
//
|
2024-05-07 10:19:58 +00:00
|
|
|
// 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. In addition to that type providing guarantees
|
|
|
|
// on stable equivalence, it may also provide performance improvements.
|
2022-04-06 23:20:29 +00:00
|
|
|
Set struct {
|
|
|
|
equivalent Distinct
|
|
|
|
}
|
|
|
|
|
2024-05-07 10:19:58 +00:00
|
|
|
// Distinct is a unique identifier of a Set.
|
|
|
|
//
|
|
|
|
// Distinct is designed to be ensures equivalence stability: comparisons
|
|
|
|
// will return the save value across versions. For this reason, Distinct
|
|
|
|
// should always be used as a map key instead of a Set.
|
2022-04-06 23:20:29 +00:00
|
|
|
Distinct struct {
|
|
|
|
iface interface{}
|
|
|
|
}
|
|
|
|
|
2024-05-07 10:19:58 +00:00
|
|
|
// 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).
|
2022-04-06 23:20:29 +00:00
|
|
|
Sortable []KeyValue
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-12-11 11:58:19 +00:00
|
|
|
// keyValueType is used in computeDistinctReflect.
|
2022-04-06 23:20:29 +00:00
|
|
|
keyValueType = reflect.TypeOf(KeyValue{})
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// emptySet is returned for empty attribute sets.
|
2022-04-06 23:20:29 +00:00
|
|
|
emptySet = &Set{
|
|
|
|
equivalent: Distinct{
|
|
|
|
iface: [0]KeyValue{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// EmptySet returns a reference to a Set with no elements.
|
|
|
|
//
|
|
|
|
// This is a convenience provided for optimized calling utility.
|
|
|
|
func EmptySet() *Set {
|
|
|
|
return emptySet
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// reflectValue abbreviates reflect.ValueOf(d).
|
|
|
|
func (d Distinct) reflectValue() reflect.Value {
|
2022-04-06 23:20:29 +00:00
|
|
|
return reflect.ValueOf(d.iface)
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// Valid returns true if this value refers to a valid Set.
|
2022-04-06 23:20:29 +00:00
|
|
|
func (d Distinct) Valid() bool {
|
|
|
|
return d.iface != nil
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// Len returns the number of attributes in this set.
|
2022-04-06 23:20:29 +00:00
|
|
|
func (l *Set) Len() int {
|
|
|
|
if l == nil || !l.equivalent.Valid() {
|
|
|
|
return 0
|
|
|
|
}
|
2023-12-11 11:58:19 +00:00
|
|
|
return l.equivalent.reflectValue().Len()
|
2022-04-06 23:20:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// Get returns the KeyValue at ordered position idx in this set.
|
2022-04-06 23:20:29 +00:00
|
|
|
func (l *Set) Get(idx int) (KeyValue, bool) {
|
2023-12-11 11:58:19 +00:00
|
|
|
if l == nil || !l.equivalent.Valid() {
|
2022-04-06 23:20:29 +00:00
|
|
|
return KeyValue{}, false
|
|
|
|
}
|
2023-12-11 11:58:19 +00:00
|
|
|
value := l.equivalent.reflectValue()
|
2022-04-06 23:20:29 +00:00
|
|
|
|
|
|
|
if idx >= 0 && idx < value.Len() {
|
|
|
|
// Note: The Go compiler successfully avoids an allocation for
|
|
|
|
// the interface{} conversion here:
|
|
|
|
return value.Index(idx).Interface().(KeyValue), true
|
|
|
|
}
|
|
|
|
|
|
|
|
return KeyValue{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the value of a specified key in this set.
|
|
|
|
func (l *Set) Value(k Key) (Value, bool) {
|
2023-12-11 11:58:19 +00:00
|
|
|
if l == nil || !l.equivalent.Valid() {
|
2022-04-06 23:20:29 +00:00
|
|
|
return Value{}, false
|
|
|
|
}
|
2023-12-11 11:58:19 +00:00
|
|
|
rValue := l.equivalent.reflectValue()
|
2022-04-06 23:20:29 +00:00
|
|
|
vlen := rValue.Len()
|
|
|
|
|
|
|
|
idx := sort.Search(vlen, func(idx int) bool {
|
|
|
|
return rValue.Index(idx).Interface().(KeyValue).Key >= k
|
|
|
|
})
|
|
|
|
if idx >= vlen {
|
|
|
|
return Value{}, false
|
|
|
|
}
|
|
|
|
keyValue := rValue.Index(idx).Interface().(KeyValue)
|
|
|
|
if k == keyValue.Key {
|
|
|
|
return keyValue.Value, true
|
|
|
|
}
|
|
|
|
return Value{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasValue tests whether a key is defined in this set.
|
|
|
|
func (l *Set) HasValue(k Key) bool {
|
|
|
|
if l == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, ok := l.Value(k)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// Iter returns an iterator for visiting the attributes in this set.
|
2022-04-06 23:20:29 +00:00
|
|
|
func (l *Set) Iter() Iterator {
|
|
|
|
return Iterator{
|
|
|
|
storage: l,
|
|
|
|
idx: -1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// ToSlice returns the set of attributes belonging to this set, sorted, where
|
|
|
|
// keys appear no more than once.
|
2022-04-06 23:20:29 +00:00
|
|
|
func (l *Set) ToSlice() []KeyValue {
|
|
|
|
iter := l.Iter()
|
|
|
|
return iter.ToSlice()
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// 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
|
|
|
|
// 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.
|
2022-04-06 23:20:29 +00:00
|
|
|
func (l *Set) Equivalent() Distinct {
|
|
|
|
if l == nil || !l.equivalent.Valid() {
|
|
|
|
return emptySet.equivalent
|
|
|
|
}
|
|
|
|
return l.equivalent
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equals returns true if the argument set is equivalent to this set.
|
|
|
|
func (l *Set) Equals(o *Set) bool {
|
|
|
|
return l.Equivalent() == o.Equivalent()
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// Encoded returns the encoded form of this set, according to encoder.
|
2022-04-06 23:20:29 +00:00
|
|
|
func (l *Set) Encoded(encoder Encoder) string {
|
|
|
|
if l == nil || encoder == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return encoder.Encode(l.Iter())
|
|
|
|
}
|
|
|
|
|
|
|
|
func empty() Set {
|
|
|
|
return Set{
|
|
|
|
equivalent: emptySet.equivalent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// NewSet returns a new Set. See the documentation for
|
|
|
|
// NewSetWithSortableFiltered for more details.
|
2022-04-06 23:20:29 +00:00
|
|
|
//
|
2023-12-11 11:58:19 +00:00
|
|
|
// Except for empty sets, this method adds an additional allocation compared
|
|
|
|
// with calls that include a Sortable.
|
2022-04-06 23:20:29 +00:00
|
|
|
func NewSet(kvs ...KeyValue) Set {
|
2024-05-07 10:19:58 +00:00
|
|
|
s, _ := NewSetWithFiltered(kvs, nil)
|
2022-04-06 23:20:29 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// NewSetWithSortable returns a new Set. See the documentation for
|
|
|
|
// NewSetWithSortableFiltered for more details.
|
2022-04-06 23:20:29 +00:00
|
|
|
//
|
2023-12-11 11:58:19 +00:00
|
|
|
// This call includes a Sortable option as a memory optimization.
|
2024-05-07 10:19:58 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use [NewSet] instead.
|
|
|
|
func NewSetWithSortable(kvs []KeyValue, _ *Sortable) Set {
|
|
|
|
s, _ := NewSetWithFiltered(kvs, nil)
|
2022-04-06 23:20:29 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// NewSetWithFiltered returns a new Set. See the documentation for
|
|
|
|
// NewSetWithSortableFiltered for more details.
|
2022-04-06 23:20:29 +00:00
|
|
|
//
|
2023-12-11 11:58:19 +00:00
|
|
|
// This call includes a Filter to include/exclude attribute keys from the
|
|
|
|
// return value. Excluded keys are returned as a slice of attribute values.
|
2022-04-06 23:20:29 +00:00
|
|
|
func NewSetWithFiltered(kvs []KeyValue, filter Filter) (Set, []KeyValue) {
|
|
|
|
// Check for empty set.
|
|
|
|
if len(kvs) == 0 {
|
|
|
|
return empty(), nil
|
|
|
|
}
|
2024-05-07 10:19:58 +00:00
|
|
|
|
|
|
|
// Stable sort so the following de-duplication can implement
|
|
|
|
// last-value-wins semantics.
|
|
|
|
slices.SortStableFunc(kvs, func(a, b KeyValue) int {
|
|
|
|
return cmp.Compare(a.Key, b.Key)
|
|
|
|
})
|
|
|
|
|
|
|
|
position := len(kvs) - 1
|
|
|
|
offset := position - 1
|
|
|
|
|
|
|
|
// The requirements stated above require that the stable
|
|
|
|
// result be placed in the end of the input slice, while
|
|
|
|
// overwritten values are swapped to the beginning.
|
|
|
|
//
|
|
|
|
// De-duplicate with last-value-wins semantics. Preserve
|
|
|
|
// duplicate values at the beginning of the input slice.
|
|
|
|
for ; offset >= 0; offset-- {
|
|
|
|
if kvs[offset].Key == kvs[position].Key {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
position--
|
|
|
|
kvs[offset], kvs[position] = kvs[position], kvs[offset]
|
|
|
|
}
|
|
|
|
kvs = kvs[position:]
|
|
|
|
|
|
|
|
if filter != nil {
|
|
|
|
if div := filteredToFront(kvs, filter); div != 0 {
|
|
|
|
return Set{equivalent: computeDistinct(kvs[div:])}, kvs[:div]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Set{equivalent: computeDistinct(kvs)}, nil
|
2022-04-06 23:20:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// NewSetWithSortableFiltered returns a new Set.
|
2022-04-06 23:20:29 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2023-12-11 11:58:19 +00:00
|
|
|
// Note that methods are defined on Set, although this returns Set. Callers
|
|
|
|
// can avoid memory allocations by:
|
2022-04-06 23:20:29 +00:00
|
|
|
//
|
2023-12-11 11:58:19 +00:00
|
|
|
// - allocating a Sortable for use as a temporary in this method
|
|
|
|
// - allocating a Set for storing the return value of this constructor.
|
2022-04-06 23:20:29 +00:00
|
|
|
//
|
2023-12-11 11:58:19 +00:00
|
|
|
// The result maintains a cache of encoded attributes, by attribute.EncoderID.
|
2022-04-06 23:20:29 +00:00
|
|
|
// This value should not be copied after its first use.
|
|
|
|
//
|
2023-12-11 11:58:19 +00:00
|
|
|
// The second []KeyValue return value is a list of attributes that were
|
2022-04-06 23:20:29 +00:00
|
|
|
// excluded by the Filter (if non-nil).
|
2024-05-07 10:19:58 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use [NewSetWithFiltered] instead.
|
|
|
|
func NewSetWithSortableFiltered(kvs []KeyValue, _ *Sortable, filter Filter) (Set, []KeyValue) {
|
|
|
|
return NewSetWithFiltered(kvs, filter)
|
2022-04-06 23:20:29 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 10:19:58 +00:00
|
|
|
// filteredToFront filters slice in-place using keep function. All KeyValues that need to
|
|
|
|
// be removed are moved to the front. All KeyValues that need to be kept are
|
|
|
|
// moved (in-order) to the back. The index for the first KeyValue to be kept is
|
|
|
|
// returned.
|
|
|
|
func filteredToFront(slice []KeyValue, keep Filter) int {
|
|
|
|
n := len(slice)
|
|
|
|
j := n
|
|
|
|
for i := n - 1; i >= 0; i-- {
|
|
|
|
if keep(slice[i]) {
|
|
|
|
j--
|
|
|
|
slice[i], slice[j] = slice[j], slice[i]
|
2022-04-06 23:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-07 10:19:58 +00:00
|
|
|
return j
|
2022-04-06 23:20:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// Filter returns a filtered copy of this Set. See the documentation for
|
|
|
|
// NewSetWithSortableFiltered for more details.
|
2022-04-06 23:20:29 +00:00
|
|
|
func (l *Set) Filter(re Filter) (Set, []KeyValue) {
|
|
|
|
if re == nil {
|
2024-05-07 10:19:58 +00:00
|
|
|
return *l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate in reverse to the first attribute that will be filtered out.
|
|
|
|
n := l.Len()
|
|
|
|
first := n - 1
|
|
|
|
for ; first >= 0; first-- {
|
|
|
|
kv, _ := l.Get(first)
|
|
|
|
if !re(kv) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No attributes will be dropped, return the immutable Set l and nil.
|
|
|
|
if first < 0 {
|
|
|
|
return *l, nil
|
2022-04-06 23:20:29 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 10:19:58 +00:00
|
|
|
// Copy now that we know we need to return a modified set.
|
|
|
|
//
|
|
|
|
// Do not do this in-place on the underlying storage of *Set l. Sets are
|
|
|
|
// immutable and filtering should not change this.
|
|
|
|
slice := l.ToSlice()
|
|
|
|
|
|
|
|
// Don't re-iterate the slice if only slice[0] is filtered.
|
|
|
|
if first == 0 {
|
|
|
|
// It is safe to assume len(slice) >= 1 given we found at least one
|
|
|
|
// attribute above that needs to be filtered out.
|
|
|
|
return Set{equivalent: computeDistinct(slice[1:])}, slice[:1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the filtered slice[first] to the front (preserving order).
|
|
|
|
kv := slice[first]
|
|
|
|
copy(slice[1:first+1], slice[:first])
|
|
|
|
slice[0] = kv
|
|
|
|
|
|
|
|
// Do not re-evaluate re(slice[first+1:]).
|
|
|
|
div := filteredToFront(slice[1:first+1], re) + 1
|
|
|
|
return Set{equivalent: computeDistinct(slice[div:])}, slice[:div]
|
2022-04-06 23:20:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// computeDistinct returns a Distinct using either the fixed- or
|
|
|
|
// reflect-oriented code path, depending on the size of the input. The input
|
|
|
|
// slice is assumed to already be sorted and de-duplicated.
|
2022-04-06 23:20:29 +00:00
|
|
|
func computeDistinct(kvs []KeyValue) Distinct {
|
|
|
|
iface := computeDistinctFixed(kvs)
|
|
|
|
if iface == nil {
|
|
|
|
iface = computeDistinctReflect(kvs)
|
|
|
|
}
|
|
|
|
return Distinct{
|
|
|
|
iface: iface,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// computeDistinctFixed computes a Distinct for small slices. It returns nil
|
|
|
|
// if the input is too large for this code path.
|
2022-04-06 23:20:29 +00:00
|
|
|
func computeDistinctFixed(kvs []KeyValue) interface{} {
|
|
|
|
switch len(kvs) {
|
|
|
|
case 1:
|
|
|
|
ptr := new([1]KeyValue)
|
|
|
|
copy((*ptr)[:], kvs)
|
|
|
|
return *ptr
|
|
|
|
case 2:
|
|
|
|
ptr := new([2]KeyValue)
|
|
|
|
copy((*ptr)[:], kvs)
|
|
|
|
return *ptr
|
|
|
|
case 3:
|
|
|
|
ptr := new([3]KeyValue)
|
|
|
|
copy((*ptr)[:], kvs)
|
|
|
|
return *ptr
|
|
|
|
case 4:
|
|
|
|
ptr := new([4]KeyValue)
|
|
|
|
copy((*ptr)[:], kvs)
|
|
|
|
return *ptr
|
|
|
|
case 5:
|
|
|
|
ptr := new([5]KeyValue)
|
|
|
|
copy((*ptr)[:], kvs)
|
|
|
|
return *ptr
|
|
|
|
case 6:
|
|
|
|
ptr := new([6]KeyValue)
|
|
|
|
copy((*ptr)[:], kvs)
|
|
|
|
return *ptr
|
|
|
|
case 7:
|
|
|
|
ptr := new([7]KeyValue)
|
|
|
|
copy((*ptr)[:], kvs)
|
|
|
|
return *ptr
|
|
|
|
case 8:
|
|
|
|
ptr := new([8]KeyValue)
|
|
|
|
copy((*ptr)[:], kvs)
|
|
|
|
return *ptr
|
|
|
|
case 9:
|
|
|
|
ptr := new([9]KeyValue)
|
|
|
|
copy((*ptr)[:], kvs)
|
|
|
|
return *ptr
|
|
|
|
case 10:
|
|
|
|
ptr := new([10]KeyValue)
|
|
|
|
copy((*ptr)[:], kvs)
|
|
|
|
return *ptr
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// computeDistinctReflect computes a Distinct using reflection, works for any
|
|
|
|
// size input.
|
2022-04-06 23:20:29 +00:00
|
|
|
func computeDistinctReflect(kvs []KeyValue) interface{} {
|
|
|
|
at := reflect.New(reflect.ArrayOf(len(kvs), keyValueType)).Elem()
|
|
|
|
for i, keyValue := range kvs {
|
|
|
|
*(at.Index(i).Addr().Interface().(*KeyValue)) = keyValue
|
|
|
|
}
|
|
|
|
return at.Interface()
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// MarshalJSON returns the JSON encoding of the Set.
|
2022-04-06 23:20:29 +00:00
|
|
|
func (l *Set) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(l.equivalent.iface)
|
|
|
|
}
|
|
|
|
|
2024-05-07 10:19:58 +00:00
|
|
|
// MarshalLog is the marshaling function used by the logging system to represent this Set.
|
2022-04-06 23:20:29 +00:00
|
|
|
func (l Set) MarshalLog() interface{} {
|
|
|
|
kvs := make(map[string]string)
|
|
|
|
for _, kv := range l.ToSlice() {
|
|
|
|
kvs[string(kv.Key)] = kv.Value.Emit()
|
|
|
|
}
|
|
|
|
return kvs
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// Len implements sort.Interface.
|
2022-04-06 23:20:29 +00:00
|
|
|
func (l *Sortable) Len() int {
|
|
|
|
return len(*l)
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// Swap implements sort.Interface.
|
2022-04-06 23:20:29 +00:00
|
|
|
func (l *Sortable) Swap(i, j int) {
|
|
|
|
(*l)[i], (*l)[j] = (*l)[j], (*l)[i]
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:58:19 +00:00
|
|
|
// Less implements sort.Interface.
|
2022-04-06 23:20:29 +00:00
|
|
|
func (l *Sortable) Less(i, j int) bool {
|
|
|
|
return (*l)[i].Key < (*l)[j].Key
|
|
|
|
}
|