Skip to content

Commit

Permalink
Make server decide the value of request-id.
Browse files Browse the repository at this point in the history
Also, use header rather than trailer.
  • Loading branch information
blkt committed Oct 15, 2024
1 parent f48342c commit f2b1f94
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 29 deletions.
4 changes: 3 additions & 1 deletion internal/controlplane/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 6 additions & 28 deletions internal/logger/logging_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit f2b1f94

Please sign in to comment.