Skip to content

Commit c7f72d6

Browse files
committed
feat: Enabling endToEndTracing support in Connection API
1 parent 0836101 commit c7f72d6

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
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;
@@ -239,6 +240,7 @@ public String[] getValidValues() {
239240
static final boolean DEFAULT_RETURN_COMMIT_STATS = false;
240241
static final boolean DEFAULT_LENIENT = false;
241242
static final boolean DEFAULT_ROUTE_TO_LEADER = true;
243+
static final boolean DEFAULT_ENABLE_END_TO_END_TRACING = false;
242244
static final boolean DEFAULT_DELAY_TRANSACTION_START_UNTIL_FIRST_WRITE = false;
243245
static final boolean DEFAULT_KEEP_TRANSACTION_ALIVE = false;
244246
static final boolean DEFAULT_TRACK_SESSION_LEAKS = true;
@@ -267,6 +269,7 @@ public String[] getValidValues() {
267269
/** Name of the 'routeToLeader' connection property. */
268270
public static final String ROUTE_TO_LEADER_PROPERTY_NAME = "routeToLeader";
269271
/** Name of the 'retry aborts internally' connection property. */
272+
public static final String ENABLE_END_TO_END_TRACING_PROPERTY_NAME = "enableEndToEndTracing";
270273
public static final String RETRY_ABORTS_INTERNALLY_PROPERTY_NAME = "retryAbortsInternally";
271274
/** Name of the property to enable/disable virtual threads for the statement executor. */
272275
public static final String USE_VIRTUAL_THREADS_PROPERTY_NAME = "useVirtualThreads";
@@ -382,6 +385,10 @@ static boolean isEnableTransactionalConnectionStateForPostgreSQL() {
382385
ROUTE_TO_LEADER_PROPERTY_NAME,
383386
"Should read/write transactions and partitioned DML be routed to leader region (true/false)",
384387
DEFAULT_ROUTE_TO_LEADER),
388+
ConnectionProperty.createBooleanProperty(
389+
ENABLE_END_TO_END_TRACING_PROPERTY_NAME,
390+
"Should we enable end to end tracing (true/false)",
391+
DEFAULT_ENABLE_END_TO_END_TRACING),
385392
ConnectionProperty.createBooleanProperty(
386393
RETRY_ABORTS_INTERNALLY_PROPERTY_NAME,
387394
"Should the connection automatically retry Aborted errors (true/false)",
@@ -1205,6 +1212,13 @@ public boolean isRouteToLeader() {
12051212
return getInitialConnectionPropertyValue(ROUTE_TO_LEADER);
12061213
}
12071214

1215+
/**
1216+
* Whether end-to-end tracing is enabled.
1217+
*/
1218+
public boolean enableEndToEndTracing() {
1219+
return getInitialConnectionPropertyValue(ENABLE_END_TO_END_TRACING);
1220+
}
1221+
12081222
/**
12091223
* The initial retryAbortsInternally value for connections created by this {@link
12101224
* ConnectionOptions}

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

Lines changed: 9 additions & 0 deletions
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;
@@ -257,6 +259,13 @@ class ConnectionProperties {
257259
DEFAULT_ROUTE_TO_LEADER,
258260
BooleanConverter.INSTANCE,
259261
Context.STARTUP);
262+
static final ConnectionProperty<Boolean> ENABLE_END_TO_END_TRACING =
263+
create(
264+
ENABLE_END_TO_END_TRACING_PROPERTY_NAME,
265+
"Should we enable end to end tracing (true/false)",
266+
DEFAULT_ENABLE_END_TO_END_TRACING,
267+
BooleanConverter.INSTANCE,
268+
Context.STARTUP);
260269
static final ConnectionProperty<Boolean> USE_VIRTUAL_THREADS =
261270
create(
262271
USE_VIRTUAL_THREADS_PROPERTY_NAME,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ static class SpannerPoolKey {
156156
private final String userAgent;
157157
private final String databaseRole;
158158
private final boolean routeToLeader;
159+
private final boolean enableEndToEndTracing;
159160
private final boolean useVirtualGrpcTransportThreads;
160161
private final OpenTelemetry openTelemetry;
161162
private final Boolean enableExtendedTracing;
@@ -186,6 +187,7 @@ private SpannerPoolKey(ConnectionOptions options) throws IOException {
186187
this.usePlainText = options.isUsePlainText();
187188
this.userAgent = options.getUserAgent();
188189
this.routeToLeader = options.isRouteToLeader();
190+
this.enableEndToEndTracing = options.enableEndToEndTracing();
189191
this.useVirtualGrpcTransportThreads = options.isUseVirtualGrpcTransportThreads();
190192
this.openTelemetry = options.getOpenTelemetry();
191193
this.enableExtendedTracing = options.isEnableExtendedTracing();
@@ -207,6 +209,7 @@ public boolean equals(Object o) {
207209
&& Objects.equals(this.usePlainText, other.usePlainText)
208210
&& Objects.equals(this.userAgent, other.userAgent)
209211
&& Objects.equals(this.routeToLeader, other.routeToLeader)
212+
&& Objects.equals(this.enableEndToEndTracing, other.enableEndToEndTracing)
210213
&& Objects.equals(
211214
this.useVirtualGrpcTransportThreads, other.useVirtualGrpcTransportThreads)
212215
&& Objects.equals(this.openTelemetry, other.openTelemetry)
@@ -226,6 +229,7 @@ public int hashCode() {
226229
this.databaseRole,
227230
this.userAgent,
228231
this.routeToLeader,
232+
this.enableEndToEndTracing,
229233
this.useVirtualGrpcTransportThreads,
230234
this.openTelemetry,
231235
this.enableExtendedTracing,
@@ -380,6 +384,9 @@ Spanner createSpanner(SpannerPoolKey key, ConnectionOptions options) {
380384
if (!options.isRouteToLeader()) {
381385
builder.disableLeaderAwareRouting();
382386
}
387+
if (options.enableEndToEndTracing()) {
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

Lines changed: 21 additions & 0 deletions
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.enableEndToEndTracing());
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.enableEndToEndTracing());
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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,54 @@ 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("cloudspanner:/projects/p/instances/i/databases/d?enableEndToEndTracing=true")
578+
.setCredentials(NoCredentials.getInstance())
579+
.build());
580+
SpannerPoolKey keyWithApiTracingDisabled =
581+
SpannerPoolKey.of(
582+
ConnectionOptions.newBuilder()
583+
.setUri("cloudspanner:/projects/p/instances/i/databases/d?enableEndToEndTracing=false")
584+
.setCredentials(NoCredentials.getInstance())
585+
.build());
586+
587+
assertNotEquals(keyWithoutApiTracingConfig, keyWithApiTracingEnabled);
588+
assertEquals(keyWithoutApiTracingConfig, keyWithApiTracingDisabled);
589+
assertNotEquals(keyWithApiTracingEnabled, keyWithApiTracingDisabled);
590+
591+
assertEquals(
592+
keyWithApiTracingEnabled,
593+
SpannerPoolKey.of(
594+
ConnectionOptions.newBuilder()
595+
.setUri("cloudspanner:/projects/p/instances/i/databases/d?enableEndToEndTracing=true")
596+
.setCredentials(NoCredentials.getInstance())
597+
.build()));
598+
assertEquals(
599+
keyWithApiTracingDisabled,
600+
SpannerPoolKey.of(
601+
ConnectionOptions.newBuilder()
602+
.setUri("cloudspanner:/projects/p/instances/i/databases/d?enableEndToEndTracing=false")
603+
.setCredentials(NoCredentials.getInstance())
604+
.build()));
605+
assertEquals(
606+
keyWithoutApiTracingConfig,
607+
SpannerPoolKey.of(
608+
ConnectionOptions.newBuilder()
609+
.setUri("cloudspanner:/projects/p/instances/i/databases/d")
610+
.setCredentials(NoCredentials.getInstance())
611+
.build()));
612+
}
613+
566614
@Test
567615
public void testOpenTelemetry() {
568616
SpannerPool pool = createSubjectAndMocks();

0 commit comments

Comments
 (0)