Debian Codegraph @

attribute/hash.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package attribute // import "go.opentelemetry.io/otel/attribute"
5
6 import (
7 "fmt"
8 "reflect"
9
10 "go.opentelemetry.io/otel/attribute/internal/xxhash"
11 )
12
13 // Type identifiers. These identifiers are hashed before the value of the
14 // corresponding type. This is done to distinguish values that are hashed with
15 // the same value representation (e.g. `int64(1)` and `true`, []int64{0} and
16 // int64(0)).
17 //
18 // These are all 8 byte length strings converted to a uint64 representation. A
19 // uint64 is used instead of the string directly as an optimization, it avoids
20 // the for loop in [xxhash] which adds minor overhead.
21 const (
22 boolID uint64 = 7953749933313450591 // "_boolean" (little endian)
23 int64ID uint64 = 7592915492740740150 // "64_bit_i" (little endian)
24 float64ID uint64 = 7376742710626956342 // "64_bit_f" (little endian)
25 stringID uint64 = 6874584755375207263 // "_string_" (little endian)
26 boolSliceID uint64 = 6875993255270243167 // "_[]bool_" (little endian)
27 int64SliceID uint64 = 3762322556277578591 // "_[]int64" (little endian)
28 float64SliceID uint64 = 7308324551835016539 // "[]double" (little endian)
29 stringSliceID uint64 = 7453010373645655387 // "[]string" (little endian)
30 emptyID uint64 = 7305809155345288421 // "__empty_" (little endian)
31 )
32
33 // hashKVs returns a new xxHash64 hash of kvs.
34 func hashKVs(kvs []KeyValue) uint64 {
35 h := xxhash.New()
36 for _, kv := range kvs {
37 h = hashKV(h, kv)
38 }
39 return h.Sum64()
40 }
41
42 // hashKV returns the xxHash64 hash of kv with h as the base.
43 func hashKV(h xxhash.Hash, kv KeyValue) xxhash.Hash {
44 h = h.String(string(kv.Key))
45
46 switch kv.Value.Type() {
47 case BOOL:
48 h = h.Uint64(boolID)
49 h = h.Uint64(kv.Value.numeric)
50 case INT64:
51 h = h.Uint64(int64ID)
52 h = h.Uint64(kv.Value.numeric)
53 case FLOAT64:
54 h = h.Uint64(float64ID)
55 // Assumes numeric stored with math.Float64bits.
56 h = h.Uint64(kv.Value.numeric)
57 case STRING:
58 h = h.Uint64(stringID)
59 h = h.String(kv.Value.stringly)
60 case BOOLSLICE:
61 h = h.Uint64(boolSliceID)
62 rv := reflect.ValueOf(kv.Value.slice)
63 for i := 0; i < rv.Len(); i++ {
64 h = h.Bool(rv.Index(i).Bool())
65 }
66 case INT64SLICE:
67 h = h.Uint64(int64SliceID)
68 rv := reflect.ValueOf(kv.Value.slice)
69 for i := 0; i < rv.Len(); i++ {
70 h = h.Int64(rv.Index(i).Int())
71 }
72 case FLOAT64SLICE:
73 h = h.Uint64(float64SliceID)
74 rv := reflect.ValueOf(kv.Value.slice)
75 for i := 0; i < rv.Len(); i++ {
76 h = h.Float64(rv.Index(i).Float())
77 }
78 case STRINGSLICE:
79 h = h.Uint64(stringSliceID)
80 rv := reflect.ValueOf(kv.Value.slice)
81 for i := 0; i < rv.Len(); i++ {
82 h = h.String(rv.Index(i).String())
83 }
84 case EMPTY:
85 h = h.Uint64(emptyID)
86 default:
87 // Logging is an alternative, but using the internal logger here
88 // causes an import cycle so it is not done.
89 v := kv.Value.AsInterface()
90 msg := fmt.Sprintf("unknown value type: %[1]v (%[1]T)", v)
91 panic(msg)
92 }
93 return h
94 }