-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(admin): replace RPC logging interceptor (#508)
### What this PR does This change replaces the RPC logging interceptor of admin from `github.com/grpc-ecosystem/go-grpc-middleware` with our simple interceptor. It makes the interceptor simple and lightweight.
- Loading branch information
Showing
35 changed files
with
63 additions
and
2,212 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
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,57 @@ | ||
package logging | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/peer" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
// UnaryServerInterceptor returns a new unary server interceptor that logs a | ||
// gRPC error. | ||
func UnaryServerInterceptor(logger *zap.Logger) grpc.UnaryServerInterceptor { | ||
if logger == nil { | ||
return func(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { | ||
return handler(ctx, req) | ||
} | ||
} | ||
|
||
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { | ||
start := time.Now() | ||
resp, err = handler(ctx, req) | ||
if err != nil { | ||
duration := time.Since(start) | ||
logger.Error(info.FullMethod, | ||
zap.Stringer("code", status.Code(err)), | ||
zap.Duration("duration", duration), | ||
zap.Stringer("request", req.(fmt.Stringer)), | ||
zap.Stringer("response", resp.(fmt.Stringer)), | ||
zap.String("peer", peerAddr(ctx)), | ||
zap.Error(err), | ||
) | ||
} | ||
return resp, err | ||
} | ||
} | ||
|
||
func peerAddr(ctx context.Context) string { | ||
p, ok := peer.FromContext(ctx) | ||
if !ok { | ||
return "" | ||
} | ||
|
||
host, _, err := net.SplitHostPort(p.Addr.String()) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
if host == "" { | ||
return "127.0.0.1" | ||
} | ||
return host | ||
} |
204 changes: 0 additions & 204 deletions
204
vendor/github.com/grpc-ecosystem/go-grpc-middleware/.gitignore
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.