@@ -1063,7 +1063,7 @@ ResultSet executeQueryInternalWithOptions(
10631063 final int prefetchChunks =
10641064 readOptions .hasPrefetchChunks () ? readOptions .prefetchChunks () : defaultPrefetchChunks ;
10651065 ResumableStreamIterator stream =
1066- new ResumableStreamIterator (MAX_BUFFERED_CHUNKS , QUERY ) {
1066+ new ResumableStreamIterator (MAX_BUFFERED_CHUNKS , QUERY , span ) {
10671067 @ Override
10681068 CloseableIterator <PartialResultSet > startStream (@ Nullable ByteString resumeToken ) {
10691069 GrpcStreamIterator stream = new GrpcStreamIterator (prefetchChunks );
@@ -1176,7 +1176,7 @@ ResultSet readInternalWithOptions(
11761176 final int prefetchChunks =
11771177 readOptions .hasPrefetchChunks () ? readOptions .prefetchChunks () : defaultPrefetchChunks ;
11781178 ResumableStreamIterator stream =
1179- new ResumableStreamIterator (MAX_BUFFERED_CHUNKS , READ ) {
1179+ new ResumableStreamIterator (MAX_BUFFERED_CHUNKS , READ , span ) {
11801180 @ Override
11811181 CloseableIterator <PartialResultSet > startStream (@ Nullable ByteString resumeToken ) {
11821182 GrpcStreamIterator stream = new GrpcStreamIterator (prefetchChunks );
@@ -1426,7 +1426,7 @@ void commit() {
14261426 mutations = null ;
14271427 }
14281428 final CommitRequest commitRequest = builder .build ();
1429- Span opSpan = tracer .spanBuilder (COMMIT ).startSpan ();
1429+ Span opSpan = tracer .spanBuilderWithExplicitParent (COMMIT , span ).startSpan ();
14301430 try (Scope s = tracer .withSpan (opSpan )) {
14311431 CommitResponse commitResponse =
14321432 runWithRetries (
@@ -2452,20 +2452,20 @@ abstract static class ResumableStreamIterator extends AbstractIterator<PartialRe
24522452 */
24532453 private boolean safeToRetry = true ;
24542454
2455- protected ResumableStreamIterator (int maxBufferSize , String streamName ) {
2455+ protected ResumableStreamIterator (int maxBufferSize , String streamName , Span parent ) {
24562456 checkArgument (maxBufferSize >= 0 );
24572457 this .maxBufferSize = maxBufferSize ;
2458- this .span = tracer .spanBuilder (streamName ).startSpan ();
2458+ this .span = tracer .spanBuilderWithExplicitParent (streamName , parent ).startSpan ();
24592459 }
24602460
24612461 abstract CloseableIterator <PartialResultSet > startStream (@ Nullable ByteString resumeToken );
24622462
24632463 @ Override
24642464 public void close (@ Nullable String message ) {
2465- span .end ();
24662465 if (stream != null ) {
24672466 stream .close (message );
24682467 }
2468+ span .end ();
24692469 }
24702470
24712471 @ Override
@@ -2478,7 +2478,11 @@ protected PartialResultSet computeNext() {
24782478 ImmutableMap .of ("ResumeToken" ,
24792479 AttributeValue .stringAttributeValue (
24802480 resumeToken == null ? "null" : resumeToken .toStringUtf8 ())));
2481- stream = checkNotNull (startStream (resumeToken ));
2481+ try (Scope s = tracer .withSpan (span )) {
2482+ // When start a new stream set the Span as current to make the gRPC Span a child of
2483+ // this Span.
2484+ stream = checkNotNull (startStream (resumeToken ));
2485+ }
24822486 }
24832487 // Buffer contains items up to a resume token or has reached capacity: flush.
24842488 if (!buffer .isEmpty ()
0 commit comments