File tree Expand file tree Collapse file tree 1 file changed +20
-2
lines changed Expand file tree Collapse file tree 1 file changed +20
-2
lines changed Original file line number Diff line number Diff line change @@ -53,16 +53,34 @@ func RequestID(ctx context.Context) string {
53
53
func addRequestID () connect.Interceptor {
54
54
return connect .UnaryInterceptorFunc (func (next connect.UnaryFunc ) connect.UnaryFunc {
55
55
return func (ctx context.Context , req connect.AnyRequest ) (connect.AnyResponse , error ) {
56
+ const traceHeader = "x-trace-id"
56
57
requestId := "req_" + ulid .Make ().String ()
57
- if head := req .Header ().Get ("x-trace-id" ); head != "" {
58
+ if head := req .Header ().Get (traceHeader ); head != "" {
58
59
requestId = head
59
60
}
60
61
log := zerolog .Ctx (ctx ).With ().Str ("requestId" , requestId ).Logger ()
61
62
62
63
ctx = log .WithContext (ctx )
63
64
ctx = context .WithValue (ctx , requestIdKey , requestId )
64
65
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
66
84
}
67
85
})
68
86
}
You can’t perform that action at this time.
0 commit comments