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,70 @@ 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
+ if (statementType != null ) this .tracingInfo .setStatementType (statementType );
228
+ if (statementText != null ) this .tracingInfo .setStatement (statementText , STATEMENT_MAX_LENGTH );
229
+ if (keyspace != null ) this .tracingInfo .setKeyspace (keyspace );
230
+ if (partitionKey != null ) this .tracingInfo .setPartitionKey (partitionKey );
231
+ if (table != null ) this .tracingInfo .setTable (table );
232
+ if (operationType != null ) this .tracingInfo .setOperationType (operationType );
166
233
}
167
234
168
235
void sendRequest () {
@@ -281,6 +348,15 @@ private void setFinalResult(
281
348
logServerWarnings (response .warnings );
282
349
}
283
350
callback .onSet (connection , response , info , statement , System .nanoTime () - startTime );
351
+
352
+ if (response .type == Message .Response .Type .RESULT ) {
353
+ Responses .Result rm = (Responses .Result ) response ;
354
+ if (rm .kind == Responses .Result .Kind .ROWS ) {
355
+ Responses .Result .Rows r = (Responses .Result .Rows ) rm ;
356
+ tracingInfo .setRowsCount (r .data .size ());
357
+ }
358
+ }
359
+ tracingInfo .setQueryPaged (info .getPagingState () != null );
284
360
tracingInfo .setStatus (TracingInfo .StatusCode .OK );
285
361
tracingInfo .tracingFinished ();
286
362
} catch (Exception e ) {
@@ -291,8 +367,8 @@ private void setFinalResult(
291
367
System .nanoTime () - startTime , /*unused*/
292
368
0 );
293
369
294
- tracingInfo .setStatus (TracingInfo .StatusCode .ERROR , e .toString ());
295
370
tracingInfo .recordException (e );
371
+ tracingInfo .setStatus (TracingInfo .StatusCode .ERROR , e .toString ());
296
372
tracingInfo .tracingFinished ();
297
373
}
298
374
}
@@ -332,6 +408,7 @@ private void setFinalException(
332
408
// Triggered when an execution reaches the end of the query plan.
333
409
// This is only a failure if there are no other running executions.
334
410
private void reportNoMoreHosts (SpeculativeExecution execution ) {
411
+ execution .parentTracingInfo .setRetryCount (execution .retryCount ());
335
412
execution .parentTracingInfo .tracingFinished ();
336
413
runningExecutions .remove (execution );
337
414
if (runningExecutions .isEmpty ())
@@ -455,6 +532,10 @@ private boolean query(final Host host) {
455
532
456
533
currentChildTracingInfo = manager .getTracingInfoFactory ().buildTracingInfo (parentTracingInfo );
457
534
currentChildTracingInfo .setNameAndStartTime ("query" );
535
+ InetSocketAddress hostAddress = host .getEndPoint ().resolve ();
536
+ currentChildTracingInfo .setPeerName (hostAddress .getHostName ());
537
+ currentChildTracingInfo .setPeerIP (hostAddress .getAddress ());
538
+ currentChildTracingInfo .setPeerPort (hostAddress .getPort ());
458
539
459
540
if (allowSpeculativeExecutions && nextExecutionScheduled .compareAndSet (false , true ))
460
541
scheduleExecution (speculativeExecutionPlan .nextExecution (host ));
@@ -674,6 +755,7 @@ void cancel() {
674
755
CancelledSpeculativeExecutionException .INSTANCE ,
675
756
System .nanoTime () - startTime );
676
757
}
758
+ parentTracingInfo .setRetryCount (retryCount ());
677
759
parentTracingInfo .tracingFinished ();
678
760
return ;
679
761
} else if (!previous .inProgress
@@ -687,6 +769,7 @@ void cancel() {
687
769
CancelledSpeculativeExecutionException .INSTANCE ,
688
770
System .nanoTime () - startTime );
689
771
}
772
+ parentTracingInfo .setRetryCount (retryCount ());
690
773
parentTracingInfo .tracingFinished ();
691
774
return ;
692
775
}
@@ -703,6 +786,7 @@ public Message.Request request() {
703
786
@ Override
704
787
public void onSet (
705
788
Connection connection , Message .Response response , long latency , int retryCount ) {
789
+ currentChildTracingInfo .setShardID (connection .shardId ());
706
790
currentChildTracingInfo .tracingFinished ();
707
791
708
792
QueryState queryState = queryStateRef .get ();
@@ -1013,6 +1097,7 @@ public boolean onTimeout(Connection connection, long latency, int retryCount) {
1013
1097
@ Override
1014
1098
public void onException (
1015
1099
Connection connection , Exception exception , long latency , int retryCount ) {
1100
+ currentChildTracingInfo .setShardID (connection .shardId ());
1016
1101
currentChildTracingInfo .tracingFinished ();
1017
1102
1018
1103
QueryState queryState = queryStateRef .get ();
@@ -1052,6 +1137,7 @@ public void onException(
1052
1137
1053
1138
@ Override
1054
1139
public boolean onTimeout (Connection connection , long latency , int retryCount ) {
1140
+ currentChildTracingInfo .setShardID (connection .shardId ());
1055
1141
currentChildTracingInfo .tracingFinished ();
1056
1142
1057
1143
QueryState queryState = queryStateRef .get ();
@@ -1095,11 +1181,13 @@ public int retryCount() {
1095
1181
}
1096
1182
1097
1183
private void setFinalException (Connection connection , Exception exception ) {
1184
+ parentTracingInfo .setRetryCount (retryCount ());
1098
1185
parentTracingInfo .tracingFinished ();
1099
1186
RequestHandler .this .setFinalException (this , connection , exception );
1100
1187
}
1101
1188
1102
1189
private void setFinalResult (Connection connection , Message .Response response ) {
1190
+ parentTracingInfo .setRetryCount (retryCount ());
1103
1191
parentTracingInfo .tracingFinished ();
1104
1192
RequestHandler .this .setFinalResult (this , connection , response );
1105
1193
}
0 commit comments