Skip to content

Commit 16cc6ee

Browse files
ankiagaolavloite
andauthored
feat: Enabling endToEndTracing support in Connection API (#3412)
* feat: Enabling endToEndTracing support in Connection API * fix formatting * Comments incorporated * formatting fixed * formatting fixed * Update google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ConnectionOptions.java Co-authored-by: Knut Olav Løite <[email protected]> * Update google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ConnectionProperties.java Co-authored-by: Knut Olav Løite <[email protected]> --------- Co-authored-by: Knut Olav Løite <[email protected]>
1 parent aeeea3c commit 16cc6ee

File tree

5 files changed

+110
-3
lines changed

5 files changed

+110
-3
lines changed

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static com.google.cloud.spanner.connection.ConnectionProperties.DATA_BOOST_ENABLED;
2727
import static com.google.cloud.spanner.connection.ConnectionProperties.DIALECT;
2828
import static com.google.cloud.spanner.connection.ConnectionProperties.ENABLE_API_TRACING;
29+
import static com.google.cloud.spanner.connection.ConnectionProperties.ENABLE_END_TO_END_TRACING;
2930
import static com.google.cloud.spanner.connection.ConnectionProperties.ENABLE_EXTENDED_TRACING;
3031
import static com.google.cloud.spanner.connection.ConnectionProperties.ENCODED_CREDENTIALS;
3132
import static com.google.cloud.spanner.connection.ConnectionProperties.ENDPOINT;
@@ -249,6 +250,7 @@ public String[] getValidValues() {
249250
static final int DEFAULT_MAX_PARTITIONED_PARALLELISM = 1;
250251
static final Boolean DEFAULT_ENABLE_EXTENDED_TRACING = null;
251252
static final Boolean DEFAULT_ENABLE_API_TRACING = null;
253+
static final boolean DEFAULT_ENABLE_END_TO_END_TRACING = false;
252254
static final boolean DEFAULT_AUTO_BATCH_DML = false;
253255
static final long DEFAULT_AUTO_BATCH_DML_UPDATE_COUNT = 1L;
254256
static final boolean DEFAULT_AUTO_BATCH_DML_UPDATE_COUNT_VERIFICATION = true;
@@ -335,6 +337,7 @@ public String[] getValidValues() {
335337

336338
public static final String ENABLE_EXTENDED_TRACING_PROPERTY_NAME = "enableExtendedTracing";
337339
public static final String ENABLE_API_TRACING_PROPERTY_NAME = "enableApiTracing";
340+
public static final String ENABLE_END_TO_END_TRACING_PROPERTY_NAME = "enableEndToEndTracing";
338341

339342
public static final String AUTO_BATCH_DML_PROPERTY_NAME = "auto_batch_dml";
340343
public static final String AUTO_BATCH_DML_UPDATE_COUNT_PROPERTY_NAME =
@@ -537,7 +540,14 @@ static boolean isEnableTransactionalConnectionStateForPostgreSQL() {
537540
+ "to get a detailed view of each RPC that is being executed by your application, "
538541
+ "or if you want to debug potential latency problems caused by RPCs that are "
539542
+ "being retried.",
540-
DEFAULT_ENABLE_API_TRACING))));
543+
DEFAULT_ENABLE_API_TRACING),
544+
ConnectionProperty.createBooleanProperty(
545+
ENABLE_END_TO_END_TRACING_PROPERTY_NAME,
546+
"Enable end-to-end tracing (true/false) to generate traces for both the time "
547+
+ "that is spent in the client, as well as time that is spent in the Spanner server. "
548+
+ "Server side traces can only go to Google Cloud Trace, so to see end to end traces, "
549+
+ "the application should configure an exporter that exports the traces to Google Cloud Trace.",
550+
DEFAULT_ENABLE_END_TO_END_TRACING))));
541551

