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(apmgrpc): wrap the server-stream with transaction-ctx #1151

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 0 additions & 2 deletions module/apmgrpc/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ github.com/santhosh-tekuri/jsonschema v1.2.4/go.mod h1:TEAUOeZSmIxTTuHatJzrvARHi
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.1.27 h1:nqDD4MMMQA0lmWq03Z2/myGPYLQoXtmi0rGVs95ntbo=
Expand Down
7 changes: 6 additions & 1 deletion module/apmgrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (

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

grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
bendiktv2 marked this conversation as resolved.
Show resolved Hide resolved
)

var (
Expand Down Expand Up @@ -134,6 +136,9 @@ 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

// TODO(axw) define span context schema for RPC,
// including at least the peer address.

Expand All @@ -153,7 +158,7 @@ func NewStreamServerInterceptor(o ...ServerOption) grpc.StreamServerInterceptor
}
setTransactionResult(tx, err)
}()
return handler(srv, stream)
return handler(srv, wrapped)
}
}

Expand Down
12 changes: 11 additions & 1 deletion module/apmgrpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func TestServerStream(t *testing.T) {
tracer, transport := transporttest.NewRecorderTracer()
defer tracer.Close()

s, _, addr := newAccumulatorServer(t, tracer, apmgrpc.WithRecovery())
s, accumulatorServer, addr := newAccumulatorServer(t, tracer, apmgrpc.WithRecovery())
defer s.GracefulStop()

conn, client := newAccumulatorClient(t, addr)
Expand Down Expand Up @@ -290,6 +290,13 @@ func TestServerStream(t *testing.T) {
tracer.Flush(nil)
transactions := transport.Payloads().Transactions
require.Len(t, transactions, 1)

// The transaction should have propagated into the accumulatorServer
require.NotNil(t, accumulatorServer.transactionFromContext)
expectedTraceID := fmt.Sprintf("%x", transactions[0].TraceID)
actualTraceID := accumulatorServer.transactionFromContext.TraceContext().Trace.String()
require.NotEmpty(t, expectedTraceID)
require.Equal(t, expectedTraceID, actualTraceID)
}

func TestServerTLS(t *testing.T) {
Expand Down Expand Up @@ -457,9 +464,12 @@ func (s *helloworldServer) SayHello(ctx context.Context, req *pb.HelloRequest) (
type accumulator struct {
panic bool
err error

transactionFromContext *apm.Transaction
}

func (a *accumulator) Accumulate(srv testservice.Accumulator_AccumulateServer) error {
a.transactionFromContext = apm.TransactionFromContext(srv.Context())
if a.panic {
panic(a.err)
}
Expand Down