Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tests
  • Loading branch information
carterkozak committed Jul 13, 2020
commit 608b3e1efd340a5fa7c462bd61c83022bbd3f5af
25 changes: 25 additions & 0 deletions tracing/src/test/java/com/palantir/tracing/TracerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,31 @@ public void testDetachedTraceAppliedToThreadState() {
assertThat(Tracer.hasTraceId()).isFalse();
try (CloseableSpan ignored = detached.childSpan(operation2)) {
assertThat(Tracer.hasTraceId()).isTrue();
assertThat(Tracer.maybeGetTraceMetadata().get().getRequestId()).isEmpty();
}
verify(observer1).consume(spanCaptor.capture());
assertThat(spanCaptor.getValue().getOperation()).isEqualTo(operation2);
assertThat(Tracer.hasTraceId()).isFalse();
} finally {
detached.complete();
}
assertThat(Tracer.hasTraceId()).isFalse();
Tracer.unsubscribe("1");
}

@Test
public void testDetachedTraceAppliedToThreadState_requestId() {
assertThat(Tracer.hasTraceId()).isFalse();
Tracer.subscribe("1", observer1);
String operation1 = "operation";
String operation2 = "attached";
DetachedSpan detached = DetachedSpan.start(operation1, SpanType.SERVER_INCOMING);
String requestId = InternalTracers.getRequestId(detached).get();
try {
assertThat(Tracer.hasTraceId()).isFalse();
try (CloseableSpan ignored = detached.childSpan(operation2)) {
assertThat(Tracer.hasTraceId()).isTrue();
assertThat(Tracer.maybeGetTraceMetadata().get().getRequestId()).hasValue(requestId);
}
verify(observer1).consume(spanCaptor.capture());
assertThat(spanCaptor.getValue().getOperation()).isEqualTo(operation2);
Expand Down
31 changes: 31 additions & 0 deletions tracing/src/test/java/com/palantir/tracing/TracersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import com.palantir.logsafe.exceptions.SafeRuntimeException;
import com.palantir.tracing.api.OpenSpan;
import com.palantir.tracing.api.Span;
import com.palantir.tracing.api.SpanType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -114,6 +116,24 @@ public void testWrapExecutorService_withSpan() throws Exception {
Tracer.fastCompleteSpan();
}

@Test
public void testWrapExecutorService_withRequestId() throws Exception {
ExecutorService wrappedService = Tracers.wrap("operation", Executors.newSingleThreadExecutor());

// Non-empty trace
Tracer.initTraceWithSpan(Observability.UNDECIDED, "traceId", "root", SpanType.SERVER_INCOMING);
String requestId = Tracer.maybeGetTraceMetadata()
.flatMap(TraceMetadata::getRequestId)
.get();
wrappedService
.submit(traceExpectingCallableWithSingleSpan("operation", Optional.of(requestId)))
.get();
wrappedService
.submit(traceExpectingRunnableWithSingleSpan("operation", Optional.of(requestId)))
.get();
Tracer.fastCompleteSpan();
}

@Test
public void testWrapScheduledExecutorService() {
withExecutor(() -> Tracers.wrap(Executors.newSingleThreadScheduledExecutor()), wrappedService -> {
Expand Down Expand Up @@ -913,6 +933,11 @@ private static Runnable newTraceExpectingRunnable(String expectedOperation) {
}

private static Callable<Void> traceExpectingCallableWithSingleSpan(String operation) {
return traceExpectingCallableWithSingleSpan(operation, Optional.empty());
}

private static Callable<Void> traceExpectingCallableWithSingleSpan(
String operation, Optional<String> expectedRequestId) {
final String outsideTraceId = Tracer.getTraceId();

return () -> {
Expand All @@ -924,11 +949,16 @@ private static Callable<Void> traceExpectingCallableWithSingleSpan(String operat
assertThat(traceId).isEqualTo(outsideTraceId);
assertThat(span.getOperation()).isEqualTo(operation);
assertThat(MDC.get(Tracers.TRACE_ID_KEY)).isEqualTo(outsideTraceId);
assertThat(MDC.get(Tracers.REQUEST_ID_KEY)).isEqualTo(expectedRequestId.orElse(null));
return null;
};
}

private static Runnable traceExpectingRunnableWithSingleSpan(String operation) {
return traceExpectingRunnableWithSingleSpan(operation, Optional.empty());
}

private static Runnable traceExpectingRunnableWithSingleSpan(String operation, Optional<String> expectedRequestId) {
final String outsideTraceId = Tracer.getTraceId();

return () -> {
Expand All @@ -940,6 +970,7 @@ private static Runnable traceExpectingRunnableWithSingleSpan(String operation) {
assertThat(traceId).isEqualTo(outsideTraceId);
assertThat(span.getOperation()).isEqualTo(operation);
assertThat(MDC.get(Tracers.TRACE_ID_KEY)).isEqualTo(outsideTraceId);
assertThat(MDC.get(Tracers.REQUEST_ID_KEY)).isEqualTo(expectedRequestId.orElse(null));
};
}

Expand Down