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,71 @@ 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
+ int batchSize = 1 ;
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 .setBatchSize (batchSize );
226
+ this .tracingInfo .setLoadBalancingPolicy (manager .loadBalancingPolicy ());
227
+ this .tracingInfo .setSpeculativeExecutionPolicy (manager .speculativeExecutionPolicy ());
228
+ if (statementType != null ) this .tracingInfo .setStatementType (statementType );
229
+ if (statementText != null ) this .tracingInfo .setStatement (statementText , STATEMENT_MAX_LENGTH );
230
+ if (keyspace != null ) this .tracingInfo .setKeyspace (keyspace );
231
+ if (partitionKey != null ) this .tracingInfo .setPartitionKey (partitionKey );
232
+ if (table != null ) this .tracingInfo .setTable (table );
233
+ if (operationType != null ) this .tracingInfo .setOperationType (operationType );
166
234
}
167
235
168
236
void sendRequest () {
@@ -282,6 +350,14 @@ private void setFinalResult(
282
350
}
283
351
callback .onSet (connection , response , info , statement , System .nanoTime () - startTime );
284
352
353
+ if (response .type == Message .Response .Type .RESULT ) {
354
+ Responses .Result rm = (Responses .Result ) response ;
355
+ if (rm .kind == Responses .Result .Kind .ROWS ) {
356
+ Responses .Result .Rows r = (Responses .Result .Rows ) rm ;
357
+ tracingInfo .setRowsCount (r .data .size ());
358
+ }
359
+ }
360
+ tracingInfo .setQueryPaged (info .getPagingState () != null );
285
361
tracingInfo .setStatus (
286
362
response .type == Message .Response .Type .ERROR
287
363
? TracingInfo .StatusCode .ERROR
@@ -336,6 +412,7 @@ private void setFinalException(
336
412
// Triggered when an execution reaches the end of the query plan.
337
413
// This is only a failure if there are no other running executions.
338
414
private void reportNoMoreHosts (SpeculativeExecution execution ) {
415
+ execution .parentTracingInfo .setAttemptCount (execution .retryCount () + 1 );
339
416
execution .parentTracingInfo .setStatus (TracingInfo .StatusCode .ERROR );
340
417
execution .parentTracingInfo .tracingFinished ();
341
418
runningExecutions .remove (execution );
@@ -460,6 +537,10 @@ private boolean query(final Host host) {
460
537
461
538
currentChildTracingInfo = manager .getTracingInfoFactory ().buildTracingInfo (parentTracingInfo );
462
539
currentChildTracingInfo .setNameAndStartTime ("attempt" );
540
+ InetSocketAddress hostAddress = host .getEndPoint ().resolve ();
541
+ currentChildTracingInfo .setPeerName (hostAddress .getHostName ());
542
+ currentChildTracingInfo .setPeerIP (hostAddress .getAddress ());
543
+ currentChildTracingInfo .setPeerPort (hostAddress .getPort ());
463
544
464
545
if (allowSpeculativeExecutions && nextExecutionScheduled .compareAndSet (false , true ))
465
546
scheduleExecution (speculativeExecutionPlan .nextExecution (host ));
@@ -679,6 +760,7 @@ void cancel() {
679
760
CancelledSpeculativeExecutionException .INSTANCE ,
680
761
System .nanoTime () - startTime );
681
762
}
763
+ parentTracingInfo .setAttemptCount (previous .retryCount + 1 );
682
764
parentTracingInfo .setStatus (TracingInfo .StatusCode .OK );
683
765
parentTracingInfo .tracingFinished ();
684
766
return ;
@@ -693,6 +775,7 @@ void cancel() {
693
775
CancelledSpeculativeExecutionException .INSTANCE ,
694
776
System .nanoTime () - startTime );
695
777
}
778
+ parentTracingInfo .setAttemptCount (previous .retryCount + 1 );
696
779
parentTracingInfo .setStatus (TracingInfo .StatusCode .OK );
697
780
parentTracingInfo .tracingFinished ();
698
781
return ;
@@ -710,6 +793,7 @@ public Message.Request request() {
710
793
@ Override
711
794
public void onSet (
712
795
Connection connection , Message .Response response , long latency , int retryCount ) {
796
+ currentChildTracingInfo .setShardID (connection .shardId ());
713
797
currentChildTracingInfo .setStatus (TracingInfo .StatusCode .OK );
714
798
currentChildTracingInfo .tracingFinished ();
715
799
@@ -1021,6 +1105,7 @@ public boolean onTimeout(Connection connection, long latency, int retryCount) {
1021
1105
@ Override
1022
1106
public void onException (
1023
1107
Connection connection , Exception exception , long latency , int retryCount ) {
1108
+ currentChildTracingInfo .setShardID (connection .shardId ());
1024
1109
currentChildTracingInfo .recordException (exception );
1025
1110
currentChildTracingInfo .setStatus (TracingInfo .StatusCode .ERROR );
1026
1111
currentChildTracingInfo .tracingFinished ();
@@ -1062,6 +1147,7 @@ public void onException(
1062
1147
1063
1148
@ Override
1064
1149
public boolean onTimeout (Connection connection , long latency , int retryCount ) {
1150
+ currentChildTracingInfo .setShardID (connection .shardId ());
1065
1151
currentChildTracingInfo .setStatus (TracingInfo .StatusCode .ERROR , "timeout" );
1066
1152
currentChildTracingInfo .tracingFinished ();
1067
1153
@@ -1106,12 +1192,14 @@ public int retryCount() {
1106
1192
}
1107
1193
1108
1194
private void setFinalException (Connection connection , Exception exception ) {
1195
+ parentTracingInfo .setAttemptCount (retryCount () + 1 );
1109
1196
parentTracingInfo .setStatus (TracingInfo .StatusCode .ERROR );
1110
1197
parentTracingInfo .tracingFinished ();
1111
1198
RequestHandler .this .setFinalException (this , connection , exception );
1112
1199
}
1113
1200
1114
1201
private void setFinalResult (Connection connection , Message .Response response ) {
1202
+ parentTracingInfo .setAttemptCount (retryCount () + 1 );
1115
1203
parentTracingInfo .setStatus (TracingInfo .StatusCode .OK );
1116
1204
parentTracingInfo .tracingFinished ();
1117
1205
RequestHandler .this .setFinalResult (this , connection , response );
0 commit comments