|
| 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/fnv" |
| 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 [fnv] 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 | +) |
| 31 | + |
| 32 | +// hashKVs returns a new FNV-1a hash of kvs. |
| 33 | +func hashKVs(kvs []KeyValue) fnv.Hash { |
| 34 | + h := fnv.New() |
| 35 | + for _, kv := range kvs { |
| 36 | + h = hashKV(h, kv) |
| 37 | + } |
| 38 | + return h |
| 39 | +} |
| 40 | + |
| 41 | +// hashKV returns the FNV-1a hash of kv with h as the base. |
| 42 | +func hashKV(h fnv.Hash, kv KeyValue) fnv.Hash { |
| 43 | + h = h.String(string(kv.Key)) |
| 44 | + |
| 45 | + switch kv.Value.Type() { |
| 46 | + case BOOL: |
| 47 | + h = h.Uint64(boolID) |
| 48 | + h = h.Uint64(kv.Value.numeric) |
| 49 | + case INT64: |
| 50 | + h = h.Uint64(int64ID) |
| 51 | + h = h.Uint64(kv.Value.numeric) |
| 52 | + case FLOAT64: |
| 53 | + h = h.Uint64(float64ID) |
| 54 | + // Assumes numeric stored with math.Float64bits. |
| 55 | + h = h.Uint64(kv.Value.numeric) |
| 56 | + case STRING: |
| 57 | + h = h.Uint64(stringID) |
| 58 | + h = h.String(kv.Value.stringly) |
| 59 | + case BOOLSLICE: |
| 60 | + h = h.Uint64(boolSliceID) |
| 61 | + rv := reflect.ValueOf(kv.Value.slice) |
| 62 | + for i := 0; i < rv.Len(); i++ { |
| 63 | + h = h.Bool(rv.Index(i).Bool()) |
| 64 | + } |
| 65 | + case INT64SLICE: |
| 66 | + h = h.Uint64(int64SliceID) |
| 67 | + rv := reflect.ValueOf(kv.Value.slice) |
| 68 | + for i := 0; i < rv.Len(); i++ { |
| 69 | + h = h.Int64(rv.Index(i).Int()) |
| 70 | + } |
| 71 | + case FLOAT64SLICE: |
| 72 | + h = h.Uint64(float64SliceID) |
| 73 | + rv := reflect.ValueOf(kv.Value.slice) |
| 74 | + for i := 0; i < rv.Len(); i++ { |
| 75 | + h = h.Float64(rv.Index(i).Float()) |
| 76 | + } |
| 77 | + case STRINGSLICE: |
| 78 | + h = h.Uint64(stringSliceID) |
| 79 | + rv := reflect.ValueOf(kv.Value.slice) |
| 80 | + for i := 0; i < rv.Len(); i++ { |
| 81 | + h = h.String(rv.Index(i).String()) |
| 82 | + } |
| 83 | + case INVALID: |
| 84 | + default: |
| 85 | + // Logging is an alternative, but using the internal logger here |
| 86 | + // causes an import cycle so it is not done. |
| 87 | + v := kv.Value.AsInterface() |
| 88 | + msg := fmt.Sprintf("unknown value type: %[1]v (%[1]T)", v) |
| 89 | + panic(msg) |
| 90 | + } |
| 91 | + return h |
| 92 | +} |
0 commit comments