-
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.
feat: Add grpcLogger to gRPC server for request logging
- Loading branch information
Showing
4 changed files
with
134 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package gapi | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"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 | ||
} | ||
|
||
type ResponseRecorder struct { | ||
http.ResponseWriter | ||
StatusCode int | ||
Body []byte | ||
} | ||
|
||
func (rec *ResponseRecorder) WriteHeader(statusCode int) { | ||
rec.StatusCode = statusCode | ||
rec.ResponseWriter.WriteHeader(statusCode) | ||
} | ||
|
||
func (rec *ResponseRecorder) Write(body []byte) (int, error) { | ||
rec.Body = body | ||
return rec.ResponseWriter.Write(body) | ||
} | ||
|
||
func HttpLogger(handler http.Handler) http.Handler { | ||
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
startTime := time.Now() | ||
rec := &ResponseRecorder{ | ||
ResponseWriter: res, | ||
StatusCode: http.StatusOK, | ||
} | ||
handler.ServeHTTP(rec, req) | ||
duration := time.Since(startTime) | ||
|
||
logger := log.Info() | ||
if rec.StatusCode != http.StatusOK { | ||
logger = log.Error().Bytes("body", rec.Body) | ||
} | ||
|
||
logger.Str("protocol", "http"). | ||
Str("method", req.Method). | ||
Str("path", req.RequestURI). | ||
Int("status_code", rec.StatusCode). | ||
Str("status_text", http.StatusText(rec.StatusCode)). | ||
Dur("duration", duration). | ||
Msg("received a HTTP request") | ||
}) | ||
} |
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