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 */
@@ -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 ();
0 commit comments