chore: trace spans: use crypto/rand for generating them

math/rand.Read is deprecated in newer Go versions.

Also, it appears that crypto/rand is faster when used from multiple
goroutines: https://gist.github.com/problame/0699acd6f99db4163f26f0b8a61569f3
This commit is contained in:
Christian Schwarz 2024-09-08 15:34:39 +00:00
parent 0ab92d4861
commit 3cb1865909
2 changed files with 4 additions and 14 deletions

View File

@ -3,7 +3,6 @@ package daemon
import ( import (
"context" "context"
"fmt" "fmt"
"math/rand"
"os" "os"
"os/signal" "os/signal"
"strings" "strings"
@ -39,12 +38,6 @@ func Run(ctx context.Context, conf *config.Config) error {
cancel() cancel()
}() }()
// The math/rand package is used presently for generating trace IDs, we
// seed it with the current time and pid so that the IDs are mostly
// unique.
rand.Seed(time.Now().UnixNano())
rand.Seed(int64(os.Getpid()))
outlets, err := logging.OutletsFromConfig(*conf.Global.Logging) outlets, err := logging.OutletsFromConfig(*conf.Global.Logging)
if err != nil { if err != nil {
return errors.Wrap(err, "cannot build logging from config") return errors.Wrap(err, "cannot build logging from config")

View File

@ -1,8 +1,8 @@
package trace package trace
import ( import (
"crypto/rand"
"encoding/base64" "encoding/base64"
"math/rand"
"strings" "strings"
"github.com/zrepl/zrepl/util/envconst" "github.com/zrepl/zrepl/util/envconst"
@ -20,13 +20,10 @@ func genID() string {
var out strings.Builder var out strings.Builder
enc := base64.NewEncoder(base64.RawStdEncoding, &out) enc := base64.NewEncoder(base64.RawStdEncoding, &out)
buf := make([]byte, genIdNumBytes) buf := make([]byte, genIdNumBytes)
for i := 0; i < len(buf); { _, err := rand.Read(buf)
n, err := rand.Read(buf[i:])
if err != nil { if err != nil {
panic(err) panic(err)
} }
i += n
}
n, err := enc.Write(buf[:]) n, err := enc.Write(buf[:])
if err != nil || n != len(buf) { if err != nil || n != len(buf) {
panic(err) panic(err)