Adding parent id to MDC
without this change we couldn't reference parent id in the logs
with this change we add the parent id to MDC context
fixes #480
This commit is contained in:
@@ -51,6 +51,7 @@ public class Slf4jSpanLogger implements SpanLogger {
|
||||
log("Starting span: {}", span);
|
||||
if (parent != null) {
|
||||
log("With parent: {}", parent);
|
||||
MDC.put(Span.PARENT_ID_NAME, Span.idToHex(parent.getSpanId()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,9 +60,16 @@ public class Slf4jSpanLogger implements SpanLogger {
|
||||
MDC.put(Span.SPAN_ID_NAME, Span.idToHex(span.getSpanId()));
|
||||
MDC.put(Span.TRACE_ID_NAME, span.traceIdString());
|
||||
MDC.put(Span.SPAN_EXPORT_NAME, String.valueOf(span.isExportable()));
|
||||
setParentIdIfPresent(span);
|
||||
log("Continued span: {}", span);
|
||||
}
|
||||
|
||||
private void setParentIdIfPresent(Span span) {
|
||||
if (!span.getParents().isEmpty()) {
|
||||
MDC.put(Span.PARENT_ID_NAME, Span.idToHex(span.getParents().get(0)));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logStoppedSpan(Span parent, Span span) {
|
||||
if (span != null) {
|
||||
@@ -71,11 +79,13 @@ public class Slf4jSpanLogger implements SpanLogger {
|
||||
log("With parent: {}", parent);
|
||||
MDC.put(Span.SPAN_ID_NAME, Span.idToHex(parent.getSpanId()));
|
||||
MDC.put(Span.SPAN_EXPORT_NAME, String.valueOf(parent.isExportable()));
|
||||
setParentIdIfPresent(parent);
|
||||
}
|
||||
else {
|
||||
MDC.remove(Span.SPAN_ID_NAME);
|
||||
MDC.remove(Span.SPAN_EXPORT_NAME);
|
||||
MDC.remove(Span.TRACE_ID_NAME);
|
||||
MDC.remove(Span.PARENT_ID_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ import static org.mockito.Mockito.times;
|
||||
public class Slf4JSpanLoggerTest {
|
||||
|
||||
Span spanWithNameToBeExcluded = Span.builder().name("Hystrix").build();
|
||||
Span spanWithNameNotToBeExcluded = Span.builder().name("Aspect").build();
|
||||
Span spanWithNameNotToBeExcluded = Span.builder().name("Aspect").parent(3L).build();
|
||||
String nameExcludingPattern = "^.*Hystrix.*$";
|
||||
Logger log = Mockito.mock(Logger.class);
|
||||
Slf4jSpanLogger slf4JSpanLogger = new Slf4jSpanLogger(this.nameExcludingPattern, this.log);
|
||||
@@ -152,4 +152,28 @@ public class Slf4JSpanLoggerTest {
|
||||
|
||||
then(this.log).should().trace(anyString(), anyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_set_mdc_entry_for_parent_when_starting() throws Exception {
|
||||
this.slf4JSpanLogger.logStartedSpan(this.spanWithNameNotToBeExcluded,
|
||||
this.spanWithNameNotToBeExcluded);
|
||||
|
||||
assertThat(MDC.get(Span.PARENT_ID_NAME)).isEqualTo(
|
||||
Span.idToHex(this.spanWithNameNotToBeExcluded.getSpanId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_set_mdc_entry_for_parent_when_continuing() throws Exception {
|
||||
this.slf4JSpanLogger.logContinuedSpan(this.spanWithNameNotToBeExcluded);
|
||||
|
||||
assertThat(MDC.get(Span.PARENT_ID_NAME)).isEqualTo(Span.idToHex(3L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_set_mdc_entry_for_parent_when_stopping() throws Exception {
|
||||
this.slf4JSpanLogger.logStoppedSpan(this.spanWithNameNotToBeExcluded,
|
||||
this.spanWithNameNotToBeExcluded);
|
||||
|
||||
assertThat(MDC.get(Span.PARENT_ID_NAME)).isEqualTo(Span.idToHex(3L));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user