log4j 无法通过MDC获取opentelemetry的trace_id,即使MDC正在通过logging.pattern.level工作

sf6xfgos  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(218)

我已经在应用程序中集成了OpenTelemarket,遵循此https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/logger-mdc-instrumentation.md之后,我能够通过日志记录将trace_id和其他信息注入日志。pattern.level = trace_id= % mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %5p
但是当我试图通过MDC.get(“trace_id”)在代码中获取这些trace_id时,我得到了null。

这是我的配置。

plugins {
        id 'org.springframework.boot' version '2.7.0'
        id 'io.spring.dependency-management' version '1.0.11.RELEASE'
        id 'java'
        id 'jacoco'
        id "org.sonarqube" version "3.4.0.2513"
        id 'de.undercouch.download' version '4.1.1'
    }
    
    
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '17'
    
    configurations {
        compileOnly {
        extendsFrom annotationProcessor
        }
    }
    
    
    
    
    ext {
        fasterxmlJackson = '2.9.7';
    }
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-actuator'
        implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
        implementation 'org.springframework.boot:spring-boot-starter-jdbc'
        implementation 'org.springframework.boot:spring-boot-starter-security'
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation('org.springframework.boot:spring-boot-starter-data-rest')
        compileOnly 'org.projectlombok:lombok'
        implementation 'org.postgresql:postgresql:42.4.0'
        implementation 'com.vladmihalcea:hibernate-types-55:2.16.2'
        annotationProcessor 'org.projectlombok:lombok'
        implementation('com.querydsl:querydsl-jpa:4.1.3')
        implementation('com.amazonaws:aws-java-sdk-s3:1.11.704')
        implementation('com.mashape.unirest:unirest-java:1.4.9')
        implementation('org.springframework.boot:spring-boot-starter-cache:2.7.1')
        implementation('org.springframework.boot:spring-boot-starter-data-redis:2.7.1')
    
        implementation("com.fasterxml.jackson.datatype:jackson-datatype-json-org:${fasterxmlJackson}",
                "com.fasterxml.jackson.datatype:jackson-datatype-joda:${fasterxmlJackson}",
                "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${fasterxmlJackson}")
        implementation('org.apache.commons:commons-lang3:3.4')
        implementation('org.apache.commons:commons-collections4:4.3')
        implementation 'org.springdoc:springdoc-openapi-ui:1.6.14'
        implementation 'org.flywaydb:flyway-core'
        implementation(platform("io.opentelemetry:opentelemetry-bom:1.18.0"))
        implementation("io.opentelemetry:opentelemetry-api")
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
        testImplementation 'org.mockito:mockito-inline:4.6.1'
        testImplementation 'org.mockito:mockito-junit-jupiter:4.6.1'
    
    }
    configurations {
        compile.all*.exclude group: "org.apache.logging.log4j", module: "log4j-slf4j-impl"
        compile.all*.exclude group: "com.vaadin.external.google", module: "android-json"
        compile.all*.exclude group: "commons-logging", module: "commons-logging"
        all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
       }
    }
    
    tasks.named('test') {
        useJUnitPlatform()
    }
    
    jacocoTestReport {
        reports {
        xml.enabled true
        html.enabled true
        }
    }
    bootJar {
        dependsOn("downloadAgent")
    }
    task downloadAgent(type: Download) {
        src "https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v1.17.0/opentelemetry-javaagent.jar"
        dest project.buildDir.toString() + "/otel/opentelemetry-javaagent.jar"
        overwrite true
    }

字符串
我需要在代码中获取trace_id,以便我可以在非日志用例中使用。

5gfr0r5j

5gfr0r5j1#

trace_id在logger中可用而在MDC中不可用的原因是,MDC Logger auto-instrumentation不会将值添加到MDC中,因为在这种情况下只检测日志记录类(如果您想调试它,请在ch.qos.logback.classic.pattern.MDCConverter#convert中设置断点)。
为了在MDC中获取trace_id,可以添加io.micrometer:micrometer-tracing-bridge-otel依赖项,就像在https://spring.io/blog/2022/10/12/observability-with-spring-boot-3https://micrometer.io/docs/tracing中描述的那样。
但是要小心!虽然跟踪id在日志检测中的键是trace_id,但在MDC中的键是traceId

相关问题