Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more REST interceptors - logging and tracing #1623

Merged
merged 2 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions insonmnia/node/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ func newServer(cfg nodeConfig, services Services, options ...ServerOption) (*Ser
options := append([]rest.Option{
rest.WithLog(opts.log),
rest.WithInterceptors(
xgrpc.OpenTracingZapUnaryInterceptor(),
xgrpc.RequestLogUnaryInterceptor(map[string]bool{}),
xgrpc.VerifyUnaryInterceptor(),
services.Interceptor(),
),
Expand Down
11 changes: 4 additions & 7 deletions util/rest/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"go.uber.org/zap"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -66,14 +67,10 @@ func WithEncoder(e Encoder) Option {
}
}

func WithInterceptor(interceptor grpc.UnaryServerInterceptor) Option {
return func(o *options) {
o.interceptor = interceptor
}
}

func WithInterceptors(interceptors ...grpc.UnaryServerInterceptor) Option {
return func(o *options) {
o.interceptor = grpc_middleware.ChainUnaryServer(interceptors...)
o.interceptor = grpc_middleware.ChainUnaryServer(
append([]grpc.UnaryServerInterceptor{grpc_zap.UnaryServerInterceptor(o.log)}, interceptors...)...,
)
}
}
8 changes: 4 additions & 4 deletions util/xgrpc/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TraceInterceptor(tracer opentracing.Tracer) ServerOption {
return func(o *options) {
o.interceptors.u = append(o.interceptors.u,
grpc_opentracing.UnaryServerInterceptor(grpc_opentracing.WithTracer(tracer)),
openTracingZapUnaryInterceptor(),
OpenTracingZapUnaryInterceptor(),
)
o.interceptors.s = append(o.interceptors.s,
grpc_opentracing.StreamServerInterceptor(grpc_opentracing.WithTracer(tracer)),
Expand Down Expand Up @@ -160,7 +160,7 @@ func wrapZapContextWithWallet(ctx context.Context) context.Context {
return ctx
}

func openTracingZapUnaryInterceptor() grpc.UnaryServerInterceptor {
func OpenTracingZapUnaryInterceptor() grpc.UnaryServerInterceptor {
return contextUnaryInterceptor(wrapZapContextWithTracing)
}

Expand Down Expand Up @@ -264,12 +264,12 @@ func RequestLogInterceptor(truncatedMethods []string) ServerOption {
truncatedMethodsSet[method] = true
}
return func(o *options) {
o.interceptors.u = append(o.interceptors.u, requestLogUnaryInterceptor(truncatedMethodsSet))
o.interceptors.u = append(o.interceptors.u, RequestLogUnaryInterceptor(truncatedMethodsSet))
o.interceptors.s = append(o.interceptors.s, requestLogStreamInterceptor(truncatedMethodsSet))
}
}

func requestLogUnaryInterceptor(truncatedMethods map[string]bool) grpc.UnaryServerInterceptor {
func RequestLogUnaryInterceptor(truncatedMethods map[string]bool) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
executeRequestLogging(ctx, req, info.FullMethod, truncatedMethods[MethodInfo(info.FullMethod).Method])
return handler(ctx, req)
Expand Down