Skip to content
This repository was archived by the owner on Jul 1, 2022. It is now read-only.

Commit 594b23c

Browse files
avimasavim
andauthored
Document MDC log correlation support (#724)
* add documentation Signed-off-by: avim <[email protected]> * PR feedback Co-authored-by: avim <[email protected]>
1 parent 936b39b commit 594b23c

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

jaeger-core/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,62 @@ Tracer tracer = configuration
140140
.withMetricsFactory(metricsReporter)
141141
.build();
142142
```
143+
### Log Correlation
144+
145+
The Jaeger Java Client supports log correlation by updating the logging context (MDC) with
146+
three variables: `traceId`, `spanId`, `sampled`.
147+
148+
To accomplish that Jaegar Tracer should be created with the following two steps:
149+
150+
1. Create the MDCScopeManager using either default MDC field names:
151+
152+
```java
153+
MDCScopeManager scopeManager = new MDCScopeManager.Builder().build()
154+
```
155+
Or by providing optional custom names:
156+
157+
```java
158+
MDCScopeManager scopeManager = new MDCScopeManager
159+
.Builder()
160+
.withMDCTraceIdKey("CustomTraceId")
161+
.withMDCSampledKey("customSampled")
162+
.withMDCSpanIdKey("customSpanId")
163+
.build();
164+
```
165+
2. Create the Jaegar Tracer by supplying the MDCScopeManager created step 1:
166+
```java
167+
JaegerTracer.Builder("serviceName").withScopeManager(scopeManager).build();
168+
```
169+
In order to have the trace info in the logs, a logging system offers MDC functionality such as log4j
170+
needs to be configured with an appender containing a proper PatternLayout.
171+
172+
For example the following code:
173+
```java
174+
Span parentSpan = tracer.buildSpan("log example").start();
175+
try (Scope scope = tracer.activateSpan(parentSpan)) {
176+
LOGGER.debug("Debug with trace context!!!");
177+
}
178+
}
179+
```
180+
With this log4j2.xml configuration:
181+
```xml
182+
<?xml version="1.0" encoding="UTF-8"?>
183+
<Configuration status="INFO">
184+
<Appenders>
185+
<Console name="console" target="SYSTEM_OUT">
186+
<PatternLayout
187+
pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg% traceId=%X{traceId} spanId=%X{spanId} sampled=%X{sampled}%n" />
188+
</Console>
189+
</Appenders>
190+
<Loggers>
191+
<Root level="debug" additivity="false">
192+
<AppenderRef ref="console" />
193+
</Root>
194+
</Loggers>
195+
</Configuration>
196+
```
197+
Prints:
198+
[DEBUG] 2020-06-28 22:25:07.152 [main] LogExample - Debug with trace context!!!% traceId=729b37ccf9c1549d spanId=729b37ccf9c1549d sampled=false
143199

144200
## Development
145201

0 commit comments

Comments
 (0)