Skip to content

Commit 9379106

Browse files
committed
Merge branch 'main' of https://github.com/googleapis/java-spanner into e2e-tracing-header
2 parents b4864da + 89c09ce commit 9379106

17 files changed

+724
-17
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ If you are using Maven without the BOM, add this to your dependencies:
5050
If you are using Gradle 5.x or later, add this to your dependencies:
5151

5252
```Groovy
53-
implementation platform('com.google.cloud:libraries-bom:26.40.0')
53+
implementation platform('com.google.cloud:libraries-bom:26.42.0')
5454
5555
implementation 'com.google.cloud:google-cloud-spanner'
5656
```
5757
If you are using Gradle without BOM, add this to your dependencies:
5858

5959
```Groovy
60-
implementation 'com.google.cloud:google-cloud-spanner:6.68.1'
60+
implementation 'com.google.cloud:google-cloud-spanner:6.69.0'
6161
```
6262

6363
If you are using SBT, add this to your dependencies:
6464

6565
```Scala
66-
libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.68.1"
66+
libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.69.0"
6767
```
6868
<!-- {x-version-update-end} -->
6969

@@ -687,7 +687,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
687687
[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-spanner/java11.html
688688
[stability-image]: https://img.shields.io/badge/stability-stable-green
689689
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-spanner.svg
690-
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-spanner/6.68.1
690+
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-spanner/6.69.0
691691
[authentication]: https://github.com/googleapis/google-cloud-java#authentication
692692
[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
693693
[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.spanner;
18+
19+
import com.google.api.core.InternalApi;
20+
import com.google.api.gax.tracing.ApiTracer;
21+
import com.google.api.gax.tracing.BaseApiTracer;
22+
import com.google.api.gax.tracing.MetricsTracer;
23+
import com.google.common.collect.ImmutableList;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
import org.threeten.bp.Duration;
27+
28+
@InternalApi
29+
public class CompositeTracer extends BaseApiTracer {
30+
private final List<ApiTracer> children;
31+
32+
public CompositeTracer(List<ApiTracer> children) {
33+
this.children = ImmutableList.copyOf(children);
34+
}
35+
36+
@Override
37+
public Scope inScope() {
38+
final List<Scope> childScopes = new ArrayList<>(children.size());
39+
40+
for (ApiTracer child : children) {
41+
childScopes.add(child.inScope());
42+
}
43+
44+
return new Scope() {
45+
@Override
46+
public void close() {
47+
for (Scope childScope : childScopes) {
48+
childScope.close();
49+
}
50+
}
51+
};
52+
}
53+
54+
@Override
55+
public void operationSucceeded() {
56+
for (ApiTracer child : children) {
57+
child.operationSucceeded();
58+
}
59+
}
60+
61+
@Override
62+
public void operationCancelled() {
63+
for (ApiTracer child : children) {
64+
child.operationCancelled();
65+
}
66+
}
67+
68+
@Override
69+
public void operationFailed(Throwable error) {
70+
for (ApiTracer child : children) {
71+
child.operationFailed(error);
72+
}
73+
}
74+
75+
@Override
76+
public void connectionSelected(String id) {
77+
for (ApiTracer child : children) {
78+
child.connectionSelected(id);
79+
}
80+
}
81+
82+
@Override
83+
public void attemptStarted(int attemptNumber) {
84+
for (ApiTracer child : children) {
85+
child.attemptStarted(null, attemptNumber);
86+
}
87+
}
88+
89+
@Override
90+
public void attemptStarted(Object request, int attemptNumber) {
91+
for (ApiTracer child : children) {
92+
child.attemptStarted(request, attemptNumber);
93+
}
94+
}
95+
96+
@Override
97+
public void attemptSucceeded() {
98+
for (ApiTracer child : children) {
99+
child.attemptSucceeded();
100+
}
101+
}
102+
103+
@Override
104+
public void attemptCancelled() {
105+
for (ApiTracer child : children) {
106+
child.attemptCancelled();
107+
}
108+
}
109+
110+
@Override
111+
public void attemptFailed(Throwable error, Duration delay) {
112+
for (ApiTracer child : children) {
113+
child.attemptFailed(error, delay);
114+
}
115+
}
116+
117+
@Override
118+
public void attemptFailedRetriesExhausted(Throwable error) {
119+
for (ApiTracer child : children) {
120+
child.attemptFailedRetriesExhausted(error);
121+
}
122+
}
123+
124+
@Override
125+
public void attemptPermanentFailure(Throwable error) {
126+
for (ApiTracer child : children) {
127+
child.attemptPermanentFailure(error);
128+
}
129+
}
130+
131+
@Override
132+
public void lroStartFailed(Throwable error) {
133+
for (ApiTracer child : children) {
134+
child.lroStartFailed(error);
135+
}
136+
}
137+
138+
@Override
139+
public void lroStartSucceeded() {
140+
for (ApiTracer child : children) {
141+
child.lroStartSucceeded();
142+
}
143+
}
144+
145+
@Override
146+
public void responseReceived() {
147+
for (ApiTracer child : children) {
148+
child.responseReceived();
149+
}
150+
}
151+
152+
@Override
153+
public void requestSent() {
154+
for (ApiTracer child : children) {
155+
child.requestSent();
156+
}
157+
}
158+
159+
@Override
160+
public void batchRequestSent(long elementCount, long requestSize) {
161+
for (ApiTracer child : children) {
162+
child.batchRequestSent(elementCount, requestSize);
163+
}
164+
}
165+
166+
public void addAttributes(String key, String value) {
167+
for (ApiTracer child : children) {
168+
if (child instanceof MetricsTracer) {
169+
MetricsTracer metricsTracer = (MetricsTracer) child;
170+
metricsTracer.addAttributes(key, value);
171+
}
172+
}
173+
};
174+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.spanner;
18+
19+
import com.google.api.core.InternalApi;
20+
import com.google.api.gax.tracing.ApiTracer;
21+
import com.google.api.gax.tracing.ApiTracerFactory;
22+
import com.google.api.gax.tracing.BaseApiTracerFactory;
23+
import com.google.api.gax.tracing.SpanName;
24+
import com.google.common.collect.ImmutableList;
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
28+
@InternalApi
29+
public class CompositeTracerFactory extends BaseApiTracerFactory {
30+
31+
private final List<ApiTracerFactory> apiTracerFactories;
32+
33+
public CompositeTracerFactory(List<ApiTracerFactory> apiTracerFactories) {
34+
this.apiTracerFactories = ImmutableList.copyOf(apiTracerFactories);
35+
}
36+
37+
@Override
38+
public ApiTracer newTracer(ApiTracer parent, SpanName spanName, OperationType operationType) {
39+
List<ApiTracer> children = new ArrayList<>(apiTracerFactories.size());
40+
41+
for (ApiTracerFactory factory : apiTracerFactories) {
42+
children.add(factory.newTracer(parent, spanName, operationType));
43+
}
44+
return new CompositeTracer(children);
45+
}
46+
}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/DelayedReadContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ T getReadContext() {
4444
try {
4545
return this.readContextFuture.get();
4646
} catch (ExecutionException executionException) {
47+
// Propagate the underlying exception as a RuntimeException (SpannerException is also a
48+
// RuntimeException).
49+
if (executionException.getCause() instanceof RuntimeException) {
50+
throw (RuntimeException) executionException.getCause();
51+
}
4752
throw SpannerExceptionFactory.asSpannerException(executionException.getCause());
4853
} catch (InterruptedException interruptedException) {
4954
throw SpannerExceptionFactory.propagateInterrupt(interruptedException);

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@
7272
import java.io.IOException;
7373
import java.net.MalformedURLException;
7474
import java.net.URL;
75+
import java.util.ArrayList;
7576
import java.util.HashMap;
77+
import java.util.List;
7678
import java.util.Map;
7779
import java.util.Map.Entry;
7880
import java.util.Set;
@@ -1634,8 +1636,12 @@ public OpenTelemetry getOpenTelemetry() {
16341636

16351637
@Override
16361638
public ApiTracerFactory getApiTracerFactory() {
1639+
List<ApiTracerFactory> apiTracerFactories = new ArrayList();
16371640
// Prefer any direct ApiTracerFactory that might have been set on the builder.
1638-
return MoreObjects.firstNonNull(super.getApiTracerFactory(), getDefaultApiTracerFactory());
1641+
apiTracerFactories.add(
1642+
MoreObjects.firstNonNull(super.getApiTracerFactory(), getDefaultApiTracerFactory()));
1643+
1644+
return new CompositeTracerFactory(apiTracerFactories);
16391645
}
16401646

16411647
private ApiTracerFactory getDefaultApiTracerFactory() {

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/AbstractBaseUnitOfWork.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ B setSpan(@Nullable Span span) {
165165
this.span = Preconditions.checkNotNull(builder.span);
166166
}
167167

168+
@Override
169+
public Span getSpan() {
170+
return this.span;
171+
}
172+
168173
ApiFuture<Void> asyncEndUnitOfWorkSpan() {
169174
return this.statementExecutor.submit(this::endUnitOfWorkSpan);
170175
}

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ConnectionImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ class ConnectionImpl implements Connection {
9696
private static final String SINGLE_USE_TRANSACTION = "SingleUseTransaction";
9797
private static final String READ_ONLY_TRANSACTION = "ReadOnlyTransaction";
9898
private static final String READ_WRITE_TRANSACTION = "ReadWriteTransaction";
99-
private static final String DML_BATCH = "DmlBatch";
10099
private static final String DDL_BATCH = "DdlBatch";
101100
private static final String DDL_STATEMENT = "DdlStatement";
102101

@@ -1932,7 +1931,8 @@ UnitOfWork createNewUnitOfWork(
19321931
.setStatementTag(statementTag)
19331932
.setExcludeTxnFromChangeStreams(excludeTxnFromChangeStreams)
19341933
.setRpcPriority(rpcPriority)
1935-
.setSpan(createSpanForUnitOfWork(DML_BATCH))
1934+
// Use the transaction Span for the DML batch.
1935+
.setSpan(transactionStack.peek().getSpan())
19361936
.build();
19371937
case DDL_BATCH:
19381938
return DdlBatch.newBuilder()

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ConnectionOptions.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ public String[] getValidValues() {
199199
private static final int DEFAULT_MAX_PARTITIONS = 0;
200200
private static final int DEFAULT_MAX_PARTITIONED_PARALLELISM = 1;
201201
private static final Boolean DEFAULT_ENABLE_EXTENDED_TRACING = null;
202+
private static final Boolean DEFAULT_ENABLE_API_TRACING = null;
202203

203204
private static final String PLAIN_TEXT_PROTOCOL = "http:";
204205
private static final String HOST_PROTOCOL = "https:";
@@ -280,6 +281,7 @@ public String[] getValidValues() {
280281
"maxPartitionedParallelism";
281282

282283
public static final String ENABLE_EXTENDED_TRACING_PROPERTY_NAME = "enableExtendedTracing";
284+
public static final String ENABLE_API_TRACING_PROPERTY_NAME = "enableApiTracing";
283285

284286
private static final String GUARDED_CONNECTION_PROPERTY_ERROR_MESSAGE =
285287
"%s can only be used if the system property %s has been set to true. "
@@ -448,7 +450,14 @@ private static String generateGuardedConnectionPropertyError(
448450
"Include the SQL string in the OpenTelemetry traces that are generated "
449451
+ "by this connection. The SQL string is added as the standard OpenTelemetry "
450452
+ "attribute 'db.statement'.",
451-
DEFAULT_ENABLE_EXTENDED_TRACING))));
453+
DEFAULT_ENABLE_EXTENDED_TRACING),
454+
ConnectionProperty.createBooleanProperty(
455+
ENABLE_API_TRACING_PROPERTY_NAME,
456+
"Add OpenTelemetry traces for each individual RPC call. Enable this "
457+
+ "to get a detailed view of each RPC that is being executed by your application, "
458+
+ "or if you want to debug potential latency problems caused by RPCs that are "
459+
+ "being retried.",
460+
DEFAULT_ENABLE_API_TRACING))));
452461

453462
private static final Set<ConnectionProperty> INTERNAL_PROPERTIES =
454463
Collections.unmodifiableSet(
@@ -743,6 +752,7 @@ public static Builder newBuilder() {
743752
private final OpenTelemetry openTelemetry;
744753
private final String tracingPrefix;
745754
private final Boolean enableExtendedTracing;
755+
private final Boolean enableApiTracing;
746756
private final List<StatementExecutionInterceptor> statementExecutionInterceptors;
747757
private final SpannerOptionsConfigurator configurator;
748758

@@ -851,6 +861,7 @@ private ConnectionOptions(Builder builder) {
851861
this.openTelemetry = builder.openTelemetry;
852862
this.tracingPrefix = builder.tracingPrefix;
853863
this.enableExtendedTracing = parseEnableExtendedTracing(this.uri);
864+
this.enableApiTracing = parseEnableApiTracing(this.uri);
854865
this.statementExecutionInterceptors =
855866
Collections.unmodifiableList(builder.statementExecutionInterceptors);
856867
this.configurator = builder.configurator;
@@ -1251,6 +1262,12 @@ static Boolean parseEnableExtendedTracing(String uri) {
12511262
return value != null ? Boolean.valueOf(value) : DEFAULT_ENABLE_EXTENDED_TRACING;
12521263
}
12531264

1265+
@VisibleForTesting
1266+
static Boolean parseEnableApiTracing(String uri) {
1267+
String value = parseUriProperty(uri, ENABLE_API_TRACING_PROPERTY_NAME);
1268+
return value != null ? Boolean.valueOf(value) : DEFAULT_ENABLE_API_TRACING;
1269+
}
1270+
12541271
@VisibleForTesting
12551272
static String parseUriProperty(String uri, String property) {
12561273
Pattern pattern = Pattern.compile(String.format("(?is)(?:;|\\?)%s=(.*?)(?:;|$)", property));
@@ -1558,6 +1575,10 @@ Boolean isEnableExtendedTracing() {
15581575
return this.enableExtendedTracing;
15591576
}
15601577

1578+
Boolean isEnableApiTracing() {
1579+
return this.enableApiTracing;
1580+
}
1581+
15611582
/** Interceptors that should be executed after each statement */
15621583
List<StatementExecutionInterceptor> getStatementExecutionInterceptors() {
15631584
return statementExecutionInterceptors;

0 commit comments

Comments
 (0)