diff --git a/internal/graph/check.go b/internal/graph/check.go index 0acb216a30..04abc668d7 100644 --- a/internal/graph/check.go +++ b/internal/graph/check.go @@ -6,7 +6,6 @@ import ( "fmt" "time" - "github.com/google/uuid" "github.com/prometheus/client_golang/prometheus" "github.com/samber/lo" "go.opentelemetry.io/otel" @@ -128,7 +127,7 @@ func (cc *ConcurrentChecker) Check(ctx context.Context, req ValidatedCheckReques if debugInfo == nil { debugInfo = &v1.DebugInformation{ Check: &v1.CheckDebugTrace{ - TraceId: uuid.NewString(), + TraceId: NewTraceID(), SourceId: nodeID, }, } @@ -1328,7 +1327,7 @@ func combineResponseMetadata(ctx context.Context, existing *v1.ResponseMeta, res debugInfo := &v1.DebugInformation{ Check: &v1.CheckDebugTrace{ - TraceId: uuid.NewString(), + TraceId: NewTraceID(), SourceId: nodeID, }, } diff --git a/internal/graph/traceid.go b/internal/graph/traceid.go new file mode 100644 index 0000000000..e275bc5adb --- /dev/null +++ b/internal/graph/traceid.go @@ -0,0 +1,13 @@ +package graph + +import ( + "github.com/google/uuid" +) + +// NewTraceID generates a new trace ID. The trace IDs will only be unique with +// a single dispatch request tree and should not be used for any other purpose. +// This function currently uses the UUID library to generate a new trace ID, +// which means it should not be invoked from performance-critical code paths. +func NewTraceID() string { + return uuid.NewString() +} diff --git a/internal/services/v1/bulkcheck.go b/internal/services/v1/bulkcheck.go index e9bc656d3c..28dfa40fb8 100644 --- a/internal/services/v1/bulkcheck.go +++ b/internal/services/v1/bulkcheck.go @@ -7,7 +7,6 @@ import ( "time" v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" - "github.com/google/uuid" "github.com/jzelinskie/stringz" "google.golang.org/grpc/status" "google.golang.org/protobuf/types/known/durationpb" @@ -199,7 +198,7 @@ func (bc *bulkChecker) checkBulkPermissions(ctx context.Context, req *v1.CheckBu }, Results: localResults, Duration: durationpb.New(time.Duration(0)), - TraceId: uuid.New().String(), + TraceId: graph.NewTraceID(), SourceId: debugInfo.Check.SourceId, }, } diff --git a/pkg/middleware/nodeid/nodeid.go b/pkg/middleware/nodeid/nodeid.go index 2c8cea66f9..cea4a51521 100644 --- a/pkg/middleware/nodeid/nodeid.go +++ b/pkg/middleware/nodeid/nodeid.go @@ -2,8 +2,10 @@ package nodeid import ( "context" + "fmt" "os" + "github.com/cespare/xxhash/v2" middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2" "google.golang.org/grpc" ) @@ -40,7 +42,14 @@ func FromContext(ctx context.Context) (string, error) { if err != nil { return "", err } - defaultNodeID = spiceDBPrefix + hostname + + // Hash the hostname to get the final default node ID. + hasher := xxhash.New() + if _, err := hasher.WriteString(hostname); err != nil { + return "", fmt.Errorf("failed to hash hostname: %w", err) + } + + defaultNodeID = spiceDBPrefix + fmt.Sprintf("%x", hasher.Sum(nil)) } if err := setInContext(ctx, defaultNodeID); err != nil {