Skip to content

Commit c14e431

Browse files
committed
connectserver: propagate request IDs
1 parent 7fa7588 commit c14e431

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

connectserver/connectserver.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,34 @@ func RequestID(ctx context.Context) string {
5353
func addRequestID() connect.Interceptor {
5454
return connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc {
5555
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
56+
const traceHeader = "x-trace-id"
5657
requestId := "req_" + ulid.Make().String()
57-
if head := req.Header().Get("x-trace-id"); head != "" {
58+
if head := req.Header().Get(traceHeader); head != "" {
5859
requestId = head
5960
}
6061
log := zerolog.Ctx(ctx).With().Str("requestId", requestId).Logger()
6162

6263
ctx = log.WithContext(ctx)
6364
ctx = context.WithValue(ctx, requestIdKey, requestId)
6465

65-
return next(ctx, req)
66+
res, err := next(ctx, req)
67+
68+
// Propagate the request ID back to the caller
69+
if err == nil {
70+
res.Header().Set(traceHeader, requestId)
71+
}
72+
73+
if err != nil {
74+
type withMeta interface {
75+
Meta() http.Header
76+
}
77+
78+
if err, ok := err.(withMeta); ok {
79+
err.Meta().Set(traceHeader, requestId)
80+
}
81+
}
82+
83+
return res, err
6684
}
6785
})
6886
}

0 commit comments

Comments
 (0)