-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
328 additions
and
97 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,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) | ||
})() | ||
} | ||
} |
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,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 ( | ||
bufferPool = sync.Pool{ | ||
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() | ||
} |
Oops, something went wrong.