542552
private static final Set<ConnectionProperty> INTERNAL_PROPERTIES =
543553
Collections.unmodifiableSet(
@@ -1205,6 +1215,11 @@ public boolean isRouteToLeader() {
12051215
return getInitialConnectionPropertyValue(ROUTE_TO_LEADER);
12061216
}
12071217

1218+
/** Whether end-to-end tracing is enabled. */
1219+
public boolean isEndToEndTracingEnabled() {
1220+
return getInitialConnectionPropertyValue(ENABLE_END_TO_END_TRACING);
1221+
}
1222+
12081223
/**
12091224
* The initial retryAbortsInternally value for connections created by this {@link
12101225
* ConnectionOptions}

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

+12
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import static com.google.cloud.spanner.connection.ConnectionOptions.DEFAULT_DDL_IN_TRANSACTION_MODE;
4040
import static com.google.cloud.spanner.connection.ConnectionOptions.DEFAULT_DELAY_TRANSACTION_START_UNTIL_FIRST_WRITE;
4141
import static com.google.cloud.spanner.connection.ConnectionOptions.DEFAULT_ENABLE_API_TRACING;
42+
import static com.google.cloud.spanner.connection.ConnectionOptions.DEFAULT_ENABLE_END_TO_END_TRACING;
4243
import static com.google.cloud.spanner.connection.ConnectionOptions.DEFAULT_ENABLE_EXTENDED_TRACING;
4344
import static com.google.cloud.spanner.connection.ConnectionOptions.DEFAULT_ENDPOINT;
4445
import static com.google.cloud.spanner.connection.ConnectionOptions.DEFAULT_KEEP_TRANSACTION_ALIVE;
@@ -65,6 +66,7 @@
6566
import static com.google.cloud.spanner.connection.ConnectionOptions.DELAY_TRANSACTION_START_UNTIL_FIRST_WRITE_NAME;
6667
import static com.google.cloud.spanner.connection.ConnectionOptions.DIALECT_PROPERTY_NAME;
6768
import static com.google.cloud.spanner.connection.ConnectionOptions.ENABLE_API_TRACING_PROPERTY_NAME;
69+
import static com.google.cloud.spanner.connection.ConnectionOptions.ENABLE_END_TO_END_TRACING_PROPERTY_NAME;
6870
import static com.google.cloud.spanner.connection.ConnectionOptions.ENABLE_EXTENDED_TRACING_PROPERTY_NAME;
6971
import static com.google.cloud.spanner.connection.ConnectionOptions.ENCODED_CREDENTIALS_PROPERTY_NAME;
7072
import static com.google.cloud.spanner.connection.ConnectionOptions.ENDPOINT_PROPERTY_NAME;
@@ -292,6 +294,16 @@ class ConnectionProperties {
292294
DEFAULT_ENABLE_API_TRACING,
293295
BooleanConverter.INSTANCE,
294296
Context.STARTUP);
297+
static final ConnectionProperty<Boolean> ENABLE_END_TO_END_TRACING =
298+
create(
299+
ENABLE_END_TO_END_TRACING_PROPERTY_NAME,
300+
"Enable end-to-end tracing (true/false) to generate traces for both the time "
301+
+ "that is spent in the client, as well as time that is spent in the Spanner server. "
302+
+ "Server side traces can only go to Google Cloud Trace, so to see end to end traces, "
303+
+ "the application should configure an exporter that exports the traces to Google Cloud Trace.",
304+
DEFAULT_ENABLE_END_TO_END_TRACING,
305+
BooleanConverter.INSTANCE,
306+
Context.STARTUP);
295307
static final ConnectionProperty<Integer> MIN_SESSIONS =
296308
create(
297309
MIN_SESSIONS_PROPERTY_NAME,

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ static class SpannerPoolKey {
160160
private final OpenTelemetry openTelemetry;
161161
private final Boolean enableExtendedTracing;
162162
private final Boolean enableApiTracing;
163+
private final boolean enableEndToEndTracing;
163164

164165
@VisibleForTesting
165166
static SpannerPoolKey of(ConnectionOptions options) {
@@ -190,6 +191,7 @@ private SpannerPoolKey(ConnectionOptions options) throws IOException {
190191
this.openTelemetry = options.getOpenTelemetry();
191192
this.enableExtendedTracing = options.isEnableExtendedTracing();
192193
this.enableApiTracing = options.isEnableApiTracing();
194+
this.enableEndToEndTracing = options.isEndToEndTracingEnabled();
193195
}
194196

195197
@Override
@@ -211,7 +213,8 @@ public boolean equals(Object o) {
211213
this.useVirtualGrpcTransportThreads, other.useVirtualGrpcTransportThreads)
212214
&& Objects.equals(this.openTelemetry, other.openTelemetry)
213215
&& Objects.equals(this.enableExtendedTracing, other.enableExtendedTracing)
214-
&& Objects.equals(this.enableApiTracing, other.enableApiTracing);
216+
&& Objects.equals(this.enableApiTracing, other.enableApiTracing)
217+
&& Objects.equals(this.enableEndToEndTracing, other.enableEndToEndTracing);
215218
}
216219

217220
@Override
@@ -229,7 +232,8 @@ public int hashCode() {
229232
this.useVirtualGrpcTransportThreads,
230233
this.openTelemetry,
231234
this.enableExtendedTracing,
232-
this.enableApiTracing);
235+
this.enableApiTracing,
236+
this.enableEndToEndTracing);
233237
}
234238
}
235239

@@ -380,6 +384,9 @@ Spanner createSpanner(SpannerPoolKey key, ConnectionOptions options) {
380384
if (!options.isRouteToLeader()) {
381385
builder.disableLeaderAwareRouting();
382386
}
387+
if (options.isEndToEndTracingEnabled()) {
388+
builder.setEnableEndToEndTracing(true);
389+
}
383390
if (key.usePlainText) {
384391
// Credentials may not be sent over a plain text channel.
385392
builder.setCredentials(NoCredentials.getInstance());

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/ConnectionOptionsTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,27 @@ public void testBuildWithRouteToLeader() {
322322
assertTrue(options.isRouteToLeader());
323323
}
324324

325+
@Test
326+
public void testBuildWithEndToEndTracingEnabled() {
327+
final String BASE_URI =
328+
"cloudspanner:/projects/test-project-123/instances/test-instance-123/databases/test-database-123";
329+
ConnectionOptions.Builder builder = ConnectionOptions.newBuilder();
330+
builder.setUri(BASE_URI + "?enableEndToEndTracing=true");
331+
builder.setCredentialsUrl(FILE_TEST_PATH);
332+
ConnectionOptions options = builder.build();
333+
assertEquals(options.getHost(), DEFAULT_HOST);
334+
assertEquals(options.getProjectId(), TEST_PROJECT);
335+
assertEquals(options.getInstanceId(), TEST_INSTANCE);
336+
assertEquals(options.getDatabaseName(), TEST_DATABASE);
337+
assertTrue(options.isEndToEndTracingEnabled());
338+
339+
// Test for default behavior for enableEndToEndTracing property.
340+
builder = ConnectionOptions.newBuilder().setUri(BASE_URI);
341+
builder.setCredentialsUrl(FILE_TEST_PATH);
342+
options = builder.build();
343+
assertFalse(options.isEndToEndTracingEnabled());
344+
}
345+
325346
@Test
326347
public void testBuildWithAutoConfigEmulatorAndHost() {
327348
ConnectionOptions.Builder builder = ConnectionOptions.newBuilder();

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/SpannerPoolTest.java

+52
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,58 @@ public void testEnableApiTracing() {
563563
.build()));
564564
}
565565

566+
@Test
567+
public void testEnableEndToEndTracing() {
568+
SpannerPoolKey keyWithoutApiTracingConfig =
569+
SpannerPoolKey.of(
570+
ConnectionOptions.newBuilder()
571+
.setUri("cloudspanner:/projects/p/instances/i/databases/d")
572+
.setCredentials(NoCredentials.getInstance())
573+
.build());
574+
SpannerPoolKey keyWithApiTracingEnabled =
575+
SpannerPoolKey.of(
576+
ConnectionOptions.newBuilder()
577+
.setUri(
578+
"cloudspanner:/projects/p/instances/i/databases/d?enableEndToEndTracing=true")
579+
.setCredentials(NoCredentials.getInstance())
580+
.build());
581+
SpannerPoolKey keyWithApiTracingDisabled =
582+
SpannerPoolKey.of(
583+
ConnectionOptions.newBuilder()
584+
.setUri(
585+
"cloudspanner:/projects/p/instances/i/databases/d?enableEndToEndTracing=false")
586+
.setCredentials(NoCredentials.getInstance())
587+
.build());
588+
589+
assertNotEquals(keyWithoutApiTracingConfig, keyWithApiTracingEnabled);
590+
assertEquals(keyWithoutApiTracingConfig, keyWithApiTracingDisabled);
591+
assertNotEquals(keyWithApiTracingEnabled, keyWithApiTracingDisabled);
592+
593+
assertEquals(
594+
keyWithApiTracingEnabled,
595+
SpannerPoolKey.of(
596+
ConnectionOptions.newBuilder()
597+
.setUri(
598+
"cloudspanner:/projects/p/instances/i/databases/d?enableEndToEndTracing=true")
599+
.setCredentials(NoCredentials.getInstance())
600+
.build()));
601+
assertEquals(
602+
keyWithApiTracingDisabled,
603+
SpannerPoolKey.of(
604+
ConnectionOptions.newBuilder()
605+
.setUri(
606+
"cloudspanner:/projects/p/instances/i/databases/d?enableEndToEndTracing=false")
607+
.setCredentials(NoCredentials.getInstance())
608+
.build()));
609+
assertEquals(
610+
keyWithoutApiTracingConfig,
611+
SpannerPoolKey.of(
612+
ConnectionOptions.newBuilder()
613+
.setUri("cloudspanner:/projects/p/instances/i/databases/d")
614+
.setCredentials(NoCredentials.getInstance())
615+
.build()));
616+
}
617+
566618
@Test
567619
public void testOpenTelemetry() {
568620
SpannerPool pool = createSubjectAndMocks();

0 commit comments

Comments
 (0)