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
@@ -295,8 +371,8 @@ private void setFinalResult(
295
371
System .nanoTime () - startTime , /*unused*/
296
372
0 );
297
373
298
- tracingInfo .setStatus (TracingInfo .StatusCode .ERROR , e .toString ());
299
374
tracingInfo .recordException (e );
375
+ tracingInfo .setStatus (TracingInfo .StatusCode .ERROR , e .toString ());
300
376
tracingInfo .tracingFinished ();
301
377
}
302
378
}
@@ -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 .setRetryCount (execution .retryCount ());
339
416
execution .parentTracingInfo .tracingFinished ();
340
417
runningExecutions .remove (execution );
341
418
if (runningExecutions .isEmpty ())
@@ -459,6 +536,10 @@ private boolean query(final Host host) {
459
536
460
537
currentChildTracingInfo = manager .getTracingInfoFactory ().buildTracingInfo (parentTracingInfo );
461
538
currentChildTracingInfo .setNameAndStartTime ("query" );
539
+ InetSocketAddress hostAddress = host .getEndPoint ().resolve ();
540
+ currentChildTracingInfo .setPeerName (hostAddress .getHostName ());
541
+ currentChildTracingInfo .setPeerIP (hostAddress .getAddress ());
542
+ currentChildTracingInfo .setPeerPort (hostAddress .getPort ());
462
543
463
544
if (allowSpeculativeExecutions && nextExecutionScheduled .compareAndSet (false , true ))
464
545
scheduleExecution (speculativeExecutionPlan .nextExecution (host ));
@@ -678,6 +759,7 @@ void cancel() {
678
759
CancelledSpeculativeExecutionException .INSTANCE ,
679
760
System .nanoTime () - startTime );
680
761
}
762
+ parentTracingInfo .setRetryCount (retryCount ());
681
763
parentTracingInfo .tracingFinished ();
682
764
return ;
683
765
} else if (!previous .inProgress
@@ -691,6 +773,7 @@ void cancel() {
691
773
CancelledSpeculativeExecutionException .INSTANCE ,
692
774
System .nanoTime () - startTime );
693
775
}
776
+ parentTracingInfo .setRetryCount (retryCount ());
694
777
parentTracingInfo .tracingFinished ();
695
778
return ;
696
779
}
@@ -707,6 +790,7 @@ public Message.Request request() {
707
790
@ Override
708
791
public void onSet (
709
792
Connection connection , Message .Response response , long latency , int retryCount ) {
793
+ currentChildTracingInfo .setShardID (connection .shardId ());
710
794
currentChildTracingInfo .tracingFinished ();
711
795
712
796
QueryState queryState = queryStateRef .get ();
@@ -1017,6 +1101,7 @@ public boolean onTimeout(Connection connection, long latency, int retryCount) {
1017
1101
@ Override
1018
1102
public void onException (
1019
1103
Connection connection , Exception exception , long latency , int retryCount ) {
1104
+ currentChildTracingInfo .setShardID (connection .shardId ());
1020
1105
currentChildTracingInfo .tracingFinished ();
1021
1106
1022
1107
QueryState queryState = queryStateRef .get ();
@@ -1056,6 +1141,7 @@ public void onException(
1056
1141
1057
1142
@ Override
1058
1143
public boolean onTimeout (Connection connection , long latency , int retryCount ) {
1144
+ currentChildTracingInfo .setShardID (connection .shardId ());
1059
1145
currentChildTracingInfo .tracingFinished ();
1060
1146
1061
1147
QueryState queryState = queryStateRef .get ();
@@ -1099,11 +1185,13 @@ public int retryCount() {
1099
1185
}
1100
1186
1101
1187
private void setFinalException (Connection connection , Exception exception ) {
1188
+ parentTracingInfo .setRetryCount (retryCount ());
1102
1189
parentTracingInfo .tracingFinished ();
1103
1190
RequestHandler .this .setFinalException (this , connection , exception );
1104
1191
}
1105
1192
1106
1193
private void setFinalResult (Connection connection , Message .Response response ) {
1194
+ parentTracingInfo .setRetryCount (retryCount ());
1107
1195
parentTracingInfo .tracingFinished ();
1108
1196
RequestHandler .this .setFinalResult (this , connection , response );
1109
1197
}
0 commit comments