set.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. // Copyright The OpenTelemetry Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package attribute // import "go.opentelemetry.io/otel/attribute"
  15. import (
  16. "encoding/json"
  17. "reflect"
  18. "sort"
  19. "sync"
  20. )
  21. type (
  22. // Set is the representation for a distinct attribute set. It manages an
  23. // immutable set of attributes, with an internal cache for storing
  24. // attribute encodings.
  25. //
  26. // This type supports the Equivalent method of comparison using values of
  27. // type Distinct.
  28. Set struct {
  29. equivalent Distinct
  30. }
  31. // Distinct wraps a variable-size array of KeyValue, constructed with keys
  32. // in sorted order. This can be used as a map key or for equality checking
  33. // between Sets.
  34. Distinct struct {
  35. iface interface{}
  36. }
  37. // Sortable implements sort.Interface, used for sorting KeyValue. This is
  38. // an exported type to support a memory optimization. A pointer to one of
  39. // these is needed for the call to sort.Stable(), which the caller may
  40. // provide in order to avoid an allocation. See NewSetWithSortable().
  41. Sortable []KeyValue
  42. )
  43. var (
  44. // keyValueType is used in computeDistinctReflect.
  45. keyValueType = reflect.TypeOf(KeyValue{})
  46. // emptySet is returned for empty attribute sets.
  47. emptySet = &Set{
  48. equivalent: Distinct{
  49. iface: [0]KeyValue{},
  50. },
  51. }
  52. // sortables is a pool of Sortables used to create Sets with a user does
  53. // not provide one.
  54. sortables = sync.Pool{
  55. New: func() interface{} { return new(Sortable) },
  56. }
  57. )
  58. // EmptySet returns a reference to a Set with no elements.
  59. //
  60. // This is a convenience provided for optimized calling utility.
  61. func EmptySet() *Set {
  62. return emptySet
  63. }
  64. // reflectValue abbreviates reflect.ValueOf(d).
  65. func (d Distinct) reflectValue() reflect.Value {
  66. return reflect.ValueOf(d.iface)
  67. }
  68. // Valid returns true if this value refers to a valid Set.
  69. func (d Distinct) Valid() bool {
  70. return d.iface != nil
  71. }
  72. // Len returns the number of attributes in this set.
  73. func (l *Set) Len() int {
  74. if l == nil || !l.equivalent.Valid() {
  75. return 0
  76. }
  77. return l.equivalent.reflectValue().Len()
  78. }
  79. // Get returns the KeyValue at ordered position idx in this set.
  80. func (l *Set) Get(idx int) (KeyValue, bool) {
  81. if l == nil || !l.equivalent.Valid() {
  82. return KeyValue{}, false
  83. }
  84. value := l.equivalent.reflectValue()
  85. if idx >= 0 && idx < value.Len() {
  86. // Note: The Go compiler successfully avoids an allocation for
  87. // the interface{} conversion here:
  88. return value.Index(idx).Interface().(KeyValue), true
  89. }
  90. return KeyValue{}, false
  91. }
  92. // Value returns the value of a specified key in this set.
  93. func (l *Set) Value(k Key) (Value, bool) {
  94. if l == nil || !l.equivalent.Valid() {
  95. return Value{}, false
  96. }
  97. rValue := l.equivalent.reflectValue()
  98. vlen := rValue.Len()
  99. idx := sort.Search(vlen, func(idx int) bool {
  100. return rValue.Index(idx).Interface().(KeyValue).Key >= k
  101. })
  102. if idx >= vlen {
  103. return Value{}, false
  104. }
  105. keyValue := rValue.Index(idx).Interface().(KeyValue)
  106. if k == keyValue.Key {
  107. return keyValue.Value, true
  108. }
  109. return Value{}, false
  110. }
  111. // HasValue tests whether a key is defined in this set.
  112. func (l *Set) HasValue(k Key) bool {
  113. if l == nil {
  114. return false
  115. }
  116. _, ok := l.Value(k)
  117. return ok
  118. }
  119. // Iter returns an iterator for visiting the attributes in this set.
  120. func (l *Set) Iter() Iterator {
  121. return Iterator{
  122. storage: l,
  123. idx: -1,
  124. }
  125. }
  126. // ToSlice returns the set of attributes belonging to this set, sorted, where
  127. // keys appear no more than once.
  128. func (l *Set) ToSlice() []KeyValue {
  129. iter := l.Iter()
  130. return iter.ToSlice()
  131. }
  132. // Equivalent returns a value that may be used as a map key. The Distinct type
  133. // guarantees that the result will equal the equivalent. Distinct value of any
  134. // attribute set with the same elements as this, where sets are made unique by
  135. // choosing the last value in the input for any given key.
  136. func (l *Set) Equivalent() Distinct {
  137. if l == nil || !l.equivalent.Valid() {
  138. return emptySet.equivalent
  139. }
  140. return l.equivalent
  141. }
  142. // Equals returns true if the argument set is equivalent to this set.
  143. func (l *Set) Equals(o *Set) bool {
  144. return l.Equivalent() == o.Equivalent()
  145. }
  146. // Encoded returns the encoded form of this set, according to encoder.
  147. func (l *Set) Encoded(encoder Encoder) string {
  148. if l == nil || encoder == nil {
  149. return ""
  150. }
  151. return encoder.Encode(l.Iter())
  152. }
  153. func empty() Set {
  154. return Set{
  155. equivalent: emptySet.equivalent,
  156. }
  157. }
  158. // NewSet returns a new Set. See the documentation for
  159. // NewSetWithSortableFiltered for more details.
  160. //
  161. // Except for empty sets, this method adds an additional allocation compared
  162. // with calls that include a Sortable.
  163. func NewSet(kvs ...KeyValue) Set {
  164. // Check for empty set.
  165. if len(kvs) == 0 {
  166. return empty()
  167. }
  168. srt := sortables.Get().(*Sortable)
  169. s, _ := NewSetWithSortableFiltered(kvs, srt, nil)
  170. sortables.Put(srt)
  171. return s
  172. }
  173. // NewSetWithSortable returns a new Set. See the documentation for
  174. // NewSetWithSortableFiltered for more details.
  175. //
  176. // This call includes a Sortable option as a memory optimization.
  177. func NewSetWithSortable(kvs []KeyValue, tmp *Sortable) Set {
  178. // Check for empty set.
  179. if len(kvs) == 0 {
  180. return empty()
  181. }
  182. s, _ := NewSetWithSortableFiltered(kvs, tmp, nil)
  183. return s
  184. }
  185. // NewSetWithFiltered returns a new Set. See the documentation for
  186. // NewSetWithSortableFiltered for more details.
  187. //
  188. // This call includes a Filter to include/exclude attribute keys from the
  189. // return value. Excluded keys are returned as a slice of attribute values.
  190. func NewSetWithFiltered(kvs []KeyValue, filter Filter) (Set, []KeyValue) {
  191. // Check for empty set.
  192. if len(kvs) == 0 {
  193. return empty(), nil
  194. }
  195. srt := sortables.Get().(*Sortable)
  196. s, filtered := NewSetWithSortableFiltered(kvs, srt, filter)
  197. sortables.Put(srt)
  198. return s, filtered
  199. }
  200. // NewSetWithSortableFiltered returns a new Set.
  201. //
  202. // Duplicate keys are eliminated by taking the last value. This
  203. // re-orders the input slice so that unique last-values are contiguous
  204. // at the end of the slice.
  205. //
  206. // This ensures the following:
  207. //
  208. // - Last-value-wins semantics
  209. // - Caller sees the reordering, but doesn't lose values
  210. // - Repeated call preserve last-value wins.
  211. //
  212. // Note that methods are defined on Set, although this returns Set. Callers
  213. // can avoid memory allocations by:
  214. //
  215. // - allocating a Sortable for use as a temporary in this method
  216. // - allocating a Set for storing the return value of this constructor.
  217. //
  218. // The result maintains a cache of encoded attributes, by attribute.EncoderID.
  219. // This value should not be copied after its first use.
  220. //
  221. // The second []KeyValue return value is a list of attributes that were
  222. // excluded by the Filter (if non-nil).
  223. func NewSetWithSortableFiltered(kvs []KeyValue, tmp *Sortable, filter Filter) (Set, []KeyValue) {
  224. // Check for empty set.
  225. if len(kvs) == 0 {
  226. return empty(), nil
  227. }
  228. *tmp = kvs
  229. // Stable sort so the following de-duplication can implement
  230. // last-value-wins semantics.
  231. sort.Stable(tmp)
  232. *tmp = nil
  233. position := len(kvs) - 1
  234. offset := position - 1
  235. // The requirements stated above require that the stable
  236. // result be placed in the end of the input slice, while
  237. // overwritten values are swapped to the beginning.
  238. //
  239. // De-duplicate with last-value-wins semantics. Preserve
  240. // duplicate values at the beginning of the input slice.
  241. for ; offset >= 0; offset-- {
  242. if kvs[offset].Key == kvs[position].Key {
  243. continue
  244. }
  245. position--
  246. kvs[offset], kvs[position] = kvs[position], kvs[offset]
  247. }
  248. kvs = kvs[position:]
  249. if filter != nil {
  250. if div := filteredToFront(kvs, filter); div != 0 {
  251. return Set{equivalent: computeDistinct(kvs[div:])}, kvs[:div]
  252. }
  253. }
  254. return Set{equivalent: computeDistinct(kvs)}, nil
  255. }
  256. // filteredToFront filters slice in-place using keep function. All KeyValues that need to
  257. // be removed are moved to the front. All KeyValues that need to be kept are
  258. // moved (in-order) to the back. The index for the first KeyValue to be kept is
  259. // returned.
  260. func filteredToFront(slice []KeyValue, keep Filter) int {
  261. n := len(slice)
  262. j := n
  263. for i := n - 1; i >= 0; i-- {
  264. if keep(slice[i]) {
  265. j--
  266. slice[i], slice[j] = slice[j], slice[i]
  267. }
  268. }
  269. return j
  270. }
  271. // Filter returns a filtered copy of this Set. See the documentation for
  272. // NewSetWithSortableFiltered for more details.
  273. func (l *Set) Filter(re Filter) (Set, []KeyValue) {
  274. if re == nil {
  275. return *l, nil
  276. }
  277. // Iterate in reverse to the first attribute that will be filtered out.
  278. n := l.Len()
  279. first := n - 1
  280. for ; first >= 0; first-- {
  281. kv, _ := l.Get(first)
  282. if !re(kv) {
  283. break
  284. }
  285. }
  286. // No attributes will be dropped, return the immutable Set l and nil.
  287. if first < 0 {
  288. return *l, nil
  289. }
  290. // Copy now that we know we need to return a modified set.
  291. //
  292. // Do not do this in-place on the underlying storage of *Set l. Sets are
  293. // immutable and filtering should not change this.
  294. slice := l.ToSlice()
  295. // Don't re-iterate the slice if only slice[0] is filtered.
  296. if first == 0 {
  297. // It is safe to assume len(slice) >= 1 given we found at least one
  298. // attribute above that needs to be filtered out.
  299. return Set{equivalent: computeDistinct(slice[1:])}, slice[:1]
  300. }
  301. // Move the filtered slice[first] to the front (preserving order).
  302. kv := slice[first]
  303. copy(slice[1:first+1], slice[:first])
  304. slice[0] = kv
  305. // Do not re-evaluate re(slice[first+1:]).
  306. div := filteredToFront(slice[1:first+1], re) + 1
  307. return Set{equivalent: computeDistinct(slice[div:])}, slice[:div]
  308. }
  309. // computeDistinct returns a Distinct using either the fixed- or
  310. // reflect-oriented code path, depending on the size of the input. The input
  311. // slice is assumed to already be sorted and de-duplicated.
  312. func computeDistinct(kvs []KeyValue) Distinct {
  313. iface := computeDistinctFixed(kvs)
  314. if iface == nil {
  315. iface = computeDistinctReflect(kvs)
  316. }
  317. return Distinct{
  318. iface: iface,
  319. }
  320. }
  321. // computeDistinctFixed computes a Distinct for small slices. It returns nil
  322. // if the input is too large for this code path.
  323. func computeDistinctFixed(kvs []KeyValue) interface{} {
  324. switch len(kvs) {
  325. case 1:
  326. ptr := new([1]KeyValue)
  327. copy((*ptr)[:], kvs)
  328. return *ptr
  329. case 2:
  330. ptr := new([2]KeyValue)
  331. copy((*ptr)[:], kvs)
  332. return *ptr
  333. case 3:
  334. ptr := new([3]KeyValue)
  335. copy((*ptr)[:], kvs)
  336. return *ptr
  337. case 4:
  338. ptr := new([4]KeyValue)
  339. copy((*ptr)[:], kvs)
  340. return *ptr
  341. case 5:
  342. ptr := new([5]KeyValue)
  343. copy((*ptr)[:], kvs)
  344. return *ptr
  345. case 6:
  346. ptr := new([6]KeyValue)
  347. copy((*ptr)[:], kvs)
  348. return *ptr
  349. case 7:
  350. ptr := new([7]KeyValue)
  351. copy((*ptr)[:], kvs)
  352. return *ptr
  353. case 8:
  354. ptr := new([8]KeyValue)
  355. copy((*ptr)[:], kvs)
  356. return *ptr
  357. case 9:
  358. ptr := new([9]KeyValue)
  359. copy((*ptr)[:], kvs)
  360. return *ptr
  361. case 10:
  362. ptr := new([10]KeyValue)
  363. copy((*ptr)[:], kvs)
  364. return *ptr
  365. default:
  366. return nil
  367. }
  368. }
  369. // computeDistinctReflect computes a Distinct using reflection, works for any
  370. // size input.
  371. func computeDistinctReflect(kvs []KeyValue) interface{} {
  372. at := reflect.New(reflect.ArrayOf(len(kvs), keyValueType)).Elem()
  373. for i, keyValue := range kvs {
  374. *(at.Index(i).Addr().Interface().(*KeyValue)) = keyValue
  375. }
  376. return at.Interface()
  377. }
  378. // MarshalJSON returns the JSON encoding of the Set.
  379. func (l *Set) MarshalJSON() ([]byte, error) {
  380. return json.Marshal(l.equivalent.iface)
  381. }
  382. // MarshalLog is the marshaling function used by the logging system to represent this Set.
  383. func (l Set) MarshalLog() interface{} {
  384. kvs := make(map[string]string)
  385. for _, kv := range l.ToSlice() {
  386. kvs[string(kv.Key)] = kv.Value.Emit()
  387. }
  388. return kvs
  389. }
  390. // Len implements sort.Interface.
  391. func (l *Sortable) Len() int {
  392. return len(*l)
  393. }
  394. // Swap implements sort.Interface.
  395. func (l *Sortable) Swap(i, j int) {
  396. (*l)[i], (*l)[j] = (*l)[j], (*l)[i]
  397. }
  398. // Less implements sort.Interface.
  399. func (l *Sortable) Less(i, j int) bool {
  400. return (*l)[i].Key < (*l)[j].Key
  401. }