Skip to content

Commit f4a0016

Browse files
committed
Tracing: added various request driver-side tags
All data that constitute added tags are collected offline, i.e. not fetched from the cluster, but known instantly by the driver.
1 parent c60c4ff commit f4a0016

File tree

7 files changed

+318
-0
lines changed

7 files changed

+318
-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,71 @@ 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+
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+
164221
this.tracingInfo = tracingInfo;
165222
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);
166234
}
167235

168236
void sendRequest() {
@@ -282,6 +350,14 @@ private void setFinalResult(
282350
}
283351
callback.onSet(connection, response, info, statement, System.nanoTime() - startTime);
284352

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);
285361
tracingInfo.setStatus(
286362
response.type == Message.Response.Type.ERROR
287363
? TracingInfo.StatusCode.ERROR
@@ -298,6 +374,7 @@ private void setFinalResult(
298374
tracingInfo.recordException(e);
299375
tracingInfo.setStatus(TracingInfo.StatusCode.ERROR, e.toString());
300376
tracingInfo.recordException(e);
377+
tracingInfo.setStatus(TracingInfo.StatusCode.ERROR, e.toString());
301378
tracingInfo.tracingFinished();
302379
}
303380
}
@@ -337,6 +414,7 @@ private void setFinalException(
337414
// Triggered when an execution reaches the end of the query plan.
338415
// This is only a failure if there are no other running executions.
339416
private void reportNoMoreHosts(SpeculativeExecution execution) {
417+
execution.parentTracingInfo.setRetryCount(execution.retryCount());
340418
execution.parentTracingInfo.setStatus(TracingInfo.StatusCode.ERROR);
341419
execution.parentTracingInfo.tracingFinished();
342420
runningExecutions.remove(execution);
@@ -461,6 +539,10 @@ private boolean query(final Host host) {
461539

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

465547
if (allowSpeculativeExecutions && nextExecutionScheduled.compareAndSet(false, true))
466548
scheduleExecution(speculativeExecutionPlan.nextExecution(host));
@@ -680,6 +762,7 @@ void cancel() {
680762
CancelledSpeculativeExecutionException.INSTANCE,
681763
System.nanoTime() - startTime);
682764
}
765+
parentTracingInfo.setRetryCount(retryCount());
683766
parentTracingInfo.setStatus(TracingInfo.StatusCode.OK);
684767
parentTracingInfo.tracingFinished();
685768
return;
@@ -694,6 +777,7 @@ void cancel() {
694777
CancelledSpeculativeExecutionException.INSTANCE,
695778
System.nanoTime() - startTime);
696779
}
780+
parentTracingInfo.setRetryCount(retryCount());
697781
parentTracingInfo.setStatus(TracingInfo.StatusCode.OK);
698782
parentTracingInfo.tracingFinished();
699783
return;
@@ -711,6 +795,7 @@ public Message.Request request() {
711795
@Override
712796
public void onSet(
713797
Connection connection, Message.Response response, long latency, int retryCount) {
798+
currentChildTracingInfo.setShardID(connection.shardId());
714799
currentChildTracingInfo.setStatus(TracingInfo.StatusCode.OK);
715800
currentChildTracingInfo.tracingFinished();
716801

@@ -1022,6 +1107,7 @@ public boolean onTimeout(Connection connection, long latency, int retryCount) {
10221107
@Override
10231108
public void onException(
10241109
Connection connection, Exception exception, long latency, int retryCount) {
1110+
currentChildTracingInfo.setShardID(connection.shardId());
10251111
currentChildTracingInfo.recordException(exception);
10261112
currentChildTracingInfo.setStatus(TracingInfo.StatusCode.ERROR);
10271113
currentChildTracingInfo.tracingFinished();
@@ -1063,6 +1149,7 @@ public void onException(
10631149

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

@@ -1107,12 +1194,14 @@ public int retryCount() {
11071194
}
11081195

11091196
private void setFinalException(Connection connection, Exception exception) {
1197+
parentTracingInfo.setRetryCount(retryCount());
11101198
parentTracingInfo.setStatus(TracingInfo.StatusCode.ERROR);
11111199
parentTracingInfo.tracingFinished();
11121200
RequestHandler.this.setFinalException(this, connection, exception);
11131201
}
11141202

11151203
private void setFinalResult(Connection connection, Message.Response response) {
1204+
parentTracingInfo.setRetryCount(retryCount());
11161205
parentTracingInfo.setStatus(TracingInfo.StatusCode.OK);
11171206
parentTracingInfo.tracingFinished();
11181207
RequestHandler.this.setFinalResult(this, connection, response);

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,76 @@
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 setRetryCount(int retryCount) {}
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 setQueryPaged(Boolean queryPaged) {}
67+
68+
@Override
69+
public void setRowsCount(int rowsCount) {}
70+
71+
@Override
72+
public void setStatement(String statement, int limit) {}
73+
74+
@Override
75+
public void setKeyspace(String keyspace) {}
76+
77+
@Override
78+
public void setPartitionKey(String partitionKey) {}
79+
80+
@Override
81+
public void setTable(String table) {}
82+
83+
@Override
84+
public void setOperationType(String operationType) {}
85+
86+
@Override
87+
public void setReplicas(String replicas) {}
88+
2589
@Override
2690
public void recordException(Exception exception) {}
2791

0 commit comments

Comments
 (0)