Skip to content

Commit ffa8063

Browse files
feat: include writerId as part of trace id (#13488)
1 parent ee79a4a commit ffa8063

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
*/
@@ -577,6 +573,7 @@ public ConnectionWorker(
577573
long maxInflightBytes,
578574
Duration maxRetryDuration,
579575
FlowController.LimitExceededBehavior limitExceededBehavior,
576+
String clientlibId,
580577
String traceId,
581578
@Nullable String compressorName,
582579
BigQueryWriteSettings clientSettings,
@@ -593,6 +590,7 @@ public ConnectionWorker(
593590
maxInflightBytes,
594591
maxRetryDuration,
595592
limitExceededBehavior,
593+
clientlibId,
596594
traceId,
597595
compressorName,
598596
clientSettings,
@@ -610,6 +608,7 @@ public ConnectionWorker(
610608
long maxInflightBytes,
611609
Duration maxRetryDuration,
612610
FlowController.LimitExceededBehavior limitExceededBehavior,
611+
String clientlibId,
613612
String traceId,
614613
@Nullable String compressorName,
615614
BigQueryWriteSettings clientSettings,
@@ -626,6 +625,7 @@ public ConnectionWorker(
626625
maxInflightBytes,
627626
maxRetryDuration,
628627
limitExceededBehavior,
628+
clientlibId,
629629
traceId,
630630
compressorName,
631631
clientSettings,
@@ -643,6 +643,7 @@ public ConnectionWorker(
643643
long maxInflightBytes,
644644
Duration maxRetryDuration,
645645
FlowController.LimitExceededBehavior limitExceededBehavior,
646+
String clientlibId,
646647
String traceId,
647648
@Nullable String compressorName,
648649
BigQueryWriteSettings clientSettings,
@@ -667,14 +668,14 @@ public ConnectionWorker(
667668
this.maxInflightRequests = maxInflightRequests;
668669
this.maxInflightBytes = maxInflightBytes;
669670
this.limitExceededBehavior = limitExceededBehavior;
670-
this.traceId = traceId;
671+
String fullTraceId = getFullTraceId(clientlibId, this.writerId, traceId);
671672
this.waitingRequestQueue = new LinkedList<AppendRequestAndResponse>();
672673
this.inflightRequestQueue = new LinkedList<AppendRequestAndResponse>();
673674
this.compressorName = compressorName;
674675
this.retrySettings = retrySettings;
675676
this.requestProfilerHook = new RequestProfiler.RequestProfilerHook(enableRequestProfiler);
676677
this.telemetryMetrics =
677-
new TelemetryMetrics(this, enableOpenTelemetry, getTableName(), writerId, traceId);
678+
new TelemetryMetrics(this, enableOpenTelemetry, getTableName(), writerId, fullTraceId);
678679
this.healthCheckMetrics = new HealthCheckMetrics();
679680
this.isMultiplexing = isMultiplexing;
680681

@@ -1004,6 +1005,17 @@ static long calculateSleepTimeMilli(long retryCount) {
10041005
return (long) Math.min(Math.pow(2, retryCount) * 50, 60000);
10051006
}
10061007

1008+
@VisibleForTesting
1009+
static String getFullTraceId(String clientlibId, String writerId, String traceId) {
1010+
String clientWithVersion =
1011+
clientlibId + ":" + GaxProperties.getLibraryVersion(StreamWriter.class) + ":" + writerId;
1012+
if (traceId == null || traceId.isEmpty()) {
1013+
return clientWithVersion;
1014+
} else {
1015+
return clientWithVersion + " " + traceId;
1016+
}
1017+
}
1018+
10071019
@VisibleForTesting
10081020
void setTestOnlyAppendLoopSleepTime(long testOnlyAppendLoopSleepTime) {
10091021
this.testOnlyAppendLoopSleepTime = testOnlyAppendLoopSleepTime;
@@ -1291,7 +1303,11 @@ private void appendLoop() {
12911303
// If we are at the first request for every table switch, including the first request in
12921304
// the connection, we will attach both stream name and table schema to the request.
12931305
destinationSet.add(streamName);
1294-
originalRequestBuilder.setTraceId(wrapper.streamWriter.getFullTraceId());
1306+
originalRequestBuilder.setTraceId(
1307+
getFullTraceId(
1308+
wrapper.streamWriter.getClientId(),
1309+
this.writerId,
1310+
wrapper.streamWriter.getTraceId()));
12951311
} else if (!isMultiplexing) {
12961312
// If we are not in multiplexing and not in the first request, clear the stream name.
12971313
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)