Skip to content

Commit 8421875

Browse files
feat: include writerId as part of trace id
1 parent 6b05a81 commit 8421875

4 files changed

Lines changed: 127 additions & 32 deletions

File tree

‎java-bigquerystorage/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/ConnectionWorker.java‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.api.core.NanoClock;
2020
import com.google.api.core.SettableApiFuture;
2121
import com.google.api.gax.batching.FlowController;
22+
import com.google.api.gax.core.GaxProperties;
2223
import com.google.api.gax.retrying.ExponentialRetryAlgorithm;
2324
import com.google.api.gax.retrying.RetrySettings;
2425
import com.google.api.gax.retrying.TimedAttemptSettings;
@@ -127,11 +128,6 @@ class ConnectionWorker implements AutoCloseable {
127128
*/
128129
private final FlowController.LimitExceededBehavior limitExceededBehavior;
129130

130-
/*
131-
* TraceId for debugging purpose.
132-
*/
133-
private final String traceId;
134-
135131
/*
136132
* Enables compression on the wire.
137133
*/
@@ -566,6 +562,7 @@ public ConnectionWorker(
566562
long maxInflightBytes,
567563
Duration maxRetryDuration,
568564
FlowController.LimitExceededBehavior limitExceededBehavior,
565+
String clientlibId,
569566
String traceId,
570567
@Nullable String compressorName,
571568
BigQueryWriteSettings clientSettings,
@@ -582,6 +579,7 @@ public ConnectionWorker(
582579
maxInflightBytes,
583580
maxRetryDuration,
584581
limitExceededBehavior,
582+
clientlibId,
585583
traceId,
586584
compressorName,
587585
clientSettings,
@@ -599,6 +597,7 @@ public ConnectionWorker(
599597
long maxInflightBytes,
600598
Duration maxRetryDuration,
601599
FlowController.LimitExceededBehavior limitExceededBehavior,
600+
String clientlibId,
602601
String traceId,
603602
@Nullable String compressorName,
604603
BigQueryWriteSettings clientSettings,
@@ -615,6 +614,7 @@ public ConnectionWorker(
615614
maxInflightBytes,
616615
maxRetryDuration,
617616
limitExceededBehavior,
617+
clientlibId,
618618
traceId,
619619
compressorName,
620620
clientSettings,
@@ -632,6 +632,7 @@ public ConnectionWorker(
632632
long maxInflightBytes,
633633
Duration maxRetryDuration,
634634
FlowController.LimitExceededBehavior limitExceededBehavior,
635+
String clientlibId,
635636
String traceId,
636637
@Nullable String compressorName,
637638
BigQueryWriteSettings clientSettings,
@@ -656,14 +657,14 @@ public ConnectionWorker(
656657
this.maxInflightRequests = maxInflightRequests;
657658
this.maxInflightBytes = maxInflightBytes;
658659
this.limitExceededBehavior = limitExceededBehavior;
659-
this.traceId = traceId;
660+
String fullTraceId = getFullTraceId(clientlibId, this.writerId, traceId);
660661
this.waitingRequestQueue = new LinkedList<AppendRequestAndResponse>();
661662
this.inflightRequestQueue = new LinkedList<AppendRequestAndResponse>();
662663
this.compressorName = compressorName;
663664
this.retrySettings = retrySettings;
664665
this.requestProfilerHook = new RequestProfiler.RequestProfilerHook(enableRequestProfiler);
665666
this.telemetryMetrics =
666-
new TelemetryMetrics(this, enableOpenTelemetry, getTableName(), writerId, traceId);
667+
new TelemetryMetrics(this, enableOpenTelemetry, getTableName(), writerId, fullTraceId);
667668
this.healthCheckMetrics = new HealthCheckMetrics();
668669
this.isMultiplexing = isMultiplexing;
669670

@@ -991,6 +992,17 @@ static long calculateSleepTimeMilli(long retryCount) {
991992
return (long) Math.min(Math.pow(2, retryCount) * 50, 60000);
992993
}
993994

995+
@VisibleForTesting
996+
static String getFullTraceId(String clientlibId, String writerId, String traceId) {
997+
String clientWithVersion =
998+
clientlibId + ":" + GaxProperties.getLibraryVersion(StreamWriter.class) + ":" + writerId;
999+
if (traceId == null || traceId.isEmpty()) {
1000+
return clientWithVersion;
1001+
} else {
1002+
return clientWithVersion + " " + traceId;
1003+
}
1004+
}
1005+
9941006
@VisibleForTesting
9951007
void setTestOnlyAppendLoopSleepTime(long testOnlyAppendLoopSleepTime) {
9961008
this.testOnlyAppendLoopSleepTime = testOnlyAppendLoopSleepTime;
@@ -1271,7 +1283,11 @@ private void appendLoop() {
12711283
// If we are at the first request for every table switch, including the first request in
12721284
// the connection, we will attach both stream name and table schema to the request.
12731285
destinationSet.add(streamName);
1274-
originalRequestBuilder.setTraceId(wrapper.streamWriter.getFullTraceId());
1286+
originalRequestBuilder.setTraceId(
1287+
getFullTraceId(
1288+
wrapper.streamWriter.getClientId(),
1289+
this.writerId,
1290+
wrapper.streamWriter.getTraceId()));
12751291
} else if (!isMultiplexing) {
12761292
// If we are not in multiplexing and not in the first request, clear the stream name.
12771293
originalRequestBuilder.clearWriteStream();

‎java-bigquerystorage/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/ConnectionWorkerPool.java‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ private ConnectionWorker createOrReuseConnectionWorker(
317317
streamWriter.getStreamName(),
318318
streamWriter.getLocation(),
319319
streamWriter.getWriterSchema(),
320-
streamWriter.getFullTraceId());
320+
streamWriter.getClientId(),
321+
streamWriter.getTraceId());
321322
} else {
322323
ConnectionWorker existingBestConnection =
323324
pickBestLoadConnection(
@@ -337,7 +338,8 @@ private ConnectionWorker createOrReuseConnectionWorker(
337338
streamWriter.getStreamName(),
338339
streamWriter.getLocation(),
339340
streamWriter.getWriterSchema(),
340-
streamWriter.getFullTraceId());
341+
streamWriter.getClientId(),
342+
streamWriter.getTraceId());
341343
} else {
342344
// Stick to the original connection if all the connections are overwhelmed.
343345
if (existingConnectionWorker != null) {
@@ -396,7 +398,8 @@ private ConnectionWorker createConnectionWorker(
396398
String streamName,
397399
String location,
398400
AppendFormats.AppendRowsSchema writeSchema,
399-
String fullTraceId)
401+
String clientId,
402+
String traceId)
400403
throws IOException {
401404
if (enableTesting) {
402405
// Though atomic integer is super lightweight, add extra if check in case adding future logic.
@@ -411,7 +414,8 @@ private ConnectionWorker createConnectionWorker(
411414
maxInflightBytes,
412415
maxRetryDuration,
413416
limitExceededBehavior,
414-
fullTraceId,
417+
clientId,
418+
traceId,
415419
compressorName,
416420
clientSettings,
417421
retrySettings,

‎java-bigquerystorage/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/StreamWriter.java‎

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.google.api.gax.batching.FlowController;
2020
import com.google.api.gax.core.CredentialsProvider;
2121
import com.google.api.gax.core.ExecutorProvider;
22-
import com.google.api.gax.core.GaxProperties;
2322
import com.google.api.gax.retrying.RetrySettings;
2423
import com.google.api.gax.rpc.TransportChannelProvider;
2524
import com.google.auth.Credentials;
@@ -93,8 +92,11 @@ private static Cache<String, String> allocateProjectLocationCache() {
9392
*/
9493
private final String streamName;
9594

96-
/** This is the library version may or may not include library version id. */
97-
private final String fullTraceId;
95+
/** Client id of the writer. */
96+
private final String clientId;
97+
98+
/** User provided trace id. */
99+
private final String traceId;
98100

99101
/** Every writer has a fixed proto schema or arrow schema. */
100102
private final AppendRowsSchema writerSchema;
@@ -256,7 +258,8 @@ private StreamWriter(Builder builder) throws IOException {
256258
BigQueryWriteSettings clientSettings = getBigQueryWriteSettings(builder);
257259
this.requestProfilerHook =
258260
new RequestProfiler.RequestProfilerHook(builder.enableRequestProfiler);
259-
this.fullTraceId = builder.getFullTraceId();
261+
this.clientId = builder.clientId;
262+
this.traceId = builder.traceId;
260263
if (builder.enableRequestProfiler) {
261264
// Request profiler is enabled on singleton level, from now on a periodical flush will be
262265
// started
@@ -275,7 +278,8 @@ private StreamWriter(Builder builder) throws IOException {
275278
builder.maxInflightBytes,
276279
builder.maxRetryDuration,
277280
builder.limitExceededBehavior,
278-
builder.getFullTraceId(),
281+
builder.clientId,
282+
builder.traceId,
279283
builder.compressorName,
280284
clientSettings,
281285
builder.retrySettings,
@@ -395,8 +399,12 @@ static void recreateProjectLocationCache(long durationExpireMillis) {
395399
projectAndDatasetToLocation = allocateProjectLocationCache();
396400
}
397401

398-
String getFullTraceId() {
399-
return fullTraceId;
402+
String getClientId() {
403+
return clientId;
404+
}
405+
406+
String getTraceId() {
407+
return traceId;
400408
}
401409

402410
AppendRowsRequest.MissingValueInterpretation getDefaultValueInterpretation() {
@@ -1119,18 +1127,6 @@ public Builder setRetrySettings(RetrySettings retrySettings) {
11191127
public StreamWriter build() throws IOException {
11201128
return new StreamWriter(this);
11211129
}
1122-
1123-
String getFullTraceId() {
1124-
String clientWithVersion =
1125-
GaxProperties.getLibraryVersion(StreamWriter.class).isEmpty()
1126-
? clientId
1127-
: clientId + ":" + GaxProperties.getLibraryVersion(StreamWriter.class);
1128-
if (traceId == null || traceId.isEmpty()) {
1129-
return clientWithVersion;
1130-
} else {
1131-
return clientWithVersion + " " + traceId;
1132-
}
1133-
}
11341130
}
11351131

11361132
private String generateRequestUniqueId() {

0 commit comments

Comments
 (0)