Skip to content

Commit

Permalink
Merge pull request #1829 from alecmerdler/requestid-import-from-authz…
Browse files Browse the repository at this point in the history
…ed-go

Import request ID metadata key from `authzed-go`
  • Loading branch information
vroldanbet authored Mar 21, 2024
2 parents e7fde37 + 6ee8644 commit 3fff19f
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
toolchain go1.21.1

require (
github.com/authzed/authzed-go v0.11.1
github.com/authzed/authzed-go v0.11.2-0.20240320204618-9622b72a72c6
github.com/authzed/grpcutil v0.0.0-20240123092924-129dc0a6a6e1
github.com/authzed/spicedb v1.29.5
github.com/brianvoe/gofakeit/v6 v6.23.2
Expand Down
4 changes: 2 additions & 2 deletions e2e/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ github.com/Masterminds/squirrel v1.5.4 h1:uUcX/aBc8O7Fg9kaISIUsHXdKuqehiXAMQTYX8
github.com/Masterminds/squirrel v1.5.4/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9 h1:goHVqTbFX3AIo0tzGr14pgfAW2ZfPChKO21Z9MGf/gk=
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230512164433-5d1fd1a340c9/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM=
github.com/authzed/authzed-go v0.11.1 h1:N5CoDgF3Y28oESncviWEa7quGcrpXwe2KbYR4WCeg0M=
github.com/authzed/authzed-go v0.11.1/go.mod h1:w3Q8IbTR2raCDGIWCj2UHXxhQuhmpRPYNRutZjgUkXM=
github.com/authzed/authzed-go v0.11.2-0.20240320204618-9622b72a72c6 h1:zpdDybjx+3fyTf7UU2F6y/rkZuiylhUcNhkvewqn2Gs=
github.com/authzed/authzed-go v0.11.2-0.20240320204618-9622b72a72c6/go.mod h1:w3Q8IbTR2raCDGIWCj2UHXxhQuhmpRPYNRutZjgUkXM=
github.com/authzed/cel-go v0.17.5 h1:lfpkNrR99B5QRHg5qdG9oLu/kguVlZC68VJuMk8tH9Y=
github.com/authzed/cel-go v0.17.5/go.mod h1:XL/zEq5hKGVF8aOdMbG7w+BQPihLjY2W8N+UIygDA2I=
github.com/authzed/grpcutil v0.0.0-20240123092924-129dc0a6a6e1 h1:zBfQzia6Hz45pJBeURTrv1b6HezmejB6UmiGuBilHZM=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
contrib.go.opencensus.io/exporter/prometheus v0.4.2
github.com/IBM/pgxpoolprometheus v1.1.1
github.com/Masterminds/squirrel v1.5.4
github.com/authzed/authzed-go v0.11.1
github.com/authzed/authzed-go v0.11.2-0.20240320204618-9622b72a72c6
github.com/authzed/cel-go v0.17.5
github.com/authzed/consistent v0.1.0
github.com/authzed/grpcutil v0.0.0-20240123092924-129dc0a6a6e1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ github.com/ashanbrown/forbidigo v1.6.0 h1:D3aewfM37Yb3pxHujIPSpTf6oQk9sc9WZi8ger
github.com/ashanbrown/forbidigo v1.6.0/go.mod h1:Y8j9jy9ZYAEHXdu723cUlraTqbzjKF1MUyfOKL+AjcU=
github.com/ashanbrown/makezero v1.1.1 h1:iCQ87C0V0vSyO+M9E/FZYbu65auqH0lnsOkf5FcB28s=
github.com/ashanbrown/makezero v1.1.1/go.mod h1:i1bJLCRSCHOcOa9Y6MyF2FTfMZMFdHvxKHxgO5Z1axI=
github.com/authzed/authzed-go v0.11.1 h1:N5CoDgF3Y28oESncviWEa7quGcrpXwe2KbYR4WCeg0M=
github.com/authzed/authzed-go v0.11.1/go.mod h1:w3Q8IbTR2raCDGIWCj2UHXxhQuhmpRPYNRutZjgUkXM=
github.com/authzed/authzed-go v0.11.2-0.20240320204618-9622b72a72c6 h1:zpdDybjx+3fyTf7UU2F6y/rkZuiylhUcNhkvewqn2Gs=
github.com/authzed/authzed-go v0.11.2-0.20240320204618-9622b72a72c6/go.mod h1:w3Q8IbTR2raCDGIWCj2UHXxhQuhmpRPYNRutZjgUkXM=
github.com/authzed/cel-go v0.17.5 h1:lfpkNrR99B5QRHg5qdG9oLu/kguVlZC68VJuMk8tH9Y=
github.com/authzed/cel-go v0.17.5/go.mod h1:XL/zEq5hKGVF8aOdMbG7w+BQPihLjY2W8N+UIygDA2I=
github.com/authzed/consistent v0.1.0 h1:tlh1wvKoRbjRhMm2P+X5WQQyR54SRoS4MyjLOg17Mp8=
Expand Down
10 changes: 6 additions & 4 deletions pkg/cmd/server/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"

"github.com/authzed/authzed-go/pkg/requestmeta"

"github.com/authzed/spicedb/internal/dispatch"
"github.com/authzed/spicedb/internal/logging"
consistencymw "github.com/authzed/spicedb/internal/middleware/consistency"
Expand Down Expand Up @@ -208,7 +210,7 @@ func DefaultUnaryMiddleware(opts MiddlewareOption) (*MiddlewareChain[grpc.UnaryS

NewUnaryMiddleware().
WithName(DefaultMiddlewareLog).
WithInterceptor(logmw.UnaryServerInterceptor(logmw.ExtractMetadataField("x-request-id", "requestID"))).
WithInterceptor(logmw.UnaryServerInterceptor(logmw.ExtractMetadataField(string(requestmeta.RequestIDKey), "requestID"))).
Done(),

NewUnaryMiddleware().
Expand Down Expand Up @@ -286,7 +288,7 @@ func DefaultStreamingMiddleware(opts MiddlewareOption) (*MiddlewareChain[grpc.St

NewStreamMiddleware().
WithName(DefaultMiddlewareLog).
WithInterceptor(logmw.StreamServerInterceptor(logmw.ExtractMetadataField("x-request-id", "requestID"))).
WithInterceptor(logmw.StreamServerInterceptor(logmw.ExtractMetadataField(string(requestmeta.RequestIDKey), "requestID"))).
Done(),

NewStreamMiddleware().
Expand Down Expand Up @@ -373,7 +375,7 @@ func DefaultDispatchMiddleware(logger zerolog.Logger, authFunc grpcauth.AuthFunc
grpcMetricsUnaryInterceptor, grpcMetricsStreamingInterceptor := GRPCMetrics(disableGRPCLatencyHistogram)
return []grpc.UnaryServerInterceptor{
requestid.UnaryServerInterceptor(requestid.GenerateIfMissing(true)),
logmw.UnaryServerInterceptor(logmw.ExtractMetadataField("x-request-id", "requestID")),
logmw.UnaryServerInterceptor(logmw.ExtractMetadataField(string(requestmeta.RequestIDKey), "requestID")),
otelgrpc.UnaryServerInterceptor(), // nolint: staticcheck
grpclog.UnaryServerInterceptor(InterceptorLogger(logger), defaultCodeToLevel, durationFieldOption, traceIDFieldOption),
grpcMetricsUnaryInterceptor,
Expand All @@ -382,7 +384,7 @@ func DefaultDispatchMiddleware(logger zerolog.Logger, authFunc grpcauth.AuthFunc
servicespecific.UnaryServerInterceptor,
}, []grpc.StreamServerInterceptor{
requestid.StreamServerInterceptor(requestid.GenerateIfMissing(true)),
logmw.StreamServerInterceptor(logmw.ExtractMetadataField("x-request-id", "requestID")),
logmw.StreamServerInterceptor(logmw.ExtractMetadataField(string(requestmeta.RequestIDKey), "requestID")),
otelgrpc.StreamServerInterceptor(), // nolint: staticcheck
grpclog.StreamServerInterceptor(InterceptorLogger(logger), defaultCodeToLevel, durationFieldOption, traceIDFieldOption),
grpcMetricsStreamingInterceptor,
Expand Down
10 changes: 5 additions & 5 deletions pkg/middleware/requestid/requestid.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (

log "github.com/authzed/spicedb/internal/logging"

"github.com/authzed/authzed-go/pkg/requestmeta"
"github.com/authzed/authzed-go/pkg/responsemeta"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
"github.com/rs/xid"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)

// MetadataKey is the key in which request IDs are passed to metadata.
const MetadataKey = "x-request-id"
const metadataKey = string(requestmeta.RequestIDKey)

// Option instances control how the middleware is initialized.
type Option func(*handleRequestID)
Expand Down Expand Up @@ -47,7 +47,7 @@ func (r *handleRequestID) ServerReporter(ctx context.Context, _ interceptors.Cal
md, ok := metadata.FromIncomingContext(ctx)
if ok {
var requestIDs []string
requestIDs, haveRequestID = md[MetadataKey]
requestIDs, haveRequestID = md[metadataKey]
if haveRequestID {
requestID = requestIDs[0]
}
Expand All @@ -61,12 +61,12 @@ func (r *handleRequestID) ServerReporter(ctx context.Context, _ interceptors.Cal
md = metadata.New(nil)
}

md.Set(MetadataKey, requestID)
md.Set(metadataKey, requestID)
ctx = metadata.NewIncomingContext(ctx, md)
}

if haveRequestID {
ctx = metadata.AppendToOutgoingContext(ctx, MetadataKey, requestID)
ctx = metadata.AppendToOutgoingContext(ctx, metadataKey, requestID)
err := responsemeta.SetResponseHeaderMetadata(ctx, map[responsemeta.ResponseMetadataHeaderKey]string{
responsemeta.RequestID: requestID,
})
Expand Down

0 comments on commit 3fff19f

Please sign in to comment.