Skip to content

Commit d41834f

Browse files
committed
Tracing: added request status codes and exceptions
After a request is completed, its status code (OK or ERROR) is added to trace data, as well as exception information (if one occured).
1 parent 57aa86f commit d41834f

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ private void setFinalResult(
281281
logServerWarnings(response.warnings);
282282
}
283283
callback.onSet(connection, response, info, statement, System.nanoTime() - startTime);
284+
285+
tracingInfo.setStatus(
286+
response.type == Message.Response.Type.ERROR
287+
? TracingInfo.StatusCode.ERROR
288+
: TracingInfo.StatusCode.OK);
284289
tracingInfo.tracingFinished();
285290
} catch (Exception e) {
286291
callback.onException(
@@ -290,6 +295,8 @@ private void setFinalResult(
290295
System.nanoTime() - startTime, /*unused*/
291296
0);
292297

298+
tracingInfo.recordException(e);
299+
tracingInfo.setStatus(TracingInfo.StatusCode.ERROR, e.toString());
293300
tracingInfo.tracingFinished();
294301
}
295302
}
@@ -315,6 +322,8 @@ private void setFinalException(
315322

316323
cancelPendingExecutions(execution);
317324

325+
tracingInfo.recordException(exception);
326+
tracingInfo.setStatus(TracingInfo.StatusCode.ERROR, exception.toString());
318327
tracingInfo.tracingFinished();
319328

320329
try {
@@ -327,6 +336,7 @@ private void setFinalException(
327336
// Triggered when an execution reaches the end of the query plan.
328337
// This is only a failure if there are no other running executions.
329338
private void reportNoMoreHosts(SpeculativeExecution execution) {
339+
execution.parentTracingInfo.setStatus(TracingInfo.StatusCode.ERROR);
330340
execution.parentTracingInfo.tracingFinished();
331341
runningExecutions.remove(execution);
332342
if (runningExecutions.isEmpty())
@@ -669,6 +679,7 @@ void cancel() {
669679
CancelledSpeculativeExecutionException.INSTANCE,
670680
System.nanoTime() - startTime);
671681
}
682+
parentTracingInfo.setStatus(TracingInfo.StatusCode.OK);
672683
parentTracingInfo.tracingFinished();
673684
return;
674685
} else if (!previous.inProgress
@@ -682,6 +693,7 @@ void cancel() {
682693
CancelledSpeculativeExecutionException.INSTANCE,
683694
System.nanoTime() - startTime);
684695
}
696+
parentTracingInfo.setStatus(TracingInfo.StatusCode.OK);
685697
parentTracingInfo.tracingFinished();
686698
return;
687699
}
@@ -698,6 +710,7 @@ public Message.Request request() {
698710
@Override
699711
public void onSet(
700712
Connection connection, Message.Response response, long latency, int retryCount) {
713+
currentChildTracingInfo.setStatus(TracingInfo.StatusCode.OK);
701714
currentChildTracingInfo.tracingFinished();
702715

703716
QueryState queryState = queryStateRef.get();
@@ -1008,6 +1021,8 @@ public boolean onTimeout(Connection connection, long latency, int retryCount) {
10081021
@Override
10091022
public void onException(
10101023
Connection connection, Exception exception, long latency, int retryCount) {
1024+
currentChildTracingInfo.recordException(exception);
1025+
currentChildTracingInfo.setStatus(TracingInfo.StatusCode.ERROR);
10111026
currentChildTracingInfo.tracingFinished();
10121027

10131028
QueryState queryState = queryStateRef.get();
@@ -1047,6 +1062,7 @@ public void onException(
10471062

10481063
@Override
10491064
public boolean onTimeout(Connection connection, long latency, int retryCount) {
1065+
currentChildTracingInfo.setStatus(TracingInfo.StatusCode.ERROR, "timeout");
10501066
currentChildTracingInfo.tracingFinished();
10511067

10521068
QueryState queryState = queryStateRef.get();
@@ -1090,11 +1106,13 @@ public int retryCount() {
10901106
}
10911107

10921108
private void setFinalException(Connection connection, Exception exception) {
1109+
parentTracingInfo.setStatus(TracingInfo.StatusCode.ERROR);
10931110
parentTracingInfo.tracingFinished();
10941111
RequestHandler.this.setFinalException(this, connection, exception);
10951112
}
10961113

10971114
private void setFinalResult(Connection connection, Message.Response response) {
1115+
parentTracingInfo.setStatus(TracingInfo.StatusCode.OK);
10981116
parentTracingInfo.tracingFinished();
10991117
RequestHandler.this.setFinalResult(this, connection, response);
11001118
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ private static class NoopTracingInfo implements TracingInfo {
2222
@Override
2323
public void setNameAndStartTime(String name) {}
2424

25+
@Override
26+
public void recordException(Exception exception) {}
27+
28+
@Override
29+
public void setStatus(StatusCode code, String description) {}
30+
31+
@Override
32+
public void setStatus(StatusCode code) {}
33+
2534
@Override
2635
public void tracingFinished() {}
2736
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
*/
2323
public interface TracingInfo {
2424

25+
/** Final status of the traced execution. */
26+
enum StatusCode {
27+
OK,
28+
ERROR,
29+
}
30+
2531
/**
2632
* Starts a span corresponding to this {@link TracingInfo} object. Must be called exactly once,
2733
* before any other method, at the beginning of the traced execution.
@@ -30,6 +36,28 @@ public interface TracingInfo {
3036
*/
3137
void setNameAndStartTime(String name);
3238

39+
/**
40+
* Records in the trace that the provided exception occured.
41+
*
42+
* @param exception the exception to be recorded.
43+
*/
44+
void recordException(Exception exception);
45+
46+
/**
47+
* Sets the final status of the traced execution.
48+
*
49+
* @param code the status code to be set.
50+
*/
51+
void setStatus(StatusCode code);
52+
53+
/**
54+
* Sets the final status of the traced execution, with additional description.
55+
*
56+
* @param code the status code to be set.
57+
* @param description the additional description of the status.
58+
*/
59+
void setStatus(StatusCode code, String description);
60+
3361
/** Must be always called exactly once at the logical end of traced execution. */
3462
void tracingFinished();
3563
}

0 commit comments

Comments
 (0)