34 lines
879 B
Go
34 lines
879 B
Go
package traceid
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// ID helps distinguish specific object with unique IDs in Go stack traces.
|
|
//
|
|
// Stack trace only prints integer arguments, so this ID is generated by converting a unique object ID to hex,
|
|
// and passing it explicitly as an argument to functions we want tagged. Just storing or logging it won't help.
|
|
type ID uint64
|
|
|
|
func (id ID) String() string {
|
|
return strconv.FormatUint(uint64(id), 16)
|
|
}
|
|
|
|
// FromString creates ID from 8-byte prefix of a string.
|
|
// It does not make any assumptions about string format.
|
|
func FromString(s string) ID {
|
|
var data [8]byte
|
|
copy(data[:], s)
|
|
return ID(binary.BigEndian.Uint64(data[:]))
|
|
}
|
|
|
|
// FromGUID creates ID from a GUID in the format TYPE_<SOMEID>.
|
|
func FromGUID(id string) ID {
|
|
if i := strings.IndexByte(id, '_'); i != -1 {
|
|
id = id[i+1:]
|
|
}
|
|
return FromString(id)
|
|
}
|