title: How to enrich Jaeger opentracing data with the application logs (produced by slf4j) for Spring Boot?
body: <p>There is an existing Spring Boot app which is using SLF4J logger. I decided to add the support of distributed tracing via standard <code>opentracing</code> API with Jaeger as the tracer. It is really amazing how easy the initial setup is - all that is required is just adding two dependencies to the <code>pom.xml</code>:</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;io.opentracing.contrib&lt;/groupId&gt;
          &lt;artifactId&gt;opentracing-spring-web-autoconfigure&lt;/artifactId&gt;
          &lt;version&gt;${io.opentracing.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;io.jaegertracing&lt;/groupId&gt;
          &lt;artifactId&gt;jaeger-core&lt;/artifactId&gt;
          &lt;version&gt;${jaegerVersion}&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>and providing the <code>Tracer</code> bean with the configuration:</p>
  
  <pre><code>@Bean
  public io.opentracing.Tracer getTracer() throws ConfigurationException {
      return new new io.jaegertracing.Tracer.Builder("my-spring-boot-app").build();
  }
  </code></pre>
  
  <p>All works like a charm - app requests are processed by Jaeger and spans are created:</p>
  
  <p><a href="https://i.stack.imgur.com/3EMFY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/3EMFY.png" alt="enter image description here"></a></p>
  
  <p>However, in the span <code>Logs</code> there are only <code>preHandle</code> &amp; <code>afterCompletion</code> events with info about the class / method that were called during request execution (no logs produced by <code>slf4j</code> logger are collected) :</p>
  
  <p><a href="https://i.stack.imgur.com/dGG4X.png" rel="noreferrer"><img src="https://i.stack.imgur.com/dGG4X.png" alt="enter image description here"></a></p>
  
  <p>The question is if it is possible to configure the Tracer to pickup the logs produced by the app logger (<code>slf4j</code> in my case) so that all the application logs done via: <code>LOG.info</code> / <code>LOG.warn</code> / <code>LOG.error</code> etc. would be also reflected in Jaeger </p>
  
  <p><strong>NOTE</strong>: I have figured out how to log to span <strong>manually</strong> via <code>opentracing</code> API e.g.:</p>
  
  <pre><code>Scope scope = tracer.scopeManager().active();
  if (scope != null) {
      scope.span().log("...");
  }
  </code></pre>
  
  <p>And do some <strong>manual</strong> manipulations with the <code>ERROR</code> tag for exception processing in filters e.g. </p>
  
  <pre><code>} catch(Exception ex) {
      Tags.ERROR.set(span, true);
      span.log(Map.of(Fields.EVENT, "error", Fields.ERROR_OBJECT, ex, Fields.MESSAGE, ex.getMessage()));
      throw ex
  }
  </code></pre>
  
  <p>But, I'm still wondering if it is possible to configure the tracer to pickup the application logs <code>automatically</code>:</p>
  
  <ul>
  <li><code>LOG.info</code> -> tracer adds new log to the active span</li>
  <li><code>LOG.error</code> -> tracer adds new log to the active span plus adds <code>ERROR</code> tag </li>
  </ul>
  
  <p><strong>UPDATE</strong>: I was able to add the application logs to the tracer by adding wrapper for the logger e.g.</p>
  
  <pre><code>public void error(String message, Exception e) {
      Scope scope = tracer.scopeManager().active();
      if (scope != null) {
          Span span = scope.span();
          Tags.ERROR.set(span, true);
          span.log(Map.of(Fields.EVENT, "error", Fields.ERROR_OBJECT, e, Fields.MESSAGE, e.getMessage()));
  
      }
      LOG.error(message, e);
  }
  </code></pre>
  
  <p>However, so far I was not able to find opentracing configuration options that would allow to add the application logs to the tracer automatically by default. Basically, it seems that it is expected that dev would add extra logs to tracer programmatically if needed. Also, after investigating tracing more it appeared to be that normally <code>logging</code> and <code>tracing</code> are handled separately and adding all the application logs to the tracer is not a good idea (tracer should mainly include sample data and tags for request identification) </p>
  
  <ul>
  <li><a href="https://github.com/openzipkin/zipkin/issues/1453" rel="noreferrer">https://github.com/openzipkin/zipkin/issues/1453</a></li>
  <li><a href="https://peter.bourgon.org/blog/2016/02/07/logging-v-instrumentation.html" rel="noreferrer">https://peter.bourgon.org/blog/2016/02/07/logging-v-instrumentation.html</a></li>
  </ul>