48
48
import com .google .common .util .concurrent .ListenableFuture ;
49
49
import io .netty .util .Timeout ;
50
50
import io .netty .util .TimerTask ;
51
+ import java .net .InetSocketAddress ;
51
52
import java .nio .ByteBuffer ;
52
53
import java .util .Collections ;
53
54
import java .util .Iterator ;
@@ -74,6 +75,10 @@ class RequestHandler {
74
75
private static final QueryLogger QUERY_LOGGER = QueryLogger .builder ().build ();
75
76
static final String DISABLE_QUERY_WARNING_LOGS = "com.datastax.driver.DISABLE_QUERY_WARNING_LOGS" ;
76
77
78
+ private static final int STATEMENT_MAX_LENGTH = 1000 ;
79
+ private static final int PARTITION_KEY_MAX_LENGTH = 1000 ;
80
+ private static final int REPLICAS_MAX_LENGTH = 1000 ;
81
+
77
82
final String id ;
78
83
79
84
private final SessionManager manager ;
@@ -161,8 +166,72 @@ public RequestHandler(
161
166
this .timerContext = metricsEnabled () ? metrics ().getRequestsTimer ().time () : null ;
162
167
this .startTime = System .nanoTime ();
163
168
169
+ ConsistencyLevel consistency = statement .getConsistencyLevel ();
170
+ if (consistency == null ) consistency = Statement .DEFAULT .getConsistencyLevel ();
171
+
172
+ String statementType = null ;
173
+ String statementText = null ;
174
+ Integer batchSize = null ;
175
+
176
+ String keyspace = null ;
177
+ String partitionKey = null ;
178
+ String table = null ;
179
+ String operationType = null ;
180
+
181
+ if (statement instanceof BatchStatement ) {
182
+ statementType = "batch" ;
183
+ batchSize = ((BatchStatement ) statement ).size ();
184
+ StringBuilder statementTextBuilder = new StringBuilder (STATEMENT_MAX_LENGTH );
185
+ for (Statement subStatement : ((BatchStatement ) statement ).getStatements ()) {
186
+ if (subStatement instanceof BoundStatement )
187
+ statementTextBuilder .append (((BoundStatement ) subStatement ).statement .getQueryString ());
188
+ else statementTextBuilder .append (subStatement .toString ());
189
+ }
190
+ statementText = statementTextBuilder .toString ();
191
+ } else if (statement instanceof BoundStatement ) {
192
+ statementType = "prepared" ;
193
+ statementText = ((BoundStatement ) statement ).statement .getQueryString ();
194
+ keyspace = ((BoundStatement ) statement ).getKeyspace ();
195
+ operationType = ((BoundStatement ) statement ).getOperationType ();
196
+
197
+ ColumnDefinitions boundColumns =
198
+ ((BoundStatement ) statement ).statement .getPreparedId ().boundValuesMetadata .variables ;
199
+
200
+ StringBuilder partitionKeyBuilder = new StringBuilder (PARTITION_KEY_MAX_LENGTH );
201
+ int [] rkIndexes = ((BoundStatement ) statement ).statement .getPreparedId ().routingKeyIndexes ;
202
+ if (rkIndexes != null ) {
203
+ for (int i : rkIndexes ) {
204
+ Object value = ((BoundStatement ) statement ).getObject (i );
205
+ String valueString = (value == null ) ? "NULL" : value .toString ();
206
+ if (partitionKeyBuilder .length () > 0 ) partitionKeyBuilder .append (", " );
207
+ String columnName = boundColumns .getName (i );
208
+ partitionKeyBuilder .append (columnName );
209
+ partitionKeyBuilder .append ('=' );
210
+ partitionKeyBuilder .append (valueString );
211
+ }
212
+ }
213
+ partitionKey = partitionKeyBuilder .toString ();
214
+
215
+ if (boundColumns .size () > 0 ) table = boundColumns .getTable (0 );
216
+ } else if (statement instanceof RegularStatement ) {
217
+ statementType = "regular" ;
218
+ statementText = ((RegularStatement ) statement ).toString ();
219
+ }
220
+
164
221
this .tracingInfo = tracingInfo ;
165
222
this .tracingInfo .setNameAndStartTime ("request" );
223
+ this .tracingInfo .setConsistencyLevel (consistency );
224
+ this .tracingInfo .setRetryPolicy (retryPolicy ());
225
+ this .tracingInfo .setLoadBalancingPolicy (manager .loadBalancingPolicy ());
226
+ this .tracingInfo .setSpeculativeExecutionPolicy (manager .speculativeExecutionPolicy ());
227
+ if (statement .getFetchSize () > 0 ) this .tracingInfo .setFetchSize (statement .getFetchSize ());
228
+ if (statementType != null ) this .tracingInfo .setStatementType (statementType );
229
+ if (statementText != null ) this .tracingInfo .setStatement (statementText , STATEMENT_MAX_LENGTH );
230
+ if (batchSize != null ) this .tracingInfo .setBatchSize (batchSize );
231
+ if (keyspace != null ) this .tracingInfo .setKeyspace (keyspace );
232
+ if (partitionKey != null ) this .tracingInfo .setPartitionKey (partitionKey );
233
+ if (table != null ) this .tracingInfo .setTable (table );
234
+ if (operationType != null ) this .tracingInfo .setOperationType (operationType );
166
235
}
167
236
168
237
void sendRequest () {
@@ -282,6 +351,14 @@ private void setFinalResult(
282
351
}
283
352
callback .onSet (connection , response , info , statement , System .nanoTime () - startTime );
284
353
354
+ if (response .type == Message .Response .Type .RESULT ) {
355
+ Responses .Result rm = (Responses .Result ) response ;
356
+ if (rm .kind == Responses .Result .Kind .ROWS ) {
357
+ Responses .Result .Rows r = (Responses .Result .Rows ) rm ;
358
+ tracingInfo .setRowsCount (r .data .size ());
359
+ tracingInfo .setHasMorePages (r .metadata .pagingState != null );
360
+ }
361
+ }
285
362
tracingInfo .setStatus (
286
363
response .type == Message .Response .Type .ERROR
287
364
? TracingInfo .StatusCode .ERROR
@@ -336,6 +413,7 @@ private void setFinalException(
336
413
// Triggered when an execution reaches the end of the query plan.
337
414
// This is only a failure if there are no other running executions.
338
415
private void reportNoMoreHosts (SpeculativeExecution execution ) {
416
+ execution .parentTracingInfo .setAttemptCount (execution .retryCount () + 1 );
339
417
execution .parentTracingInfo .setStatus (TracingInfo .StatusCode .ERROR );
340
418
execution .parentTracingInfo .tracingFinished ();
341
419
runningExecutions .remove (execution );
@@ -460,6 +538,10 @@ private boolean query(final Host host) {
460
538
461
539
currentChildTracingInfo = manager .getTracingInfoFactory ().buildTracingInfo (parentTracingInfo );
462
540
currentChildTracingInfo .setNameAndStartTime ("attempt" );
541
+ InetSocketAddress hostAddress = host .getEndPoint ().resolve ();
542
+ currentChildTracingInfo .setPeerName (hostAddress .getHostName ());
543
+ currentChildTracingInfo .setPeerIP (hostAddress .getAddress ());
544
+ currentChildTracingInfo .setPeerPort (hostAddress .getPort ());
463
545
464
546
if (allowSpeculativeExecutions && nextExecutionScheduled .compareAndSet (false , true ))
465
547
scheduleExecution (speculativeExecutionPlan .nextExecution (host ));
@@ -679,6 +761,7 @@ void cancel() {
679
761
CancelledSpeculativeExecutionException .INSTANCE ,
680
762
System .nanoTime () - startTime );
681
763
}
764
+ parentTracingInfo .setAttemptCount (previous .retryCount + 1 );
682
765
parentTracingInfo .setStatus (TracingInfo .StatusCode .OK );
683
766
parentTracingInfo .tracingFinished ();
684
767
return ;
@@ -693,6 +776,7 @@ void cancel() {
693
776
CancelledSpeculativeExecutionException .INSTANCE ,
694
777
System .nanoTime () - startTime );
695
778
}
779
+ parentTracingInfo .setAttemptCount (previous .retryCount + 1 );
696
780
parentTracingInfo .setStatus (TracingInfo .StatusCode .OK );
697
781
parentTracingInfo .tracingFinished ();
698
782
return ;
@@ -710,6 +794,7 @@ public Message.Request request() {
710
794
@ Override
711
795
public void onSet (
712
796
Connection connection , Message .Response response , long latency , int retryCount ) {
797
+ currentChildTracingInfo .setShardID (connection .shardId ());
713
798
currentChildTracingInfo .setStatus (TracingInfo .StatusCode .OK );
714
799
currentChildTracingInfo .tracingFinished ();
715
800
@@ -1021,6 +1106,7 @@ public boolean onTimeout(Connection connection, long latency, int retryCount) {
1021
1106
@ Override
1022
1107
public void onException (
1023
1108
Connection connection , Exception exception , long latency , int retryCount ) {
1109
+ currentChildTracingInfo .setShardID (connection .shardId ());
1024
1110
currentChildTracingInfo .recordException (exception );
1025
1111
currentChildTracingInfo .setStatus (TracingInfo .StatusCode .ERROR );
1026
1112
currentChildTracingInfo .tracingFinished ();
@@ -1062,6 +1148,7 @@ public void onException(
1062
1148
1063
1149
@ Override
1064
1150
public boolean onTimeout (Connection connection , long latency , int retryCount ) {
1151
+ currentChildTracingInfo .setShardID (connection .shardId ());
1065
1152
currentChildTracingInfo .setStatus (TracingInfo .StatusCode .ERROR , "timeout" );
1066
1153
currentChildTracingInfo .tracingFinished ();
1067
1154
@@ -1106,12 +1193,14 @@ public int retryCount() {
1106
1193
}
1107
1194
1108
1195
private void setFinalException (Connection connection , Exception exception ) {
1196
+ parentTracingInfo .setAttemptCount (retryCount () + 1 );
1109
1197
parentTracingInfo .setStatus (TracingInfo .StatusCode .ERROR );
1110
1198
parentTracingInfo .tracingFinished ();
1111
1199
RequestHandler .this .setFinalException (this , connection , exception );
1112
1200
}
1113
1201
1114
1202
private void setFinalResult (Connection connection , Message .Response response ) {
1203
+ parentTracingInfo .setAttemptCount (retryCount () + 1 );
1115
1204
parentTracingInfo .setStatus (TracingInfo .StatusCode .OK );
1116
1205
parentTracingInfo .tracingFinished ();
1117
1206
RequestHandler .this .setFinalResult (this , connection , response );
0 commit comments