Skip to content

Commit

Permalink
Deprecate Sortable
Browse files Browse the repository at this point in the history
  • Loading branch information
ash2k committed Mar 6, 2024
1 parent da2949b commit 0c0e007
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Drop support for [Go 1.20]. (#4967)

### Deprecated

- Deprecate `go.opentelemetry.io/otel/attribute.Sortable` and methods that use it. (#4734)

## [1.24.0/0.46.0/0.0.1-alpha] 2024-02-23

This release is the last to support [Go 1.20].
Expand Down
40 changes: 15 additions & 25 deletions attribute/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
package attribute // import "go.opentelemetry.io/otel/attribute"

import (
"cmp"
"encoding/json"
"reflect"
"slices"
"sort"
"sync"
)

type (
Expand All @@ -28,10 +29,9 @@ type (
iface interface{}
}

// 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().
// Sortable implements sort.Interface, used for sorting KeyValue.
//
// Deprecated: no longer needed.
Sortable []KeyValue
)

Expand All @@ -45,12 +45,6 @@ var (
iface: [0]KeyValue{},
},
}

// sortables is a pool of Sortables used to create Sets with a user does
// not provide one.
sortables = sync.Pool{
New: func() interface{} { return new(Sortable) },
}
)

// EmptySet returns a reference to a Set with no elements.
Expand Down Expand Up @@ -180,22 +174,21 @@ func NewSet(kvs ...KeyValue) Set {
if len(kvs) == 0 {
return empty()
}
srt := sortables.Get().(*Sortable)
s, _ := NewSetWithSortableFiltered(kvs, srt, nil)
sortables.Put(srt)
s, _ := NewSetWithSortableFiltered(kvs, nil, nil)
return s
}

// NewSetWithSortable returns a new Set. See the documentation for
// NewSetWithSortableFiltered for more details.
//
// This call includes a Sortable option as a memory optimization.
func NewSetWithSortable(kvs []KeyValue, tmp *Sortable) Set {
// Deprecated: NewSet.
func NewSetWithSortable(kvs []KeyValue, _ *Sortable) Set {
// Check for empty set.
if len(kvs) == 0 {
return empty()
}
s, _ := NewSetWithSortableFiltered(kvs, tmp, nil)
s, _ := NewSetWithSortableFiltered(kvs, nil, nil)
return s
}

Expand All @@ -209,9 +202,7 @@ func NewSetWithFiltered(kvs []KeyValue, filter Filter) (Set, []KeyValue) {
if len(kvs) == 0 {
return empty(), nil
}
srt := sortables.Get().(*Sortable)
s, filtered := NewSetWithSortableFiltered(kvs, srt, filter)
sortables.Put(srt)
s, filtered := NewSetWithSortableFiltered(kvs, nil, filter)
return s, filtered
}

Expand All @@ -238,19 +229,18 @@ func NewSetWithFiltered(kvs []KeyValue, filter Filter) (Set, []KeyValue) {
//
// The second []KeyValue return value is a list of attributes that were
// excluded by the Filter (if non-nil).
func NewSetWithSortableFiltered(kvs []KeyValue, tmp *Sortable, filter Filter) (Set, []KeyValue) {
// Deprecated: use NewSetWithFiltered.
func NewSetWithSortableFiltered(kvs []KeyValue, _ *Sortable, filter Filter) (Set, []KeyValue) {
// Check for empty set.
if len(kvs) == 0 {
return empty(), nil
}

*tmp = kvs

// Stable sort so the following de-duplication can implement
// last-value-wins semantics.
sort.Stable(tmp)

*tmp = nil
slices.SortStableFunc(kvs, func(a, b KeyValue) int {
return cmp.Compare(a.Key, b.Key)
})

position := len(kvs) - 1
offset := position - 1
Expand Down

0 comments on commit 0c0e007

Please sign in to comment.