-
Notifications
You must be signed in to change notification settings - Fork 77
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
Add a gRPC interceptor for embedding payloads into trace spans #900
Changes from all commits
533b12b
519d2b9
42f019b
0ca142d
a7ee29a
4e681d8
4f4924a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// Copyright (C) 2019-2021 vdaas.org vald team <[email protected]> | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
// Package recover provides gRPC interceptors for recovery | ||
package recover | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/vdaas/vald/internal/net/grpc" | ||
"github.com/vdaas/vald/internal/safety" | ||
) | ||
|
||
func RecoverInterceptor() grpc.UnaryServerInterceptor { | ||
return func( | ||
ctx context.Context, | ||
req interface{}, | ||
info *grpc.UnaryServerInfo, | ||
handler grpc.UnaryHandler, | ||
) (resp interface{}, err error) { | ||
err = safety.RecoverWithoutPanicFunc(func() (err error) { | ||
resp, err = handler(ctx, req) | ||
return err | ||
})() | ||
return resp, err | ||
} | ||
} | ||
|
||
func RecoverStreamInterceptor() grpc.StreamServerInterceptor { | ||
return func( | ||
srv interface{}, | ||
ss grpc.ServerStream, | ||
info *grpc.StreamServerInfo, | ||
handler grpc.StreamHandler, | ||
) error { | ||
return safety.RecoverWithoutPanicFunc(func() (err error) { | ||
return handler(srv, ss) | ||
})() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,30 +14,31 @@ | |
// limitations under the License. | ||
// | ||
|
||
// Package grpc provides generic functionality for grpc | ||
package grpc | ||
// Package recover provides gRPC interceptors for recovery | ||
package recover | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 |
||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/vdaas/vald/internal/errors" | ||
"github.com/vdaas/vald/internal/net/grpc" | ||
"go.uber.org/goleak" | ||
) | ||
|
||
func TestRecoverInterceptor(t *testing.T) { | ||
t.Parallel() | ||
type want struct { | ||
want UnaryServerInterceptor | ||
want grpc.UnaryServerInterceptor | ||
} | ||
type test struct { | ||
name string | ||
want want | ||
checkFunc func(want, UnaryServerInterceptor) error | ||
checkFunc func(want, grpc.UnaryServerInterceptor) error | ||
beforeFunc func() | ||
afterFunc func() | ||
} | ||
defaultCheckFunc := func(w want, got UnaryServerInterceptor) error { | ||
defaultCheckFunc := func(w want, got grpc.UnaryServerInterceptor) error { | ||
if !reflect.DeepEqual(got, w.want) { | ||
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) | ||
} | ||
|
@@ -84,23 +85,24 @@ func TestRecoverInterceptor(t *testing.T) { | |
if err := test.checkFunc(test.want, got); err != nil { | ||
tt.Errorf("error = %v", err) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 |
||
}) | ||
} | ||
} | ||
|
||
func TestRecoverStreamInterceptor(t *testing.T) { | ||
t.Parallel() | ||
type want struct { | ||
want StreamServerInterceptor | ||
want grpc.StreamServerInterceptor | ||
} | ||
type test struct { | ||
name string | ||
want want | ||
checkFunc func(want, StreamServerInterceptor) error | ||
checkFunc func(want, grpc.StreamServerInterceptor) error | ||
beforeFunc func() | ||
afterFunc func() | ||
} | ||
defaultCheckFunc := func(w want, got StreamServerInterceptor) error { | ||
defaultCheckFunc := func(w want, got grpc.StreamServerInterceptor) error { | ||
if !reflect.DeepEqual(got, w.want) { | ||
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) | ||
} | ||
|
@@ -147,6 +149,7 @@ func TestRecoverStreamInterceptor(t *testing.T) { | |
if err := test.checkFunc(test.want, got); err != nil { | ||
tt.Errorf("error = %v", err) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 |
||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// | ||
// Copyright (C) 2019-2021 vdaas.org vald team <[email protected]> | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
// Package trace provides gRPC interceptors for traces | ||
package trace | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"path" | ||
"sync" | ||
|
||
"github.com/vdaas/vald/internal/encoding/json" | ||
"github.com/vdaas/vald/internal/net/grpc" | ||
"github.com/vdaas/vald/internal/observability/trace" | ||
) | ||
|
||
const ( | ||
grpcKindUnary = "unary" | ||
grpcKindStream = "stream" | ||
|
||
traceAttrGRPCKind = "grpc.kind" | ||
traceAttrGRPCService = "grpc.service" | ||
traceAttrGRPCMethod = "grpc.method" | ||
|
||
traceAttrGRPCRequestPayload = "grpc.request.payload" | ||
traceAttrGRPCResponsePayload = "grpc.response.payload" | ||
) | ||
|
||
var ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 |
||
bufferPool = sync.Pool{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci] reported by reviewdog 🐶 |
||
New: func() interface{} { | ||
return &bytes.Buffer{} | ||
}, | ||
} | ||
) | ||
|
||
func TracePayloadInterceptor() grpc.UnaryServerInterceptor { | ||
return func( | ||
ctx context.Context, | ||
req interface{}, | ||
info *grpc.UnaryServerInfo, | ||
handler grpc.UnaryHandler, | ||
) (resp interface{}, err error) { | ||
span := trace.FromContext(ctx) | ||
if span == nil { | ||
return handler(ctx, req) | ||
} | ||
|
||
service, method := parseMethod(info.FullMethod) | ||
span.AddAttributes( | ||
trace.StringAttribute(traceAttrGRPCKind, grpcKindUnary), | ||
trace.StringAttribute(traceAttrGRPCService, service), | ||
trace.StringAttribute(traceAttrGRPCMethod, method), | ||
) | ||
|
||
if reqj := marshalJSON(req); reqj != "" { | ||
span.AddAttributes( | ||
trace.StringAttribute(traceAttrGRPCRequestPayload, reqj), | ||
) | ||
} | ||
|
||
resp, err = handler(ctx, req) | ||
|
||
if resj := marshalJSON(resp); resj != "" { | ||
span.AddAttributes( | ||
trace.StringAttribute(traceAttrGRPCResponsePayload, resj), | ||
) | ||
} | ||
|
||
return resp, err | ||
} | ||
} | ||
|
||
func TracePayloadStreamInterceptor() grpc.StreamServerInterceptor { | ||
return func( | ||
srv interface{}, | ||
ss grpc.ServerStream, | ||
info *grpc.StreamServerInfo, | ||
handler grpc.StreamHandler, | ||
) error { | ||
span := trace.FromContext(ss.Context()) | ||
if span == nil { | ||
return handler(srv, ss) | ||
} | ||
|
||
service, method := parseMethod(info.FullMethod) | ||
span.AddAttributes( | ||
trace.StringAttribute(traceAttrGRPCKind, grpcKindStream), | ||
trace.StringAttribute(traceAttrGRPCService, service), | ||
trace.StringAttribute(traceAttrGRPCMethod, method), | ||
) | ||
|
||
tss := &tracingServerStream{ | ||
ServerStream: ss, | ||
} | ||
|
||
err := handler(srv, tss) | ||
|
||
span.AddAttributes( | ||
trace.StringAttribute(traceAttrGRPCRequestPayload, tss.request), | ||
trace.StringAttribute(traceAttrGRPCResponsePayload, tss.response), | ||
) | ||
|
||
return err | ||
} | ||
} | ||
|
||
type tracingServerStream struct { | ||
grpc.ServerStream | ||
request string | ||
response string | ||
} | ||
|
||
func (tss *tracingServerStream) RecvMsg(m interface{}) error { | ||
err := tss.ServerStream.RecvMsg(m) | ||
if err == nil && tss.request == "" { | ||
if reqj := marshalJSON(m); reqj != "" { | ||
tss.request = reqj | ||
} | ||
} | ||
|
||
return err | ||
} | ||
|
||
func (tss *tracingServerStream) SendMsg(m interface{}) error { | ||
err := tss.ServerStream.SendMsg(m) | ||
if err == nil && tss.response == "" { | ||
if resj := marshalJSON(m); resj != "" { | ||
tss.response = resj | ||
} | ||
} | ||
|
||
return err | ||
} | ||
|
||
func parseMethod(fullMethod string) (service, method string) { | ||
service = path.Dir(fullMethod)[1:] | ||
method = path.Base(fullMethod) | ||
|
||
return service, method | ||
} | ||
|
||
func marshalJSON(pbMsg interface{}) string { | ||
b := bufferPool.Get().(*bytes.Buffer) | ||
defer bufferPool.Put(b) | ||
defer b.Reset() | ||
|
||
err := json.Encode(b, pbMsg) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
return b.String() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚫 [golangci] reported by reviewdog 🐶
package name recover has same name as predeclared identifier (predeclared)