set.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. )
  20. type (
  21. // Set is the representation for a distinct label set. It
  22. // manages an immutable set of labels, with an internal cache
  23. // for storing label encodings.
  24. //
  25. // This type supports the `Equivalent` method of comparison
  26. // using values of type `Distinct`.
  27. //
  28. // This type is used to implement:
  29. // 1. Metric labels
  30. // 2. Resource sets
  31. // 3. Correlation map (TODO)
  32. Set struct {
  33. equivalent Distinct
  34. }
  35. // Distinct wraps a variable-size array of `KeyValue`,
  36. // constructed with keys in sorted order. This can be used as
  37. // a map key or for equality checking between Sets.
  38. Distinct struct {
  39. iface interface{}
  40. }
  41. // Filter supports removing certain labels from label sets.
  42. // When the filter returns true, the label will be kept in
  43. // the filtered label set. When the filter returns false, the
  44. // label is excluded from the filtered label set, and the
  45. // label instead appears in the `removed` list of excluded labels.
  46. Filter func(KeyValue) bool
  47. // Sortable implements `sort.Interface`, used for sorting
  48. // `KeyValue`. This is an exported type to support a
  49. // memory optimization. A pointer to one of these is needed
  50. // for the call to `sort.Stable()`, which the caller may
  51. // provide in order to avoid an allocation. See
  52. // `NewSetWithSortable()`.
  53. Sortable []KeyValue
  54. )
  55. var (
  56. // keyValueType is used in `computeDistinctReflect`.
  57. keyValueType = reflect.TypeOf(KeyValue{})
  58. // emptySet is returned for empty label sets.
  59. emptySet = &Set{
  60. equivalent: Distinct{
  61. iface: [0]KeyValue{},
  62. },
  63. }
  64. )
  65. // EmptySet returns a reference to a Set with no elements.
  66. //
  67. // This is a convenience provided for optimized calling utility.
  68. func EmptySet() *Set {
  69. return emptySet
  70. }
  71. // reflect abbreviates `reflect.ValueOf`.
  72. func (d Distinct) reflect() reflect.Value {
  73. return reflect.ValueOf(d.iface)
  74. }
  75. // Valid returns true if this value refers to a valid `*Set`.
  76. func (d Distinct) Valid() bool {
  77. return d.iface != nil
  78. }
  79. // Len returns the number of labels in this set.
  80. func (l *Set) Len() int {
  81. if l == nil || !l.equivalent.Valid() {
  82. return 0
  83. }
  84. return l.equivalent.reflect().Len()
  85. }
  86. // Get returns the KeyValue at ordered position `idx` in this set.
  87. func (l *Set) Get(idx int) (KeyValue, bool) {
  88. if l == nil {
  89. return KeyValue{}, false
  90. }
  91. value := l.equivalent.reflect()
  92. if idx >= 0 && idx < value.Len() {
  93. // Note: The Go compiler successfully avoids an allocation for
  94. // the interface{} conversion here:
  95. return value.Index(idx).Interface().(KeyValue), true
  96. }
  97. return KeyValue{}, false
  98. }
  99. // Value returns the value of a specified key in this set.
  100. func (l *Set) Value(k Key) (Value, bool) {
  101. if l == nil {
  102. return Value{}, false
  103. }
  104. rValue := l.equivalent.reflect()
  105. vlen := rValue.Len()
  106. idx := sort.Search(vlen, func(idx int) bool {
  107. return rValue.Index(idx).Interface().(KeyValue).Key >= k
  108. })
  109. if idx >= vlen {
  110. return Value{}, false
  111. }
  112. keyValue := rValue.Index(idx).Interface().(KeyValue)
  113. if k == keyValue.Key {
  114. return keyValue.Value, true
  115. }
  116. return Value{}, false
  117. }
  118. // HasValue tests whether a key is defined in this set.
  119. func (l *Set) HasValue(k Key) bool {
  120. if l == nil {
  121. return false
  122. }
  123. _, ok := l.Value(k)
  124. return ok
  125. }
  126. // Iter returns an iterator for visiting the labels in this set.
  127. func (l *Set) Iter() Iterator {
  128. return Iterator{
  129. storage: l,
  130. idx: -1,
  131. }
  132. }
  133. // ToSlice returns the set of labels belonging to this set, sorted,
  134. // where keys appear no more than once.
  135. func (l *Set) ToSlice() []KeyValue {
  136. iter := l.Iter()
  137. return iter.ToSlice()
  138. }
  139. // Equivalent returns a value that may be used as a map key. The
  140. // Distinct type guarantees that the result will equal the equivalent
  141. // Distinct value of any label set with the same elements as this,
  142. // where sets are made unique by choosing the last value in the input
  143. // for any given key.
  144. func (l *Set) Equivalent() Distinct {
  145. if l == nil || !l.equivalent.Valid() {
  146. return emptySet.equivalent
  147. }
  148. return l.equivalent
  149. }
  150. // Equals returns true if the argument set is equivalent to this set.
  151. func (l *Set) Equals(o *Set) bool {
  152. return l.Equivalent() == o.Equivalent()
  153. }
  154. // Encoded returns the encoded form of this set, according to
  155. // `encoder`.
  156. func (l *Set) Encoded(encoder Encoder) string {
  157. if l == nil || encoder == nil {
  158. return ""
  159. }
  160. return encoder.Encode(l.Iter())
  161. }
  162. func empty() Set {
  163. return Set{
  164. equivalent: emptySet.equivalent,
  165. }
  166. }
  167. // NewSet returns a new `Set`. See the documentation for
  168. // `NewSetWithSortableFiltered` for more details.
  169. //
  170. // Except for empty sets, this method adds an additional allocation
  171. // compared with calls that include a `*Sortable`.
  172. func NewSet(kvs ...KeyValue) Set {
  173. // Check for empty set.
  174. if len(kvs) == 0 {
  175. return empty()
  176. }
  177. s, _ := NewSetWithSortableFiltered(kvs, new(Sortable), nil)
  178. return s
  179. }
  180. // NewSetWithSortable returns a new `Set`. See the documentation for
  181. // `NewSetWithSortableFiltered` for more details.
  182. //
  183. // This call includes a `*Sortable` option as a memory optimization.
  184. func NewSetWithSortable(kvs []KeyValue, tmp *Sortable) Set {
  185. // Check for empty set.
  186. if len(kvs) == 0 {
  187. return empty()
  188. }
  189. s, _ := NewSetWithSortableFiltered(kvs, tmp, nil)
  190. return s
  191. }
  192. // NewSetWithFiltered returns a new `Set`. See the documentation for
  193. // `NewSetWithSortableFiltered` for more details.
  194. //
  195. // This call includes a `Filter` to include/exclude label keys from
  196. // the return value. Excluded keys are returned as a slice of label
  197. // values.
  198. func NewSetWithFiltered(kvs []KeyValue, filter Filter) (Set, []KeyValue) {
  199. // Check for empty set.
  200. if len(kvs) == 0 {
  201. return empty(), nil
  202. }
  203. return NewSetWithSortableFiltered(kvs, new(Sortable), filter)
  204. }
  205. // NewSetWithSortableFiltered returns a new `Set`.
  206. //
  207. // Duplicate keys are eliminated by taking the last value. This
  208. // re-orders the input slice so that unique last-values are contiguous
  209. // at the end of the slice.
  210. //
  211. // This ensures the following:
  212. //
  213. // - Last-value-wins semantics
  214. // - Caller sees the reordering, but doesn't lose values
  215. // - Repeated call preserve last-value wins.
  216. //
  217. // Note that methods are defined on `*Set`, although this returns `Set`.
  218. // Callers can avoid memory allocations by:
  219. //
  220. // - allocating a `Sortable` for use as a temporary in this method
  221. // - allocating a `Set` for storing the return value of this
  222. // constructor.
  223. //
  224. // The result maintains a cache of encoded labels, by attribute.EncoderID.
  225. // This value should not be copied after its first use.
  226. //
  227. // The second `[]KeyValue` return value is a list of labels that were
  228. // excluded by the Filter (if non-nil).
  229. func NewSetWithSortableFiltered(kvs []KeyValue, tmp *Sortable, filter Filter) (Set, []KeyValue) {
  230. // Check for empty set.
  231. if len(kvs) == 0 {
  232. return empty(), nil
  233. }
  234. *tmp = kvs
  235. // Stable sort so the following de-duplication can implement
  236. // last-value-wins semantics.
  237. sort.Stable(tmp)
  238. *tmp = nil
  239. position := len(kvs) - 1
  240. offset := position - 1
  241. // The requirements stated above require that the stable
  242. // result be placed in the end of the input slice, while
  243. // overwritten values are swapped to the beginning.
  244. //
  245. // De-duplicate with last-value-wins semantics. Preserve
  246. // duplicate values at the beginning of the input slice.
  247. for ; offset >= 0; offset-- {
  248. if kvs[offset].Key == kvs[position].Key {
  249. continue
  250. }
  251. position--
  252. kvs[offset], kvs[position] = kvs[position], kvs[offset]
  253. }
  254. if filter != nil {
  255. return filterSet(kvs[position:], filter)
  256. }
  257. return Set{
  258. equivalent: computeDistinct(kvs[position:]),
  259. }, nil
  260. }
  261. // filterSet reorders `kvs` so that included keys are contiguous at
  262. // the end of the slice, while excluded keys precede the included keys.
  263. func filterSet(kvs []KeyValue, filter Filter) (Set, []KeyValue) {
  264. var excluded []KeyValue
  265. // Move labels that do not match the filter so
  266. // they're adjacent before calling computeDistinct().
  267. distinctPosition := len(kvs)
  268. // Swap indistinct keys forward and distinct keys toward the
  269. // end of the slice.
  270. offset := len(kvs) - 1
  271. for ; offset >= 0; offset-- {
  272. if filter(kvs[offset]) {
  273. distinctPosition--
  274. kvs[offset], kvs[distinctPosition] = kvs[distinctPosition], kvs[offset]
  275. continue
  276. }
  277. }
  278. excluded = kvs[:distinctPosition]
  279. return Set{
  280. equivalent: computeDistinct(kvs[distinctPosition:]),
  281. }, excluded
  282. }
  283. // Filter returns a filtered copy of this `Set`. See the
  284. // documentation for `NewSetWithSortableFiltered` for more details.
  285. func (l *Set) Filter(re Filter) (Set, []KeyValue) {
  286. if re == nil {
  287. return Set{
  288. equivalent: l.equivalent,
  289. }, nil
  290. }
  291. // Note: This could be refactored to avoid the temporary slice
  292. // allocation, if it proves to be expensive.
  293. return filterSet(l.ToSlice(), re)
  294. }
  295. // computeDistinct returns a `Distinct` using either the fixed- or
  296. // reflect-oriented code path, depending on the size of the input.
  297. // The input slice is assumed to already be sorted and de-duplicated.
  298. func computeDistinct(kvs []KeyValue) Distinct {
  299. iface := computeDistinctFixed(kvs)
  300. if iface == nil {
  301. iface = computeDistinctReflect(kvs)
  302. }
  303. return Distinct{
  304. iface: iface,
  305. }
  306. }
  307. // computeDistinctFixed computes a `Distinct` for small slices. It
  308. // returns nil if the input is too large for this code path.
  309. func computeDistinctFixed(kvs []KeyValue) interface{} {
  310. switch len(kvs) {
  311. case 1:
  312. ptr := new([1]KeyValue)
  313. copy((*ptr)[:], kvs)
  314. return *ptr
  315. case 2:
  316. ptr := new([2]KeyValue)
  317. copy((*ptr)[:], kvs)
  318. return *ptr
  319. case 3:
  320. ptr := new([3]KeyValue)
  321. copy((*ptr)[:], kvs)
  322. return *ptr
  323. case 4:
  324. ptr := new([4]KeyValue)
  325. copy((*ptr)[:], kvs)
  326. return *ptr
  327. case 5:
  328. ptr := new([5]KeyValue)
  329. copy((*ptr)[:], kvs)
  330. return *ptr
  331. case 6:
  332. ptr := new([6]KeyValue)
  333. copy((*ptr)[:], kvs)
  334. return *ptr
  335. case 7:
  336. ptr := new([7]KeyValue)
  337. copy((*ptr)[:], kvs)
  338. return *ptr
  339. case 8:
  340. ptr := new([8]KeyValue)
  341. copy((*ptr)[:], kvs)
  342. return *ptr
  343. case 9:
  344. ptr := new([9]KeyValue)
  345. copy((*ptr)[:], kvs)
  346. return *ptr
  347. case 10:
  348. ptr := new([10]KeyValue)
  349. copy((*ptr)[:], kvs)
  350. return *ptr
  351. default:
  352. return nil
  353. }
  354. }
  355. // computeDistinctReflect computes a `Distinct` using reflection,
  356. // works for any size input.
  357. func computeDistinctReflect(kvs []KeyValue) interface{} {
  358. at := reflect.New(reflect.ArrayOf(len(kvs), keyValueType)).Elem()
  359. for i, keyValue := range kvs {
  360. *(at.Index(i).Addr().Interface().(*KeyValue)) = keyValue
  361. }
  362. return at.Interface()
  363. }
  364. // MarshalJSON returns the JSON encoding of the `*Set`.
  365. func (l *Set) MarshalJSON() ([]byte, error) {
  366. return json.Marshal(l.equivalent.iface)
  367. }
  368. // Len implements `sort.Interface`.
  369. func (l *Sortable) Len() int {
  370. return len(*l)
  371. }
  372. // Swap implements `sort.Interface`.
  373. func (l *Sortable) Swap(i, j int) {
  374. (*l)[i], (*l)[j] = (*l)[j], (*l)[i]
  375. }
  376. // Less implements `sort.Interface`.
  377. func (l *Sortable) Less(i, j int) bool {
  378. return (*l)[i].Key < (*l)[j].Key
  379. }