From 34b81593a9576b5feaae7718e2ba018b02a7f0fa Mon Sep 17 00:00:00 2001 From: Alec Merdler Date: Wed, 20 Mar 2024 16:26:03 -0400 Subject: [PATCH] RequestID utilities Adds function to inject a request ID into the gRPC headers when making a request to SpiceDB. This will be used to enable the '--request-id' flag in the 'zed' CLI. --- pkg/requestmeta/requestmeta.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkg/requestmeta/requestmeta.go b/pkg/requestmeta/requestmeta.go index 3c946c3..d6ade2d 100644 --- a/pkg/requestmeta/requestmeta.go +++ b/pkg/requestmeta/requestmeta.go @@ -29,6 +29,13 @@ const ( // the New Enemy Problem. This is only used with the CockroachDB datastore, // and only if user-provided request overlap is enabled. RequestOverlapKey RequestMetadataHeaderKey = "io.spicedb.requestoverlapkey" + + // RequestIDKey, if specified in a request header, will propagate the given string value + // through SpiceDB for the lifetime of the request. This can be used to correlate logs + // and traces with a specific request. + // + // TODO(alecmerdler): This is duplicated in SpiceDB source; we should be importing it from here. + RequestIDKey RequestMetadataHeaderKey = "x-request-id" ) // AddRequestHeaders returns a new context with the given values as request headers. @@ -54,3 +61,7 @@ func SetRequestHeaders(ctx context.Context, values map[RequestMetadataHeaderKey] func WithOverlapKey(ctx context.Context, key string) context.Context { return metadata.AppendToOutgoingContext(ctx, string(RequestOverlapKey), key) } + +func WithRequestID(ctx context.Context, requestID string) context.Context { + return metadata.AppendToOutgoingContext(ctx, string(RequestIDKey), requestID) +}