Skip to content

Commit 9187488

Browse files
Bogdan Drutupongad
Bogdan Drutu
authored andcommitted
Fix parent/child spans relationship in Spanner. (#3690)
1 parent 3c9bbf2 commit 9187488

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

google-cloud-clients/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerImpl.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -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()

google-cloud-clients/google-cloud-spanner/src/test/java/com/google/cloud/spanner/ResumableStreamIteratorTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void setUp() {
9898

9999
private void initWithLimit(int maxBufferSize) {
100100
iterator =
101-
new SpannerImpl.ResumableStreamIterator(maxBufferSize, "") {
101+
new SpannerImpl.ResumableStreamIterator(maxBufferSize, "", null) {
102102
@Override
103103
SpannerImpl.CloseableIterator<PartialResultSet> startStream(
104104
@Nullable ByteString resumeToken) {

0 commit comments

Comments
 (0)