use of com.datastax.oss.driver.api.core.cql.TraceEvent in project java-driver by datastax.
the class QueryTraceFetcher method buildTrace.
private QueryTrace buildTrace(Row sessionRow, Iterable<Row> eventRows) {
ImmutableList.Builder<TraceEvent> eventsBuilder = ImmutableList.builder();
for (Row eventRow : eventRows) {
UUID eventId = eventRow.getUuid("event_id");
int sourcePort = 0;
if (eventRow.getColumnDefinitions().contains("source_port")) {
sourcePort = eventRow.getInt("source_port");
}
eventsBuilder.add(new DefaultTraceEvent(eventRow.getString("activity"), eventId == null ? -1 : eventId.timestamp(), new InetSocketAddress(eventRow.getInetAddress("source"), sourcePort), eventRow.getInt("source_elapsed"), eventRow.getString("thread")));
}
Instant startedAt = sessionRow.getInstant("started_at");
int coordinatorPort = 0;
if (sessionRow.getColumnDefinitions().contains("coordinator_port")) {
coordinatorPort = sessionRow.getInt("coordinator_port");
}
return new DefaultQueryTrace(tracingId, sessionRow.getString("request"), sessionRow.getInt("duration"), new InetSocketAddress(sessionRow.getInetAddress("coordinator"), coordinatorPort), sessionRow.getMap("parameters", String.class, String.class), startedAt == null ? -1 : startedAt.toEpochMilli(), eventsBuilder.build());
}
use of com.datastax.oss.driver.api.core.cql.TraceEvent in project java-driver by datastax.
the class QueryTraceFetcherTest method should_retry_when_session_row_is_incomplete.
@Test
public void should_retry_when_session_row_is_incomplete() {
// Given
CompletionStage<AsyncResultSet> sessionRow1 = incompleteSessionRow();
CompletionStage<AsyncResultSet> sessionRow2 = completeSessionRow();
CompletionStage<AsyncResultSet> eventRows = singlePageEventRows();
when(session.executeAsync(any(SimpleStatement.class))).thenAnswer(invocation -> sessionRow1).thenAnswer(invocation -> sessionRow2).thenAnswer(invocation -> eventRows);
// When
QueryTraceFetcher fetcher = new QueryTraceFetcher(TRACING_ID, session, context, config);
CompletionStage<QueryTrace> traceFuture = fetcher.fetch();
// Then
verify(session, times(3)).executeAsync(statementCaptor.capture());
List<SimpleStatement> statements = statementCaptor.getAllValues();
assertSessionQuery(statements.get(0));
assertSessionQuery(statements.get(1));
assertEventsQuery(statements.get(2));
verifyNoMoreInteractions(session);
assertThatStage(traceFuture).isSuccess(trace -> {
assertThat(trace.getTracingId()).isEqualTo(TRACING_ID);
assertThat(trace.getRequestType()).isEqualTo("mock request");
assertThat(trace.getDurationMicros()).isEqualTo(42);
assertThat(trace.getCoordinatorAddress().getAddress()).isEqualTo(address);
assertThat(trace.getCoordinatorAddress().getPort()).isEqualTo(PORT);
assertThat(trace.getParameters()).hasSize(2).containsEntry("key1", "value1").containsEntry("key2", "value2");
assertThat(trace.getStartedAt()).isEqualTo(0);
List<TraceEvent> events = trace.getEvents();
assertThat(events).hasSize(3);
for (int i = 0; i < events.size(); i++) {
TraceEvent event = events.get(i);
assertThat(event.getActivity()).isEqualTo("mock activity " + i);
assertThat(event.getTimestamp()).isEqualTo(i);
assertThat(event.getSourceAddress()).isNotNull();
assertThat(event.getSourceAddress().getAddress()).isEqualTo(address);
assertThat(event.getSourceAddress().getPort()).isEqualTo(PORT);
assertThat(event.getSourceElapsedMicros()).isEqualTo(i);
assertThat(event.getThreadName()).isEqualTo("mock thread " + i);
}
});
}
use of com.datastax.oss.driver.api.core.cql.TraceEvent in project java-driver by datastax.
the class QueryTraceFetcherTest method should_succeed_when_both_queries_succeed_immediately.
@Test
public void should_succeed_when_both_queries_succeed_immediately() {
// Given
CompletionStage<AsyncResultSet> sessionRow = completeSessionRow();
CompletionStage<AsyncResultSet> eventRows = singlePageEventRows();
when(session.executeAsync(any(SimpleStatement.class))).thenAnswer(invocation -> sessionRow).thenAnswer(invocation -> eventRows);
// When
QueryTraceFetcher fetcher = new QueryTraceFetcher(TRACING_ID, session, context, config);
CompletionStage<QueryTrace> traceFuture = fetcher.fetch();
// Then
verify(session, times(2)).executeAsync(statementCaptor.capture());
List<SimpleStatement> statements = statementCaptor.getAllValues();
assertSessionQuery(statements.get(0));
SimpleStatement statement = statements.get(1);
assertEventsQuery(statement);
verifyNoMoreInteractions(session);
assertThatStage(traceFuture).isSuccess(trace -> {
assertThat(trace.getTracingId()).isEqualTo(TRACING_ID);
assertThat(trace.getRequestType()).isEqualTo("mock request");
assertThat(trace.getDurationMicros()).isEqualTo(42);
assertThat(trace.getCoordinatorAddress().getAddress()).isEqualTo(address);
assertThat(trace.getCoordinatorAddress().getPort()).isEqualTo(PORT);
assertThat(trace.getParameters()).hasSize(2).containsEntry("key1", "value1").containsEntry("key2", "value2");
assertThat(trace.getStartedAt()).isEqualTo(0);
List<TraceEvent> events = trace.getEvents();
assertThat(events).hasSize(3);
for (int i = 0; i < events.size(); i++) {
TraceEvent event = events.get(i);
assertThat(event.getActivity()).isEqualTo("mock activity " + i);
assertThat(event.getTimestamp()).isEqualTo(i);
assertThat(event.getSourceAddress()).isNotNull();
assertThat(event.getSourceAddress().getAddress()).isEqualTo(address);
assertThat(event.getSourceAddress().getPort()).isEqualTo(PORT);
assertThat(event.getSourceElapsedMicros()).isEqualTo(i);
assertThat(event.getThreadName()).isEqualTo("mock thread " + i);
}
});
}
Aggregations