// Copyright The OpenTelemetry Authors// SPDX-License-Identifier: Apache-2.0package attributeimport ()// Type identifiers. These identifiers are hashed before the value of the// corresponding type. This is done to distinguish values that are hashed with// the same value representation (e.g. `int64(1)` and `true`, []int64{0} and// int64(0)).//// These are all 8 byte length strings converted to a uint64 representation. A// uint64 is used instead of the string directly as an optimization, it avoids// the for loop in [xxhash] which adds minor overhead.const (boolIDuint64 = 7953749933313450591// "_boolean" (little endian)int64IDuint64 = 7592915492740740150// "64_bit_i" (little endian)float64IDuint64 = 7376742710626956342// "64_bit_f" (little endian)stringIDuint64 = 6874584755375207263// "_string_" (little endian)boolSliceIDuint64 = 6875993255270243167// "_[]bool_" (little endian)int64SliceIDuint64 = 3762322556277578591// "_[]int64" (little endian)float64SliceIDuint64 = 7308324551835016539// "[]double" (little endian)stringSliceIDuint64 = 7453010373645655387// "[]string" (little endian)byteSliceIDuint64 = 6874028470941080415// "_[]byte_" (little endian)sliceIDuint64 = 7883494272577650031// "__slice_" (little endian)mapIDuint64 = 6872316492666199903// "__map___" (little endian)emptyIDuint64 = 7305809155345288421// "__empty_" (little endian))// Hasher computes a Distinct value from KeyValue attributes supplied with// Write.//// A Hasher must be obtained from [NewHasher]. The zero value is not usable and// its methods will panic.typeHasherstruct {hxxhash.Hash}// NewHasher returns a new Hasher.func () *Hasher {return &Hasher{h: xxhash.New()}}// Reset resets h to its initial state so it can be reused.func ( *Hasher) () { .h.Reset()}// Write adds kv to the hash.//// Write requires attributes to be supplied in ascending key order with no// duplicate keys. To produce the same Distinct as Set.Equivalent, write// attributes in ascending key order, with no more than one value for each key.// If the source contains duplicate keys, retain the last value for each key// before calling Write.func ( *Hasher) ( KeyValue) {// hashKV mutates the digest h.h refers to in place and returns the same // Hash value it was passed. Discarding the result keeps the digest pointer // from flowing back into h, which would force the digest to be heap // allocated for every Hasher. Keeping Write this small also keeps it within // the inlining budget, which matters because hashKVs calls it per attribute. _ = hashKV(.h, )}// Distinct returns the identifier for the attributes written to h. When Write// is called as described above, it returns the same value as [Set.Equivalent].func ( *Hasher) () Distinct {// No count of written attributes is needed to detect the empty case. The // sum of a digest with nothing written to it is emptyHash, which is // non-zero (0xef46db3751d8e999), so it passes through remapZeroHash // unchanged and matches emptySet.Equivalent.returnDistinct{hash: remapZeroHash(.h.Sum64())}}// remapZeroHash remaps a 0 sum to a non-zero value, because hash == 0 is a// reserved sentinel (treated as empty/invalid).func ( uint64) uint64 {if == 0 {return1 }return}// hashKVs returns a new xxHash64 hash of kvs.//// This routes through [Hasher] so that Set hashing and Hasher cannot disagree:// there is exactly one implementation of how attributes are mixed and how the// final sum is framed.func ( []KeyValue) uint64 { := NewHasher()for , := range { .Write() }return .Distinct().hash}// hashKV returns the xxHash64 hash of kv with h as the base.func ( xxhash.Hash, KeyValue) xxhash.Hash { = .String(string(.Key))returnhashValue(, .Value)}func ( xxhash.Hash, Value) xxhash.Hash {switch .Type() {caseBOOL: = .Uint64(boolID) = .Uint64(.numeric)caseINT64: = .Uint64(int64ID) = .Uint64(.numeric)caseFLOAT64: = .Uint64(float64ID)// Assumes numeric stored with math.Float64bits. = .Uint64(.numeric)caseSTRING: = .Uint64(stringID) = .String(.stringly)caseBOOLSLICE: = .Uint64(boolSliceID)switch vals := .slice.(type) {case [0]bool:case [1]bool: = .Bool([0])case [2]bool: = .Bool([0]) = .Bool([1])case [3]bool: = .Bool([0]) = .Bool([1]) = .Bool([2])default: := reflect.ValueOf(.slice)for := 0; < .Len(); ++ { = .Bool(.Index().Bool()) } }caseINT64SLICE: = .Uint64(int64SliceID)switch vals := .slice.(type) {case [0]int64:case [1]int64: = .Int64([0])case [2]int64: = .Int64([0]) = .Int64([1])case [3]int64: = .Int64([0]) = .Int64([1]) = .Int64([2])default: := reflect.ValueOf(.slice)for := 0; < .Len(); ++ { = .Int64(.Index().Int()) } }caseFLOAT64SLICE: = .Uint64(float64SliceID)switch vals := .slice.(type) {case [0]float64:case [1]float64: = .Float64([0])case [2]float64: = .Float64([0]) = .Float64([1])case [3]float64: = .Float64([0]) = .Float64([1]) = .Float64([2])default: := reflect.ValueOf(.slice)for := 0; < .Len(); ++ { = .Float64(.Index().Float()) } }caseSTRINGSLICE: = .Uint64(stringSliceID)switch vals := .slice.(type) {case [0]string:case [1]string: = .String([0])case [2]string: = .String([0]) = .String([1])case [3]string: = .String([0]) = .String([1]) = .String([2])default: := reflect.ValueOf(.slice)for := 0; < .Len(); ++ { = .String(.Index().String()) } }caseBYTESLICE: = .Uint64(byteSliceID) = .String(.stringly)caseSLICE: = .Uint64(sliceID)switch vals := .slice.(type) {case [0]Value:// No values to hash, but the type identifier is still hashed above.case [1]Value: = (, [0])case [2]Value: = (, [0]) = (, [1])case [3]Value: = (, [0]) = (, [1]) = (, [2])case [4]Value: = (, [0]) = (, [1]) = (, [2]) = (, [3])case [5]Value: = (, [0]) = (, [1]) = (, [2]) = (, [3]) = (, [4])default: := reflect.ValueOf(.slice)for := 0; < .Len(); ++ { = (, .Index().Interface().(Value)) } }caseMAP: = .Uint64(mapID)switch vals := .slice.(type) {case [0]KeyValue:// No values to hash, but the type identifier is still hashed above.case [1]KeyValue: = .String(string([0].Key)) = (, [0].Value)case [2]KeyValue: = .String(string([0].Key)) = (, [0].Value) = .String(string([1].Key)) = (, [1].Value)case [3]KeyValue: = .String(string([0].Key)) = (, [0].Value) = .String(string([1].Key)) = (, [1].Value) = .String(string([2].Key)) = (, [2].Value)case [4]KeyValue: = .String(string([0].Key)) = (, [0].Value) = .String(string([1].Key)) = (, [1].Value) = .String(string([2].Key)) = (, [2].Value) = .String(string([3].Key)) = (, [3].Value)case [5]KeyValue: = .String(string([0].Key)) = (, [0].Value) = .String(string([1].Key)) = (, [1].Value) = .String(string([2].Key)) = (, [2].Value) = .String(string([3].Key)) = (, [3].Value) = .String(string([4].Key)) = (, [4].Value)default: := reflect.ValueOf(.slice)for := 0; < .Len(); ++ { := .Index().Interface().(KeyValue) = .String(string(.Key)) = (, .Value) } }caseEMPTY: = .Uint64(emptyID)default:// Logging is an alternative, but using the internal logger here // causes an import cycle so it is not done. := .AsInterface() := fmt.Sprintf("unknown value type: %[1]v (%[1]T)", )panic() }return}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.