Skip to content

Commit 59222d2

Browse files
author
myxy99
committed
grpc
1 parent 62054bf commit 59222d2

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

xgrpc/server/interceptor.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,34 @@ func TraceUnaryServerInterceptor() grpc.UnaryServerInterceptor {
105105
}
106106
}
107107

108+
109+
type contextedServerStream struct {
110+
grpc.ServerStream
111+
ctx context.Context
112+
}
113+
114+
// Context ...
115+
func (css contextedServerStream) Context() context.Context {
116+
return css.ctx
117+
}
118+
119+
func TraceStreamServerInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
120+
span, ctx := xtrace.StartSpanFromContext(
121+
ss.Context(),
122+
info.FullMethod,
123+
xtrace.FromIncomingContext(ss.Context()),
124+
xtrace.TagComponent("gRPC"),
125+
xtrace.TagSpanKind("server.stream"),
126+
xtrace.CustomTag("isServerStream", info.IsServerStream),
127+
)
128+
defer span.Finish()
129+
130+
return handler(srv, contextedServerStream{
131+
ServerStream: ss,
132+
ctx: ctx,
133+
})
134+
}
135+
108136
func XTimeoutUnaryServerInterceptor(timeout time.Duration) grpc.UnaryServerInterceptor {
109137
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
110138
handler grpc.UnaryHandler) (resp interface{}, err error) {

xgrpc/xgrpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ func ExtractFromCtx(ctx context.Context, key string) string {
2727
if md, ok := metadata.FromIncomingContext(ctx); ok {
2828
return strings.Join(md.Get(key), ",")
2929
}
30-
return ""
30+
return "unknown"
3131
}

0 commit comments

Comments
 (0)