如何使用Spring Boot的应用程序日志(由slf4j生产)丰富Jaeger opentracing数据? [英] How to enrich Jaeger opentracing data with the application logs (produced by slf4j) for Spring Boot?

查看:289
本文介绍了如何使用Spring Boot的应用程序日志(由slf4j生产)丰富Jaeger opentracing数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个现有的Spring Boot应用程序正在使用SLF4J记录器.我决定通过使用Jaeger作为跟踪器的标准opentracing API添加对分布式跟踪的支持.初始设置如此简单真是令人惊讶-只需向pom.xml添加两个依赖项即可:

There is an existing Spring Boot app which is using SLF4J logger. I decided to add the support of distributed tracing via standard opentracing 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 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>

并为Tracer bean提供配置:

and providing the Tracer bean with the configuration:

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

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

All works like a charm - app requests are processed by Jaeger and spans are created:

但是,在范围Logs中,只有preHandle&在请求执行期间被调用的afterCompletion事件,其中包含有关类/方法的信息(不收集slf4j记录器生成的日志):

However, in the span Logs there are only preHandle & afterCompletion events with info about the class / method that were called during request execution (no logs produced by slf4j logger are collected) :

问题是,是否可以将Tracer配置为提取应用程序记录器生成的日志(在我的情况下为slf4j),以便通过以下方式完成所有应用程序日志:LOG.info/LOG.warn/等也将反映在Jaeger

The question is if it is possible to configure the Tracer to pickup the logs produced by the app logger (slf4j in my case) so that all the application logs done via: LOG.info / LOG.warn / LOG.error etc. would be also reflected in Jaeger

注意:我已经弄清楚了如何通过opentracing API例如手动登录

NOTE: I have figured out how to log to span manually via opentracing API e.g.:

Scope scope = tracer.scopeManager().active();
if (scope != null) {
    scope.span().log("...");
}

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

And do some manual manipulations with the ERROR tag for exception processing in filters e.g.

} 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
}

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

But, I'm still wondering if it is possible to configure the tracer to pickup the application logs automatically:

  • LOG.info->跟踪器将新日志添加到活动范围
  • LOG.error->跟踪器将新日志添加到活动范围,并添加ERROR标记
  • LOG.info -> tracer adds new log to the active span
  • LOG.error -> tracer adds new log to the active span plus adds ERROR tag

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

UPDATE: I was able to add the application logs to the tracer by adding wrapper for the logger e.g.

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);
}

但是,到目前为止,我还无法找到opentracing配置选项,该选项允许默认情况下自动将应用程序日志添加到跟踪器中.基本上,似乎期望开发人员在需要时以编程方式向跟踪器添加额外的日志.另外,在调查了更多跟踪之后,似乎通常将loggingtracing分开处理,并且将所有应用程序日志添加到跟踪器中并不是一个好主意(跟踪器应主要包括样本数据和用于标识请求的标签)

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 logging and tracing 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)

  • https://github.com/openzipkin/zipkin/issues/1453
  • https://peter.bourgon.org/blog/2016/02/07/logging-v-instrumentation.html

推荐答案

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

https://github.com/opentracing-contrib/java-spring-cloud project automatically sends standard logging to the active span. Just add the following dependency to your pom.xml

<dependency>
   <groupId>io.opentracing.contrib</groupId>
   <artifactId>opentracing-spring-cloud-starter</artifactId>
</dependency>

或使用此 https://github.com/opentracing-contrib/java-spring-cloud/tree/master/instrument-starters/opentracing-spring-cloud-core 入门.

这篇关于如何使用Spring Boot的应用程序日志(由slf4j生产)丰富Jaeger opentracing数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