1919import com .google .api .core .NanoClock ;
2020import com .google .api .core .SettableApiFuture ;
2121import com .google .api .gax .batching .FlowController ;
22+ import com .google .api .gax .core .GaxProperties ;
2223import com .google .api .gax .retrying .ExponentialRetryAlgorithm ;
2324import com .google .api .gax .retrying .RetrySettings ;
2425import 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 ();
0 commit comments