如何使用Spring Boot的应用程序日志(由slf4j生产)丰富Jaeger opentracing数据?

Ily*_*iuk 5 logging slf4j spring-boot opentracing jaeger

现有的Spring Boot应用程序正在使用SLF4J记录器。我决定通过opentracing使用Jaeger作为跟踪器的标准API 添加对分布式跟踪的支持。初始设置多么容易,真是太神奇了-所需要的只是在上添加两个依赖项pom.xml

    <dependency>
        <groupId>io.opentracing.contrib</groupId>
        <artifactId>opentracing-spring-web-autoconfigure</artifactId>
        <version>${io.opentracing.version}</version>
    </dependency>

    <dependency>
        <groupId>io.jaegertracing</groupId>
        <artifactId>jaeger-core</artifactId>
        <version>${jaegerVersion}</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

并为TracerBean提供配置:

@Bean
public io.opentracing.Tracer getTracer() throws ConfigurationException {
    return new new io.jaegertracing.Tracer.Builder("my-spring-boot-app").build();
}
Run Code Online (Sandbox Code Playgroud)

所有工作都像一个咒语-Jaeger处理应用程序请求并创建跨度:

在此处输入图片说明

但是,在跨度Logs中,只有preHandleafterCompletion事件包含在请求执行期间被调用的有关类/方法的信息(不slf4j收集由logger 生成的日志):

在此处输入图片说明

现在的问题是,如果它是可以配置示踪至拾取由应用记录仪(产生的日志slf4j在我的情况),使所有的应用程序日志进行经由:LOG.info/ LOG.warn/ LOG.error等将也反映在积

注意:我想出了如何通过API 手动记录跨度,opentracing例如:

Scope scope = tracer.scopeManager().active();
if (scope != null) {
    scope.span().log("...");
}
Run Code Online (Sandbox Code Playgroud)

并对标签进行一些手动操作,ERROR以便在过滤器中进行异常处理,例如

} 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
}
Run Code Online (Sandbox Code Playgroud)

但是,我仍然想知道是否可以配置跟踪器以提取应用程序日志automatically

  • LOG.info ->跟踪器将新日志添加到活动范围
  • LOG.error->跟踪器将新日志添加到活动范围,并添加ERROR标签

更新:通过添加记录器的包装器,我能够将应用程序日志添加到跟踪器,例如

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);
}
Run Code Online (Sandbox Code Playgroud)

但是,到目前为止,我无法找到默认情况下允许自动将应用程序日志添加到跟踪器的opentracing配置选项。基本上,似乎期望开发人员在需要时以编程方式向跟踪器添加额外的日志。此外,调查跟踪的详细经过它似乎是正常loggingtracing被单独处理,再添加全部的应用程序日志,以示踪剂是不是一个好主意(示踪剂应主要包括请求识别的样本数据和标签。)

小智 5

https://github.com/opentracing-contrib/java-spring-cloud项目会自动将标准日志记录发送到活动范围。只需将以下依赖项添加到您的pom.xml中

<dependency>
   <groupId>io.opentracing.contrib</groupId>
   <artifactId>opentracing-spring-cloud-starter</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

或者,如果只想记录集成,使用此https://github.com/opentracing-contrib/java-spring-cloud/tree/master/instrument-starters/opentracing-spring-cloud-core入门工具。