forked from techschool/simplebank
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add gRPC logger interceptor (techschool#63)
* add gRPC logger interceptor * add back error to the log * update new lecture link Co-authored-by: phamlequang <phamlequang@gmail.com>
- Loading branch information
1 parent
114a6fc
commit cb29a1d
Showing
7 changed files
with
90 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func GrpcLogger( | ||
ctx context.Context, | ||
req interface{}, | ||
info *grpc.UnaryServerInfo, | ||
handler grpc.UnaryHandler, | ||
) (resp interface{}, err error) { | ||
startTime := time.Now() | ||
result, err := handler(ctx, req) | ||
duration := time.Since(startTime) | ||
|
||
statusCode := codes.Unknown | ||
if st, ok := status.FromError(err); ok { | ||
statusCode = st.Code() | ||
} | ||
|
||
logger := log.Info() | ||
if err != nil { | ||
logger = log.Error().Err(err) | ||
} | ||
|
||
logger.Str("protocol", "grpc"). | ||
Str("method", info.FullMethod). | ||
Int("status_code", int(statusCode)). | ||
Str("status_text", statusCode.String()). | ||
Dur("duration", duration). | ||
Msg("received a gRPC request") | ||
|
||
return result, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters