Skip to content

Commit 77b3f0b

Browse files
committed
Tracing: added various request driver-side tags
All data that constitute added tags are already know by the driver, i.e. they do not require fetching any additional metadata from the cluster.
1 parent 3ec5bcb commit 77b3f0b

File tree

7 files changed

+329
-0
lines changed

7 files changed

+329
-0
lines changed

clirr-ignores.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
Modified by ScyllaDB
1717
-->
1818
<differences>
19+
<difference>
20+
<differenceType>7012</differenceType> <!-- method added to interface -->
21+
<className>com/datastax/driver/core/PreparedStatement</className>
22+
<method>java.lang.String getOperationType()</method>
23+
<justification>New method to get the type of operation performed by this PreparedStatement</justification>
24+
</difference>
1925
<difference>
2026
<differenceType>7012</differenceType> <!-- method added to interface -->
2127
<className>com/datastax/driver/core/Session</className>

driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public class BoundStatement extends Statement
7474

7575
private ByteBuffer routingKey;
7676

77+
private final String operationType;
78+
7779
/**
7880
* Creates a new {@code BoundStatement} from the provided prepared statement.
7981
*
@@ -92,6 +94,7 @@ public BoundStatement(PreparedStatement statement) {
9294
this.setSerialConsistencyLevel(statement.getSerialConsistencyLevel());
9395
if (statement.isTracing()) this.enableTracing();
9496
if (statement.getRetryPolicy() != null) this.setRetryPolicy(statement.getRetryPolicy());
97+
this.operationType = statement.getOperationType();
9598
if (statement.getOutgoingPayload() != null)
9699
this.setOutgoingPayload(statement.getOutgoingPayload());
97100
else
@@ -104,6 +107,10 @@ public BoundStatement(PreparedStatement statement) {
104107
}
105108
}
106109

110+
public String getOperationType() {
111+
return operationType;
112+
}
113+
107114
@Override
108115
public boolean isLWT() {
109116
return statement.isLWT();

driver-core/src/main/java/com/datastax/driver/core/DefaultPreparedStatement.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class DefaultPreparedStatement implements PreparedStatement {
4141
final Cluster cluster;
4242
final boolean isLWT;
4343
final Token.Factory partitioner;
44+
final String operationType;
4445

4546
volatile ByteBuffer routingKey;
4647

@@ -66,6 +67,7 @@ private DefaultPreparedStatement(
6667
this.cluster = cluster;
6768
this.isLWT = isLWT;
6869
this.partitioner = partitioner;
70+
this.operationType = null;
6971
}
7072

7173
static DefaultPreparedStatement fromMessage(
@@ -315,4 +317,10 @@ public Boolean isIdempotent() {
315317
public boolean isLWT() {
316318
return isLWT;
317319
}
320+
321+
/** {@inheritDoc} */
322+
@Override
323+
public String getOperationType() {
324+
return operationType;
325+
}
318326
}

driver-core/src/main/java/com/datastax/driver/core/PreparedStatement.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,7 @@ public interface PreparedStatement {
361361

362362
/** Whether a prepared statement is LWT statement */
363363
public boolean isLWT();
364+
365+
/** Type of prepared operation (e.g. SELECT) */
366+
public String getOperationType();
364367
}

driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import com.google.common.util.concurrent.ListenableFuture;
4949
import io.netty.util.Timeout;
5050
import io.netty.util.TimerTask;
51+
import java.net.InetSocketAddress;
5152
import java.nio.ByteBuffer;
5253
import java.util.Collections;
5354
import java.util.Iterator;
@@ -74,6 +75,10 @@ class RequestHandler {
7475
private static final QueryLogger QUERY_LOGGER = QueryLogger.builder().build();
7576
static final String DISABLE_QUERY_WARNING_LOGS = "com.datastax.driver.DISABLE_QUERY_WARNING_LOGS";
7677

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+
7782
final String id;
7883

7984
private final SessionManager manager;
@@ -161,8 +166,72 @@ public RequestHandler(
161166
this.timerContext = metricsEnabled() ? metrics().getRequestsTimer().time() : null;
162167
this.startTime = System.nanoTime();
163168

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+
164221
this.tracingInfo = tracingInfo;
165222
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);
166235
}
167236

168237
void sendRequest() {
@@ -282,6 +351,14 @@ private void setFinalResult(
282351
}
283352
callback.onSet(connection, response, info, statement, System.nanoTime() - startTime);
284353

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+
}
285362
tracingInfo.setStatus(
286363
response.type == Message.Response.Type.ERROR
287364
? TracingInfo.StatusCode.ERROR
@@ -336,6 +413,7 @@ private void setFinalException(
336413
// Triggered when an execution reaches the end of the query plan.
337414
// This is only a failure if there are no other running executions.
338415
private void reportNoMoreHosts(SpeculativeExecution execution) {
416+
execution.parentTracingInfo.setAttemptCount(execution.retryCount() + 1);
339417
execution.parentTracingInfo.setStatus(TracingInfo.StatusCode.ERROR);
340418
execution.parentTracingInfo.tracingFinished();
341419
runningExecutions.remove(execution);
@@ -460,6 +538,10 @@ private boolean query(final Host host) {
460538

461539
currentChildTracingInfo = manager.getTracingInfoFactory().buildTracingInfo(parentTracingInfo);
462540
currentChildTracingInfo.setNameAndStartTime("attempt");
541+
InetSocketAddress hostAddress = host.getEndPoint().resolve();
542+
currentChildTracingInfo.setPeerName(hostAddress.getHostName());
543+
currentChildTracingInfo.setPeerIP(hostAddress.getAddress());
544+
currentChildTracingInfo.setPeerPort(hostAddress.getPort());
463545

464546
if (allowSpeculativeExecutions && nextExecutionScheduled.compareAndSet(false, true))
465547
scheduleExecution(speculativeExecutionPlan.nextExecution(host));
@@ -679,6 +761,7 @@ void cancel() {
679761
CancelledSpeculativeExecutionException.INSTANCE,
680762
System.nanoTime() - startTime);
681763
}
764+
parentTracingInfo.setAttemptCount(previous.retryCount + 1);
682765
parentTracingInfo.setStatus(TracingInfo.StatusCode.OK);
683766
parentTracingInfo.tracingFinished();
684767
return;
@@ -693,6 +776,7 @@ void cancel() {
693776
CancelledSpeculativeExecutionException.INSTANCE,
694777
System.nanoTime() - startTime);
695778
}
779+
parentTracingInfo.setAttemptCount(previous.retryCount + 1);
696780
parentTracingInfo.setStatus(TracingInfo.StatusCode.OK);
697781
parentTracingInfo.tracingFinished();
698782
return;
@@ -710,6 +794,7 @@ public Message.Request request() {
710794
@Override
711795
public void onSet(
712796
Connection connection, Message.Response response, long latency, int retryCount) {
797+
currentChildTracingInfo.setShardID(connection.shardId());
713798
currentChildTracingInfo.setStatus(TracingInfo.StatusCode.OK);
714799
currentChildTracingInfo.tracingFinished();
715800

@@ -1021,6 +1106,7 @@ public boolean onTimeout(Connection connection, long latency, int retryCount) {
10211106
@Override
10221107
public void onException(
10231108
Connection connection, Exception exception, long latency, int retryCount) {
1109+
currentChildTracingInfo.setShardID(connection.shardId());
10241110
currentChildTracingInfo.recordException(exception);
10251111
currentChildTracingInfo.setStatus(TracingInfo.StatusCode.ERROR);
10261112
currentChildTracingInfo.tracingFinished();
@@ -1062,6 +1148,7 @@ public void onException(
10621148

10631149
@Override
10641150
public boolean onTimeout(Connection connection, long latency, int retryCount) {
1151+
currentChildTracingInfo.setShardID(connection.shardId());
10651152
currentChildTracingInfo.setStatus(TracingInfo.StatusCode.ERROR, "timeout");
10661153
currentChildTracingInfo.tracingFinished();
10671154

@@ -1106,12 +1193,14 @@ public int retryCount() {
11061193
}
11071194

11081195
private void setFinalException(Connection connection, Exception exception) {
1196+
parentTracingInfo.setAttemptCount(retryCount() + 1);
11091197
parentTracingInfo.setStatus(TracingInfo.StatusCode.ERROR);
11101198
parentTracingInfo.tracingFinished();
11111199
RequestHandler.this.setFinalException(this, connection, exception);
11121200
}
11131201

11141202
private void setFinalResult(Connection connection, Message.Response response) {
1203+
parentTracingInfo.setAttemptCount(retryCount() + 1);
11151204
parentTracingInfo.setStatus(TracingInfo.StatusCode.OK);
11161205
parentTracingInfo.tracingFinished();
11171206
RequestHandler.this.setFinalResult(this, connection, response);

driver-core/src/main/java/com/datastax/driver/core/tracing/NoopTracingInfoFactory.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,79 @@
1616

1717
package com.datastax.driver.core.tracing;
1818

19+
import com.datastax.driver.core.ConsistencyLevel;
20+
import com.datastax.driver.core.policies.LoadBalancingPolicy;
21+
import com.datastax.driver.core.policies.RetryPolicy;
22+
import com.datastax.driver.core.policies.SpeculativeExecutionPolicy;
23+
import java.net.InetAddress;
24+
1925
public class NoopTracingInfoFactory implements TracingInfoFactory {
2026

2127
private static class NoopTracingInfo implements TracingInfo {
2228
@Override
2329
public void setNameAndStartTime(String name) {}
2430

31+
@Override
32+
public void setConsistencyLevel(ConsistencyLevel consistency) {}
33+
34+
@Override
35+
public void setStatementType(String statementType) {}
36+
37+
@Override
38+
public void setRetryPolicy(RetryPolicy retryPolicy) {}
39+
40+
@Override
41+
public void setLoadBalancingPolicy(LoadBalancingPolicy loadBalancingPolicy) {};
42+
43+
@Override
44+
public void setSpeculativeExecutionPolicy(
45+
SpeculativeExecutionPolicy speculativeExecutionPolicy) {}
46+
47+
@Override
48+
public void setBatchSize(int batchSize) {}
49+
50+
@Override
51+
public void setAttemptCount(int attemptCount) {}
52+
53+
@Override
54+
public void setShardID(int shardID) {}
55+
56+
@Override
57+
public void setPeerName(String peerName) {}
58+
59+
@Override
60+
public void setPeerIP(InetAddress peerIP) {}
61+
62+
@Override
63+
public void setPeerPort(int peerPort) {}
64+
65+
@Override
66+
public void setFetchSize(int fetchSize) {}
67+
68+
@Override
69+
public void setHasMorePages(boolean hasMorePages) {}
70+
71+
@Override
72+
public void setRowsCount(int rowsCount) {}
73+
74+
@Override
75+
public void setStatement(String statement, int limit) {}
76+
77+
@Override
78+
public void setKeyspace(String keyspace) {}
79+
80+
@Override
81+
public void setPartitionKey(String partitionKey) {}
82+
83+
@Override
84+
public void setTable(String table) {}
85+
86+
@Override
87+
public void setOperationType(String operationType) {}
88+
89+
@Override
90+
public void setReplicas(String replicas) {}
91+
2592
@Override
2693
public void recordException(Exception exception) {}
2794

0 commit comments

Comments
 (0)