2020-04-11 15:49:41 +02:00
|
|
|
package trace
|
|
|
|
|
|
|
|
import (
|
2024-09-08 17:34:39 +02:00
|
|
|
"crypto/rand"
|
2020-04-11 15:49:41 +02:00
|
|
|
"encoding/base64"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/zrepl/zrepl/util/envconst"
|
|
|
|
)
|
|
|
|
|
|
|
|
var genIdNumBytes = envconst.Int("ZREPL_TRACE_ID_NUM_BYTES", 3)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if genIdNumBytes < 1 {
|
|
|
|
panic("trace node id byte length must be at least 1")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func genID() string {
|
|
|
|
var out strings.Builder
|
|
|
|
enc := base64.NewEncoder(base64.RawStdEncoding, &out)
|
|
|
|
buf := make([]byte, genIdNumBytes)
|
2024-09-08 17:34:39 +02:00
|
|
|
_, err := rand.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2020-04-11 15:49:41 +02:00
|
|
|
}
|
|
|
|
n, err := enc.Write(buf[:])
|
|
|
|
if err != nil || n != len(buf) {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := enc.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|