Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds key traceSampled to MDC when set trace is observable #104

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 13 additions & 1 deletion tracing/src/main/java/com/palantir/tracing/Tracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,19 @@ static Optional<Trace> copyTrace() {
static void setTrace(Trace trace) {
currentTrace.set(trace);

// Give SLF4J appenders access to the trace id
// Give log appenders access to the trace id and whether the trace is being sampled
MDC.put(Tracers.TRACE_ID_KEY, trace.getTraceId());
setTraceSampledMdcIfObservable(trace.isObservable());
}

private static void setTraceSampledMdcIfObservable(boolean observable) {
if (observable) {
// Set to 1 to be consistent with values associated with http header key TraceHttpHeaders.IS_SAMPLED
MDC.put(Tracers.TRACE_SAMPLED_KEY, "1");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might add a comment describing why we're using 1 instead of true.

} else {
// To ensure MDC state is cleared when trace is not observable
MDC.remove(Tracers.TRACE_SAMPLED_KEY);
}
}

private static Trace getOrCreateCurrentTrace() {
Expand All @@ -347,5 +358,6 @@ private static Trace getOrCreateCurrentTrace() {
private static void clearCurrentTrace() {
currentTrace.remove();
MDC.remove(Tracers.TRACE_ID_KEY);
MDC.remove(Tracers.TRACE_SAMPLED_KEY);
}
}
1 change: 1 addition & 0 deletions tracing/src/main/java/com/palantir/tracing/Tracers.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
public final class Tracers {
/** The key under which trace ids are inserted into SLF4J {@link org.slf4j.MDC MDCs}. */
public static final String TRACE_ID_KEY = "traceId";
public static final String TRACE_SAMPLED_KEY = "traceSampled";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should document this key similarly to TRACE_ID_KEY and describe possible values.

private static final String DEFAULT_ROOT_SPAN_OPERATION = "root";
private static final char[] HEX_DIGITS =
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
Expand Down
16 changes: 16 additions & 0 deletions tracing/src/test/java/com/palantir/tracing/TracerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@ public void testSetTraceSetsCurrentTraceAndMdcTraceIdKey() throws Exception {
assertThat(MDC.get(Tracers.TRACE_ID_KEY)).isNull();
}

@Test
public void testSetTraceSetsMdcTraceSampledKeyWhenObserved() {
Tracer.setTrace(new Trace(true, "observedTraceId"));
assertThat(MDC.get(Tracers.TRACE_SAMPLED_KEY)).isEqualTo("1");
assertThat(Tracer.completeSpan()).isEmpty();
assertThat(MDC.get(Tracers.TRACE_SAMPLED_KEY)).isNull();
}

@Test
public void testSetTraceMissingMdcTraceSampledKeyWhenNotObserved() {
Tracer.setTrace(new Trace(false, "notObservedTraceId"));
assertThat(MDC.get(Tracers.TRACE_SAMPLED_KEY)).isNull();
assertThat(Tracer.completeSpan()).isEmpty();
assertThat(MDC.get(Tracers.TRACE_SAMPLED_KEY)).isNull();
}

@Test
public void testCompletedSpanHasCorrectSpanType() throws Exception {
for (SpanType type : SpanType.values()) {
Expand Down