Java应用日志如何与Jaeger的trace关联

欢迎访问我的GitHub

本篇概览

  1. 一次web请求可能有多条业务日志(log4j或者logback配置的那种),这和您写代码执行log.info的次数有关,假设有10条,那么十次请求就有一百条业务日志
  2. 通过jaeger发现这十次请求中有一次耗时特别长,想定位一下具体原因,现在问题来了:一共有100条业务日志,到底哪些是和Jaeger中耗时长的哪一次请求有关?

关于MDC


        
            INFO
        
        
            
            %d{HH:mm:ss} [%thread] %-5level %logger{10} [user-id=%X{user-id}] %msg%n
            utf-8
        
    
@GetMapping("/test")
    public void test() {
        MDC.put("user-id", "user-" + System.currentTimeMillis());
        log.info("this is test request");
    }
15:17:47 [http-nio-18081-exec-6] INFO  c.b.j.c.c.HelloConsumerController [user-id=user-1632122267618] this is test request

关于Jaeger的官方方案

编码实战

<?xml version="1.0" encoding="UTF-8"?>



    logback

    
    
        
            INFO
        
        
            
            %d{HH:mm:ss} [%thread] %-5level %logger{10} [traceId=%X{traceId} spanId=%X{spanId} sampled=%X{sampled}] %msg%n
            utf-8
        
    

    
        
    
package com.bolingcavalry.jaeger.provider.config;

import io.jaegertracing.internal.MDCScopeManager;
import io.opentracing.contrib.java.spring.jaeger.starter.TracerBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JaegerConfig {
    @Bean
    public TracerBuilderCustomizer mdcBuilderCustomizer() {
        // 1.8新特性,函数式接口
        return builder -> builder.withScopeManager(new MDCScopeManager.Builder().build());
    }
}

验证

欢迎关注头条号:程序员欣宸

展开阅读全文

页面更新:2024-03-12

标签:上下文   日志   变量   实战   模板   代码   页面   业务   方案   官方

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top