set.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. if filter != nil {
  249. return filterSet(kvs[position:], filter)
  250. }
  251. return Set{
  252. equivalent: computeDistinct(kvs[position:]),
  253. }, nil
  254. }
  255. // filterSet reorders kvs so that included keys are contiguous at the end of
  256. // the slice, while excluded keys precede the included keys.
  257. func filterSet(kvs []KeyValue, filter Filter) (Set, []KeyValue) {
  258. var excluded []KeyValue
  259. // Move attributes that do not match the filter so they're adjacent before
  260. // calling computeDistinct().
  261. distinctPosition := len(kvs)
  262. // Swap indistinct keys forward and distinct keys toward the
  263. // end of the slice.
  264. offset := len(kvs) - 1
  265. for ; offset >= 0; offset-- {
  266. if filter(kvs[offset]) {
  267. distinctPosition--
  268. kvs[offset], kvs[distinctPosition] = kvs[distinctPosition], kvs[offset]
  269. continue
  270. }
  271. }
  272. excluded = kvs[:distinctPosition]
  273. return Set{
  274. equivalent: computeDistinct(kvs[distinctPosition:]),
  275. }, excluded
  276. }
  277. // Filter returns a filtered copy of this Set. See the documentation for
  278. // NewSetWithSortableFiltered for more details.
  279. func (l *Set) Filter(re Filter) (Set, []KeyValue) {
  280. if re == nil {
  281. return Set{
  282. equivalent: l.equivalent,
  283. }, nil
  284. }
  285. // Note: This could be refactored to avoid the temporary slice
  286. // allocation, if it proves to be expensive.
  287. return filterSet(l.ToSlice(), re)
  288. }
  289. // computeDistinct returns a Distinct using either the fixed- or
  290. // reflect-oriented code path, depending on the size of the input. The input
  291. // slice is assumed to already be sorted and de-duplicated.
  292. func computeDistinct(kvs []KeyValue) Distinct {
  293. iface := computeDistinctFixed(kvs)
  294. if iface == nil {
  295. iface = computeDistinctReflect(kvs)
  296. }
  297. return Distinct{
  298. iface: iface,
  299. }
  300. }
  301. // computeDistinctFixed computes a Distinct for small slices. It returns nil
  302. // if the input is too large for this code path.
  303. func computeDistinctFixed(kvs []KeyValue) interface{} {
  304. switch len(kvs) {
  305. case 1:
  306. ptr := new([1]KeyValue)
  307. copy((*ptr)[:], kvs)
  308. return *ptr
  309. case 2:
  310. ptr := new([2]KeyValue)
  311. copy((*ptr)[:], kvs)
  312. return *ptr
  313. case 3:
  314. ptr := new([3]KeyValue)
  315. copy((*ptr)[:], kvs)
  316. return *ptr
  317. case 4:
  318. ptr := new([4]KeyValue)
  319. copy((*ptr)[:], kvs)
  320. return *ptr
  321. case 5:
  322. ptr := new([5]KeyValue)
  323. copy((*ptr)[:], kvs)
  324. return *ptr
  325. case 6:
  326. ptr := new([6]KeyValue)
  327. copy((*ptr)[:], kvs)
  328. return *ptr
  329. case 7:
  330. ptr := new([7]KeyValue)
  331. copy((*ptr)[:], kvs)
  332. return *ptr
  333. case 8:
  334. ptr := new([8]KeyValue)
  335. copy((*ptr)[:], kvs)
  336. return *ptr
  337. case 9:
  338. ptr := new([9]KeyValue)
  339. copy((*ptr)[:], kvs)
  340. return *ptr
  341. case 10:
  342. ptr := new([10]KeyValue)
  343. copy((*ptr)[:], kvs)
  344. return *ptr
  345. default:
  346. return nil
  347. }
  348. }
  349. // computeDistinctReflect computes a Distinct using reflection, works for any
  350. // size input.
  351. func computeDistinctReflect(kvs []KeyValue) interface{} {
  352. at := reflect.New(reflect.ArrayOf(len(kvs), keyValueType)).Elem()
  353. for i, keyValue := range kvs {
  354. *(at.Index(i).Addr().Interface().(*KeyValue)) = keyValue
  355. }
  356. return at.Interface()
  357. }
  358. // MarshalJSON returns the JSON encoding of the Set.
  359. func (l *Set) MarshalJSON() ([]byte, error) {
  360. return json.Marshal(l.equivalent.iface)
  361. }
  362. // MarshalLog is the marshaling function used by the logging system to represent this exporter.
  363. func (l Set) MarshalLog() interface{} {
  364. kvs := make(map[string]string)
  365. for _, kv := range l.ToSlice() {
  366. kvs[string(kv.Key)] = kv.Value.Emit()
  367. }
  368. return kvs
  369. }
  370. // Len implements sort.Interface.
  371. func (l *Sortable) Len() int {
  372. return len(*l)
  373. }
  374. // Swap implements sort.Interface.
  375. func (l *Sortable) Swap(i, j int) {
  376. (*l)[i], (*l)[j] = (*l)[j], (*l)[i]
  377. }
  378. // Less implements sort.Interface.
  379. func (l *Sortable) Less(i, j int) bool {
  380. return (*l)[i].Key < (*l)[j].Key
  381. }