Skip to content

Commit

Permalink
[improvement] Adds key traceSampled to MDC when set trace is observab…
Browse files Browse the repository at this point in the history
…le (#144)

## Before this PR
Log consumers were unable to tell which traces were sampled (observable: true) vs which traces were not sampled (observable: false).

## After this PR
An MDC key traceSampled will now appear with the value "1" when the set trace has been sampled (observable: true). If the set trace is not sampled (observable: false), the key will not appear in the MDC.

Note: this replaces #104
  • Loading branch information
raiju authored and bulldozer-bot[bot] committed Apr 17, 2019
1 parent ef1eb83 commit 45ca2aa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
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 @@ -338,8 +338,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");
} else {
// To ensure MDC state is cleared when trace is not observable
MDC.remove(Tracers.TRACE_SAMPLED_KEY);
}
}

private static Trace getOrCreateCurrentTrace() {
Expand All @@ -354,5 +365,6 @@ private static Trace getOrCreateCurrentTrace() {
private static void clearCurrentTrace() {
currentTrace.remove();
MDC.remove(Tracers.TRACE_ID_KEY);
MDC.remove(Tracers.TRACE_SAMPLED_KEY);
}
}
4 changes: 4 additions & 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,10 @@
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";
/** The key under which trace sampling state are inserted into SLF4J {@link org.slf4j.MDC MDCs}. If present, the
* field can take the values of "1" or "0", where "1" indicates the trace was sampled.
*/
public static final String TRACE_SAMPLED_KEY = "_sampled";
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

0 comments on commit 45ca2aa

Please sign in to comment.