diff --git a/internal/controlplane/server.go b/internal/controlplane/server.go index 707c979b4c..c107553cf8 100644 --- a/internal/controlplane/server.go +++ b/internal/controlplane/server.go @@ -250,8 +250,10 @@ func (s *Server) StartGRPCServer(ctx context.Context) error { interceptors := []grpc.UnaryServerInterceptor{ // TODO: this has no test coverage! util.SanitizingInterceptor(), + // This adds `Grpc-Metadata-Request-Id` to the + // response. + logger.RequestIDInterceptor("request-id"), logger.Interceptor(s.cfg.LoggingConfig), - logger.RequestIDInterceptor(), TokenValidationInterceptor, EntityContextProjectInterceptor, ProjectAuthorizationInterceptor, diff --git a/internal/logger/logging_interceptor.go b/internal/logger/logging_interceptor.go index b9c2ae4944..c0562f4a53 100644 --- a/internal/logger/logging_interceptor.go +++ b/internal/logger/logging_interceptor.go @@ -105,42 +105,20 @@ func Interceptor(cfg config.LoggingConfig) grpc.UnaryServerInterceptor { // RequestIDInterceptor traces request ids. // -// It tries to use the request id from the request context, creating a -// new one if that is missing. It also sends back in the trailer the -// request id, ensuring that the client receives it. -func RequestIDInterceptor() grpc.UnaryServerInterceptor { +// It's job is to add a request id (UUID) to the context so that all +// subsequent logs inherit it, making it easier to track down problems +// on a per-request basis. It also sends back it back in a header. +func RequestIDInterceptor(headerSuffix string) grpc.UnaryServerInterceptor { return func(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { - rID := maybeGetRequestID(ctx) + rID := uuid.New().String() ctx = zerolog.Ctx(ctx).With().Str("request_id", rID).Logger().WithContext(ctx) resp, err := handler(ctx, req) - if err := grpc.SetTrailer(ctx, metadata.Pairs("request-id", rID)); err != nil { + if err := grpc.SendHeader(ctx, metadata.Pairs(headerSuffix, rID)); err != nil { zerolog.Ctx(ctx).Trace().Err(err).Msg("unable to attach request id to trailer") } return resp, err } } - -func maybeGetRequestID(ctx context.Context) string { - var rID string - if md, ok := metadata.FromIncomingContext(ctx); ok { - if rIDs, ok := md["request-id"]; ok { - if len(rIDs) != 0 { - rID = rIDs[0] - } - } - } - - if rID == "" { - return uuid.New().String() - } - - if _, err := uuid.Parse(rID); err != nil { - zerolog.Ctx(ctx).Trace().Err(err).Msg("request id is not valid uuid") - return uuid.New().String() - } - - return rID -}