Skip to content

Commit

Permalink
define wrappedServerStream instead of importing from go-grpc-middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
bendiktv2 committed Nov 15, 2021
1 parent 15832fd commit 53ec384
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions module/apmgrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ import (

"go.elastic.co/apm"
"go.elastic.co/apm/module/apmhttp"

grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
)

var (
Expand Down Expand Up @@ -136,8 +134,8 @@ func NewStreamServerInterceptor(o ...ServerOption) grpc.StreamServerInterceptor
tx, ctx := startTransaction(ctx, opts.tracer, info.FullMethod)
defer tx.End()

wrapped := grpc_middleware.WrapServerStream(stream)
wrapped.WrappedContext = ctx
wrapped := wrapServerStream(stream)
wrapped.wrappedContext = ctx

// TODO(axw) define span context schema for RPC,
// including at least the peer address.
Expand Down Expand Up @@ -316,3 +314,20 @@ func WithServerStreamIgnorer(s StreamIgnorerFunc) ServerOption {
o.streamIgnorer = s
}
}

// wrappedServerStream is a thin wrapper around grpc.ServerStream that allows modifying context.
type wrappedServerStream struct {
grpc.ServerStream
// wrappedContext is the wrapper's own Context. You can assign it.
wrappedContext context.Context
}

// Context returns the wrapper's WrappedContext, overwriting the nested grpc.ServerStream.Context()
func (w *wrappedServerStream) Context() context.Context {
return w.wrappedContext
}

// wrapServerStream returns a ServerStream that has the ability to overwrite context.
func wrapServerStream(stream grpc.ServerStream) *wrappedServerStream {
return &wrappedServerStream{ServerStream: stream, wrappedContext: stream.Context()}
}

0 comments on commit 53ec384

Please sign in to comment.