mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-25 16:26:48 +00:00
27 lines
694 B
Go
27 lines
694 B
Go
package middleware
|
|
|
|
import (
|
|
"time"
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
"golang.org/x/net/context"
|
|
"google.golang.org/grpc"
|
|
|
|
"github.com/weaveworks/common/logging"
|
|
)
|
|
|
|
const gRPC = "gRPC"
|
|
|
|
// ServerLoggingInterceptor logs gRPC requests, errors and latency.
|
|
var ServerLoggingInterceptor = func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
|
begin := time.Now()
|
|
resp, err := handler(ctx, req)
|
|
entry := logging.With(ctx).WithFields(log.Fields{"method": info.FullMethod, "duration": time.Since(begin)})
|
|
if err != nil {
|
|
entry.WithError(err).Warn(gRPC)
|
|
} else {
|
|
entry.Debugf("%s (success)", gRPC)
|
|
}
|
|
return resp, err
|
|
}
|