Skip to content

Commit 89c09ce

Browse files
chore: add connection property for API tracing (#3168)
* chore: add connection property for API tracing Add a connection URL property for enabling API tracing, so it can be set in the JDBC connection URL. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore: add NoCredentials for tests --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 8ec0cf2 commit 89c09ce

File tree

4 files changed

+104
-3
lines changed

4 files changed

+104
-3
lines changed

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;

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ static class SpannerPoolKey {
159159
private final boolean useVirtualGrpcTransportThreads;
160160
private final OpenTelemetry openTelemetry;
161161
private final Boolean enableExtendedTracing;
162+
private final Boolean enableApiTracing;
162163

163164
@VisibleForTesting
164165
static SpannerPoolKey of(ConnectionOptions options) {
@@ -188,6 +189,7 @@ private SpannerPoolKey(ConnectionOptions options) throws IOException {
188189
this.useVirtualGrpcTransportThreads = options.isUseVirtualGrpcTransportThreads();
189190
this.openTelemetry = options.getOpenTelemetry();
190191
this.enableExtendedTracing = options.isEnableExtendedTracing();
192+
this.enableApiTracing = options.isEnableApiTracing();
191193
}
192194

193195
@Override
@@ -208,7 +210,8 @@ public boolean equals(Object o) {
208210
&& Objects.equals(
209211
this.useVirtualGrpcTransportThreads, other.useVirtualGrpcTransportThreads)
210212
&& Objects.equals(this.openTelemetry, other.openTelemetry)
211-
&& Objects.equals(this.enableExtendedTracing, other.enableExtendedTracing);
213+
&& Objects.equals(this.enableExtendedTracing, other.enableExtendedTracing)
214+
&& Objects.equals(this.enableApiTracing, other.enableApiTracing);
212215
}
213216

214217
@Override
@@ -225,7 +228,8 @@ public int hashCode() {
225228
this.routeToLeader,
226229
this.useVirtualGrpcTransportThreads,
227230
this.openTelemetry,
228-
this.enableExtendedTracing);
231+
this.enableExtendedTracing,
232+
this.enableApiTracing);
229233
}
230234
}
231235

@@ -364,6 +368,9 @@ Spanner createSpanner(SpannerPoolKey key, ConnectionOptions options) {
364368
if (key.enableExtendedTracing != null) {
365369
builder.setEnableExtendedTracing(key.enableExtendedTracing);
366370
}
371+
if (key.enableApiTracing != null) {
372+
builder.setEnableApiTracing(key.enableApiTracing);
373+
}
367374
if (key.numChannels != null) {
368375
builder.setNumChannels(key.numChannels);
369376
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,4 +1165,29 @@ public void testMaxCommitDelay() {
11651165
.build()
11661166
.getMaxCommitDelay());
11671167
}
1168+
1169+
@Test
1170+
public void testEnableApiTracing() {
1171+
assertNull(
1172+
ConnectionOptions.newBuilder()
1173+
.setUri(
1174+
"cloudspanner:/projects/test-project-123/instances/test-instance/databases/test-database")
1175+
.setCredentials(NoCredentials.getInstance())
1176+
.build()
1177+
.isEnableApiTracing());
1178+
assertTrue(
1179+
ConnectionOptions.newBuilder()
1180+
.setUri(
1181+
"cloudspanner:/projects/test-project-123/instances/test-instance/databases/test-database?enableApiTracing=true")
1182+
.setCredentials(NoCredentials.getInstance())
1183+
.build()
1184+
.isEnableApiTracing());
1185+
assertFalse(
1186+
ConnectionOptions.newBuilder()
1187+
.setUri(
1188+
"cloudspanner:/projects/test-project-123/instances/test-instance/databases/test-database?enableApiTracing=false")
1189+
.setCredentials(NoCredentials.getInstance())
1190+
.build()
1191+
.isEnableApiTracing());
1192+
}
11681193
}

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
@@ -515,6 +515,54 @@ public void testSpannerPoolKeyEquality() {
515515
assertNotEquals(key4, key5);
516516
}
517517

518+
@Test
519+
public void testEnableApiTracing() {
520+
SpannerPoolKey keyWithoutApiTracingConfig =
521+
SpannerPoolKey.of(
522+
ConnectionOptions.newBuilder()
523+
.setUri("cloudspanner:/projects/p/instances/i/databases/d")
524+
.setCredentials(NoCredentials.getInstance())
525+
.build());
526+
SpannerPoolKey keyWithApiTracingEnabled =
527+
SpannerPoolKey.of(
528+
ConnectionOptions.newBuilder()
529+
.setUri("cloudspanner:/projects/p/instances/i/databases/d?enableApiTracing=true")
530+
.setCredentials(NoCredentials.getInstance())
531+
.build());
532+
SpannerPoolKey keyWithApiTracingDisabled =
533+
SpannerPoolKey.of(
534+
ConnectionOptions.newBuilder()
535+
.setUri("cloudspanner:/projects/p/instances/i/databases/d?enableApiTracing=false")
536+
.setCredentials(NoCredentials.getInstance())
537+
.build());
538+
539+
assertNotEquals(keyWithoutApiTracingConfig, keyWithApiTracingEnabled);
540+
assertNotEquals(keyWithoutApiTracingConfig, keyWithApiTracingDisabled);
541+
assertNotEquals(keyWithApiTracingEnabled, keyWithApiTracingDisabled);
542+
543+
assertEquals(
544+
keyWithApiTracingEnabled,
545+
SpannerPoolKey.of(
546+
ConnectionOptions.newBuilder()
547+
.setUri("cloudspanner:/projects/p/instances/i/databases/d?enableApiTracing=true")
548+
.setCredentials(NoCredentials.getInstance())
549+
.build()));
550+
assertEquals(
551+
keyWithApiTracingDisabled,
552+
SpannerPoolKey.of(
553+
ConnectionOptions.newBuilder()
554+
.setUri("cloudspanner:/projects/p/instances/i/databases/d?enableApiTracing=false")
555+
.setCredentials(NoCredentials.getInstance())
556+
.build()));
557+
assertEquals(
558+
keyWithoutApiTracingConfig,
559+
SpannerPoolKey.of(
560+
ConnectionOptions.newBuilder()
561+
.setUri("cloudspanner:/projects/p/instances/i/databases/d")
562+
.setCredentials(NoCredentials.getInstance())
563+
.build()));
564+
}
565+
518566
@Test
519567
public void testOpenTelemetry() {
520568
SpannerPool pool = createSubjectAndMocks();

0 commit comments

Comments
 (0)