File tree

11 files changed

+27
-47
lines changed

11 files changed

+27
-47
lines changed
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ public final void invalidate() {
457457

458458
@Override
459459
public void close() {
460-
span.end();
460+
span.end(TraceUtil.END_SPAN_OPTIONS);
461461
synchronized (lock) {
462462
isClosed = true;
463463
}
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ protected ResumableStreamIterator(int maxBufferSize, String streamName, Span par
847847
public void close(@Nullable String message) {
848848
if (stream != null) {
849849
stream.close(message);
850-
span.end();
850+
span.end(TraceUtil.END_SPAN_OPTIONS);
851851
}
852852
}
853853

Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ private enum SessionMode {
3737
READ_WRITE
3838
}
3939

40-
static {
41-
TraceUtil.exportSpans(READ_WRITE_TRANSACTION, READ_ONLY_TRANSACTION, PARTITION_DML_TRANSACTION);
42-
}
43-
4440
@VisibleForTesting final SessionPool pool;
4541

4642
DatabaseClientImpl(SessionPool pool) {
@@ -73,7 +69,7 @@ public Timestamp apply(Session session) {
7369
TraceUtil.endSpanWithFailure(span, e);
7470
throw e;
7571
} finally {
76-
span.end();
72+
span.end(TraceUtil.END_SPAN_OPTIONS);
7773
}
7874
}
7975

@@ -93,7 +89,7 @@ public Timestamp apply(Session session) {
9389
TraceUtil.endSpanWithFailure(span, e);
9490
throw e;
9591
} finally {
96-
span.end();
92+
span.end(TraceUtil.END_SPAN_OPTIONS);
9793
}
9894
}
9995

Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ private BatchCreateSessionsRunnable(
126126
public void run() {
127127
List<SessionImpl> sessions = null;
128128
int remainingSessionsToCreate = sessionCount;
129-
try (Scope scope =
130-
SpannerImpl.tracer.spanBuilder(SpannerImpl.BATCH_CREATE_SESSIONS).startScopedSpan()) {
129+
Span span = SpannerImpl.tracer.spanBuilder(SpannerImpl.BATCH_CREATE_SESSIONS).startSpan();
130+
try (Scope s = SpannerImpl.tracer.withSpan(span)) {
131131
SpannerImpl.tracer
132132
.getCurrentSpan()
133133
.addAnnotation(String.format("Creating %d sessions", sessionCount));
@@ -144,6 +144,8 @@ public void run() {
144144
}
145145
remainingSessionsToCreate -= sessions.size();
146146
}
147+
} finally {
148+
span.end(TraceUtil.END_SPAN_OPTIONS);
147149
}
148150
}
149151
}
@@ -205,7 +207,7 @@ SessionImpl createSession() {
205207
spanner
206208
.getRpc()
207209
.createSession(db.getName(), spanner.getOptions().getSessionLabels(), options);
208-
span.end();
210+
span.end(TraceUtil.END_SPAN_OPTIONS);
209211
return new SessionImpl(spanner, session.getName(), options);
210212
} catch (RuntimeException e) {
211213
TraceUtil.endSpanWithFailure(span, e);
@@ -284,7 +286,7 @@ private List<SessionImpl> internalBatchCreateSessions(
284286
span.addAnnotation(
285287
String.format(
286288
"Request for %d sessions returned %d sessions", sessionCount, sessions.size()));
287-
span.end();
289+
span.end(TraceUtil.END_SPAN_OPTIONS);
288290
List<SessionImpl> res = new ArrayList<>(sessionCount);
289291
for (com.google.spanner.v1.Session session : sessions) {
290292
res.add(new SessionImpl(spanner, session.getName(), options));
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public Timestamp writeAtLeastOnce(Iterable<Mutation> mutations) throws SpannerEx
138138
try (Scope s = tracer.withSpan(span)) {
139139
CommitResponse response = spanner.getRpc().commit(request, options);
140140
Timestamp t = Timestamp.fromProto(response.getCommitTimestamp());
141-
span.end();
141+
span.end(TraceUtil.END_SPAN_OPTIONS);
142142
return t;
143143
} catch (IllegalArgumentException e) {
144144
TraceUtil.endSpanWithFailure(span, e);
@@ -201,7 +201,7 @@ public void close() {
201201
Span span = tracer.spanBuilder(SpannerImpl.DELETE_SESSION).startSpan();
202202
try (Scope s = tracer.withSpan(span)) {
203203
spanner.getRpc().deleteSession(name, options);
204-
span.end();
204+
span.end(TraceUtil.END_SPAN_OPTIONS);
205205
} catch (RuntimeException e) {
206206
TraceUtil.endSpanWithFailure(span, e);
207207
throw e;
@@ -222,7 +222,7 @@ ByteString beginTransaction() {
222222
if (txn.getId().isEmpty()) {
223223
throw newSpannerException(ErrorCode.INTERNAL, "Missing id in transaction\n" + getName());
224224
}
225-
span.end();
225+
span.end(TraceUtil.END_SPAN_OPTIONS);
226226
return txn.getId();
227227
} catch (RuntimeException e) {
228228
TraceUtil.endSpanWithFailure(span, e);
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ final class SessionPool {
7272
private static final Tracer tracer = Tracing.getTracer();
7373
static final String WAIT_FOR_SESSION = "SessionPool.WaitForSession";
7474

75-
static {
76-
TraceUtil.exportSpans(WAIT_FOR_SESSION);
77-
}
78-
7975
/**
8076
* Wrapper around current time so that we can fake it in tests. TODO(user): Replace with Java 8
8177
* Clock.
@@ -854,7 +850,8 @@ private void put(SpannerException e) {
854850
private PooledSession take() throws SpannerException {
855851
long currentTimeout = options.getInitialWaitForSessionTimeoutMillis();
856852
while (true) {
857-
try (Scope waitScope = tracer.spanBuilder(WAIT_FOR_SESSION).startScopedSpan()) {
853+
Span span = tracer.spanBuilder(WAIT_FOR_SESSION).startSpan();
854+
try (Scope waitScope = tracer.withSpan(span)) {
858855
SessionOrError s = pollUninterruptiblyWithTimeout(currentTimeout);
859856
if (s == null) {
860857
// Set the status to DEADLINE_EXCEEDED and retry.
@@ -870,6 +867,8 @@ private PooledSession take() throws SpannerException {
870867
} catch (Exception e) {
871868
TraceUtil.endSpanWithFailure(tracer.getCurrentSpan(), e);
872869
throw e;
870+
} finally {
871+
span.end(TraceUtil.END_SPAN_OPTIONS);
873872
}
874873
}
875874
}
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,6 @@ class SpannerImpl extends BaseService<SpannerOptions> implements Spanner {
7171
static final String QUERY = "CloudSpannerOperation.ExecuteQuery";
7272
static final String READ = "CloudSpannerOperation.ExecuteRead";
7373

74-
static {
75-
TraceUtil.exportSpans(
76-
BATCH_CREATE_SESSIONS,
77-
BATCH_CREATE_SESSIONS_REQUEST,
78-
CREATE_SESSION,
79-
DELETE_SESSION,
80-
BEGIN_TRANSACTION,
81-
COMMIT,
82-
QUERY,
83-
READ);
84-
}
85-
8674
private final SpannerRpc gapicRpc;
8775

8876
@GuardedBy("this")
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@
2121
import com.google.spanner.v1.Transaction;
2222
import io.opencensus.contrib.grpc.util.StatusConverter;
2323
import io.opencensus.trace.AttributeValue;
24+
import io.opencensus.trace.EndSpanOptions;
2425
import io.opencensus.trace.Span;
2526
import io.opencensus.trace.Status;
26-
import io.opencensus.trace.Tracing;
27-
import io.opencensus.trace.export.SampledSpanStore;
28-
import java.util.Arrays;
2927
import java.util.Map;
3028

3129
/** Utility methods for tracing. */
3230
class TraceUtil {
3331

32+
static final EndSpanOptions END_SPAN_OPTIONS =
33+
EndSpanOptions.builder().setSampleToLocalSpanStore(true).build();
34+
3435
static Map<String, AttributeValue> getTransactionAnnotations(Transaction t) {
3536
return ImmutableMap.of(
3637
"Id",
@@ -58,21 +59,14 @@ static void endSpanWithFailure(Span span, Throwable e) {
5859
endSpanWithFailure(span, (SpannerException) e);
5960
} else {
6061
span.setStatus(Status.INTERNAL.withDescription(e.getMessage()));
61-
span.end();
62+
span.end(END_SPAN_OPTIONS);
6263
}
6364
}
6465

6566
static void endSpanWithFailure(Span span, SpannerException e) {
6667
span.setStatus(
6768
StatusConverter.fromGrpcStatus(e.getErrorCode().getGrpcStatus())
6869
.withDescription(e.getMessage()));
69-
span.end();
70-
}
71-
72-
static void exportSpans(String... spans) {
73-
SampledSpanStore store = Tracing.getExportComponent().getSampledSpanStore();
74-
if (store != null) {
75-
store.registerSpanNamesForCollection(Arrays.asList(spans));
76-
}
70+
span.end(END_SPAN_OPTIONS);
7771
}
7872
}
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void close() {
115115
txnState = TransactionState.ROLLED_BACK;
116116
}
117117
} finally {
118-
span.end();
118+
span.end(TraceUtil.END_SPAN_OPTIONS);
119119
}
120120
}
121121

Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void commit() {
130130
ErrorCode.INTERNAL, "Missing commitTimestamp:\n" + session.getName());
131131
}
132132
commitTimestamp = Timestamp.fromProto(commitResponse.getCommitTimestamp());
133-
opSpan.end();
133+
opSpan.end(TraceUtil.END_SPAN_OPTIONS);
134134
} catch (RuntimeException e) {
135135
span.addAnnotation("Commit Failed", TraceUtil.getExceptionAnnotations(e));
136136
TraceUtil.endSpanWithFailure(opSpan, e);
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.grpc.Metadata;
3232
import io.grpc.StatusRuntimeException;
3333
import io.grpc.protobuf.ProtoUtils;
34+
import io.opencensus.trace.EndSpanOptions;
3435
import io.opencensus.trace.Span;
3536
import java.util.ArrayList;
3637
import java.util.Iterator;
@@ -157,7 +158,7 @@ public void closedSpan() {
157158
assertThat(consume(resumableStreamIterator)).containsExactly("a", "b").inOrder();
158159

159160
resumableStreamIterator.close("closed");
160-
verify(span).end();
161+
verify(span).end(EndSpanOptions.builder().setSampleToLocalSpanStore(true).build());
161162
}
162163

163164
@Test

0 commit comments

Comments
 (0)