@@ -6,6 +6,7 @@ package server
6
6
7
7
import (
8
8
"context"
9
+ "github.com/gitpod-io/gitpod/common-go/log"
9
10
10
11
"github.com/bufbuild/connect-go"
11
12
"github.com/sirupsen/logrus"
@@ -14,42 +15,38 @@ import (
14
15
func NewLogInterceptor (entry * logrus.Entry ) connect.UnaryInterceptorFunc {
15
16
interceptor := func (next connect.UnaryFunc ) connect.UnaryFunc {
16
17
return connect .UnaryFunc (func (ctx context.Context , req connect.AnyRequest ) (connect.AnyResponse , error ) {
17
- logger := entry .
18
- WithContext (ctx ).
19
- WithField ("protocol" , "connect" ).
20
- WithField ("procedure" , req .Spec ().Procedure ).
21
- WithField ("address" , req .Peer ().Addr ).
22
- WithField ("stream_type" , streamType (req .Spec ().StreamType ))
18
+ ctx = log .ToContext (ctx , entry .WithContext (ctx ))
23
19
24
- isClient := req .Spec ().IsClient
25
-
26
- if isClient {
27
- logger .WithField ("headers" , req .Header ()).Debugf ("Starting request for %s" , req .Spec ().Procedure )
28
- } else {
29
- logger .WithField ("headers" , req .Header ()).Debugf ("Handling request for %s" , req .Spec ().Procedure )
30
- }
20
+ log .AddFields (ctx , logrus.Fields {
21
+ "request.protocol" : "connect" ,
22
+ "request.procedure" : req .Spec ().Procedure ,
23
+ "address" : req .Peer ().Addr ,
24
+ "request.stream_type" : streamType (req .Spec ().StreamType ),
25
+ "request.headers" : req .Header (),
26
+ })
27
+ log .Extract (ctx ).Debugf ("Handling request for %s" , req .Spec ().Procedure )
31
28
32
29
resp , err := next (ctx , req )
30
+
31
+ // Retrieve the logger from the context again, in case it's been updated.
33
32
code := codeOf (err )
34
- logger = logger . WithField ( " code", code )
33
+ log . AddFields ( ctx , logrus. Fields { "response. code": code } )
35
34
36
35
if err != nil {
37
- logger = logger .WithError (err )
38
- if isClient {
39
- logger .Errorf ("Received response for %s with code %s" , req .Spec ().Procedure , code )
40
- } else {
41
- logger .Warnf ("Completed handling of request for %s with code %s" , req .Spec ().Procedure , code )
42
- }
36
+ log .AddFields (ctx , logrus.Fields {logrus .ErrorKey : err })
43
37
} else {
44
- if resp .Any () != nil {
45
- logger = logger .WithField ("response" , resp .Any ())
46
- }
38
+ log .AddFields (ctx , logrus.Fields {"response.body" : resp .Any ()})
39
+ }
47
40
48
- if isClient {
49
- logger .WithField ("response" , resp .Any ()).Debugf ("Received ok response for %s" , req .Spec ().Procedure )
41
+ if req .Spec ().IsClient {
42
+ if err != nil {
43
+ log .Extract (ctx ).Errorf ("Received response for %s with code %s" , req .Spec ().Procedure , code )
50
44
} else {
51
- logger . WithField ( "response" , resp . Any ()). Debugf ( "Completed handling of request for %s with code %s" , req .Spec ().Procedure , code )
45
+ log . Extract ( ctx ). Infof ( "Received response for %s with code %s" , req .Spec ().Procedure , code )
52
46
}
47
+ log .Extract (ctx ).Errorf ("Received response for %s with code %s" , req .Spec ().Procedure , code )
48
+ } else {
49
+ log .Extract (ctx ).Warnf ("Completed handling of request for %s with code %s" , req .Spec ().Procedure , code )
53
50
}
54
51
55
52
return resp , err
0 commit comments