@@ -247,6 +247,8 @@ class ConnectionWorker implements AutoCloseable {
247
247
*/
248
248
private final RetrySettings retrySettings ;
249
249
250
+ private final RequestProfiler .RequestProfilerHook requestProfilerHook ;
251
+
250
252
private static String projectMatching = "projects/[^/]+/" ;
251
253
private static Pattern streamPatternProject = Pattern .compile (projectMatching );
252
254
@@ -386,7 +388,8 @@ public ConnectionWorker(
386
388
String traceId ,
387
389
@ Nullable String compressorName ,
388
390
BigQueryWriteSettings clientSettings ,
389
- RetrySettings retrySettings )
391
+ RetrySettings retrySettings ,
392
+ boolean enableRequestProfiler )
390
393
throws IOException {
391
394
this .lock = new ReentrantLock ();
392
395
this .hasMessageInWaitingQueue = lock .newCondition ();
@@ -410,6 +413,7 @@ public ConnectionWorker(
410
413
this .compressorName = compressorName ;
411
414
this .retrySettings = retrySettings ;
412
415
this .telemetryAttributes = buildOpenTelemetryAttributes ();
416
+ this .requestProfilerHook = new RequestProfiler .RequestProfilerHook (enableRequestProfiler );
413
417
registerOpenTelemetryMetrics ();
414
418
415
419
// Always recreate a client for connection worker.
@@ -503,7 +507,7 @@ private boolean shouldWaitForBackoff(AppendRequestAndResponse requestWrapper) {
503
507
504
508
private void waitForBackoffIfNecessary (AppendRequestAndResponse requestWrapper ) {
505
509
lock .lock ();
506
- RequestProfiler . REQUEST_PROFILER_SINGLETON .startOperation (
510
+ requestProfilerHook .startOperation (
507
511
RequestProfiler .OperationName .RETRY_BACKOFF , requestWrapper .requestUniqueId );
508
512
try {
509
513
Condition condition = lock .newCondition ();
@@ -513,7 +517,7 @@ private void waitForBackoffIfNecessary(AppendRequestAndResponse requestWrapper)
513
517
} catch (InterruptedException e ) {
514
518
throw new IllegalStateException (e );
515
519
} finally {
516
- RequestProfiler . REQUEST_PROFILER_SINGLETON .endOperation (
520
+ requestProfilerHook .endOperation (
517
521
RequestProfiler .OperationName .RETRY_BACKOFF , requestWrapper .requestUniqueId );
518
522
lock .unlock ();
519
523
}
@@ -535,7 +539,7 @@ private void addMessageToWaitingQueue(
535
539
++this .inflightRequests ;
536
540
this .inflightBytes += requestWrapper .messageSize ;
537
541
hasMessageInWaitingQueue .signal ();
538
- RequestProfiler . REQUEST_PROFILER_SINGLETON .startOperation (
542
+ requestProfilerHook .startOperation (
539
543
RequestProfiler .OperationName .WAIT_QUEUE , requestWrapper .requestUniqueId );
540
544
if (addToFront ) {
541
545
waitingRequestQueue .addFirst (requestWrapper );
@@ -649,13 +653,12 @@ private ApiFuture<AppendRowsResponse> appendInternal(
649
653
writerId ));
650
654
return requestWrapper .appendResult ;
651
655
}
652
- RequestProfiler .REQUEST_PROFILER_SINGLETON .startOperation (
653
- RequestProfiler .OperationName .WAIT_QUEUE , requestUniqueId );
656
+ requestProfilerHook .startOperation (RequestProfiler .OperationName .WAIT_QUEUE , requestUniqueId );
654
657
++this .inflightRequests ;
655
658
this .inflightBytes += requestWrapper .messageSize ;
656
659
waitingRequestQueue .addLast (requestWrapper );
657
660
hasMessageInWaitingQueue .signal ();
658
- RequestProfiler . REQUEST_PROFILER_SINGLETON .startOperation (
661
+ requestProfilerHook .startOperation (
659
662
RequestProfiler .OperationName .WAIT_INFLIGHT_QUOTA , requestUniqueId );
660
663
try {
661
664
maybeWaitForInflightQuota ();
@@ -665,7 +668,7 @@ private ApiFuture<AppendRowsResponse> appendInternal(
665
668
this .inflightBytes -= requestWrapper .messageSize ;
666
669
throw ex ;
667
670
}
668
- RequestProfiler . REQUEST_PROFILER_SINGLETON .endOperation (
671
+ requestProfilerHook .endOperation (
669
672
RequestProfiler .OperationName .WAIT_INFLIGHT_QUOTA , requestUniqueId );
670
673
return requestWrapper .appendResult ;
671
674
} finally {
@@ -831,10 +834,10 @@ private void appendLoop() {
831
834
while (!inflightRequestQueue .isEmpty ()) {
832
835
AppendRequestAndResponse requestWrapper = inflightRequestQueue .pollLast ();
833
836
// Consider the backend latency as completed for the current request.
834
- RequestProfiler . REQUEST_PROFILER_SINGLETON .endOperation (
837
+ requestProfilerHook .endOperation (
835
838
RequestProfiler .OperationName .RESPONSE_LATENCY , requestWrapper .requestUniqueId );
836
839
requestWrapper .requestSendTimeStamp = null ;
837
- RequestProfiler . REQUEST_PROFILER_SINGLETON .startOperation (
840
+ requestProfilerHook .startOperation (
838
841
RequestProfiler .OperationName .WAIT_QUEUE , requestWrapper .requestUniqueId );
839
842
waitingRequestQueue .addFirst (requestWrapper );
840
843
}
@@ -845,7 +848,7 @@ private void appendLoop() {
845
848
}
846
849
while (!this .waitingRequestQueue .isEmpty ()) {
847
850
AppendRequestAndResponse requestWrapper = this .waitingRequestQueue .pollFirst ();
848
- RequestProfiler . REQUEST_PROFILER_SINGLETON .endOperation (
851
+ requestProfilerHook .endOperation (
849
852
RequestProfiler .OperationName .WAIT_QUEUE , requestWrapper .requestUniqueId );
850
853
waitForBackoffIfNecessary (requestWrapper );
851
854
this .inflightRequestQueue .add (requestWrapper );
@@ -931,7 +934,7 @@ private void appendLoop() {
931
934
}
932
935
firstRequestForTableOrSchemaSwitch = false ;
933
936
934
- RequestProfiler . REQUEST_PROFILER_SINGLETON .startOperation (
937
+ requestProfilerHook .startOperation (
935
938
RequestProfiler .OperationName .RESPONSE_LATENCY , requestUniqueId );
936
939
937
940
// Send should only throw an exception if there is a problem with the request. The catch
@@ -1212,7 +1215,7 @@ private void requestCallback(AppendRowsResponse response) {
1212
1215
}
1213
1216
if (!this .inflightRequestQueue .isEmpty ()) {
1214
1217
requestWrapper = pollFirstInflightRequestQueue ();
1215
- RequestProfiler . REQUEST_PROFILER_SINGLETON .endOperation (
1218
+ requestProfilerHook .endOperation (
1216
1219
RequestProfiler .OperationName .RESPONSE_LATENCY , requestWrapper .requestUniqueId );
1217
1220
} else if (inflightCleanuped ) {
1218
1221
// It is possible when requestCallback is called, the inflight queue is already drained
@@ -1277,7 +1280,7 @@ private void requestCallback(AppendRowsResponse response) {
1277
1280
requestWrapper .appendResult .set (response );
1278
1281
}
1279
1282
} finally {
1280
- RequestProfiler . REQUEST_PROFILER_SINGLETON .endOperation (
1283
+ requestProfilerHook .endOperation (
1281
1284
RequestProfiler .OperationName .TOTAL_LATENCY , requestWrapper .requestUniqueId );
1282
1285
}
1283
1286
});
0 commit comments