How to get trace-id in spring-cloud-gateway using micrometer-brave
Asked Answered
C

4

5

I want to display traceId in logs for each request in Spring Cloud Gateway. However, traceId and spanId are just empty.

Log config is like below:

logging:
  pattern:
    level: "%5p [TRACE_ID: %X{traceId:-}] [SPAN_ID: %X{spanId:-}]"

Part of pom.xml:

  <dependency>
      <groupId>io.micrometer</groupId>
      <artifactId>micrometer-tracing-bridge-brave</artifactId>
  </dependency>
  
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
      <version>3.0.1</version>
  </dependency>
Canzona answered 24/1, 2023 at 13:0 Comment(2)
you might put the following setting in your yml file: spring: sleuth: reactor: instrumentation-type: decorate_queuesSprout
But I use micrometer instead of sleuthCanzona
N
9

With Spring Boot 3.x and Micrometer 1.10+ you can try the following dependencies:

(Part of pom.xml)

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-core</artifactId>
        <version>3.5.3</version>
    </dependency>
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-tracing-bridge-brave</artifactId>
    </dependency>
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-observation</artifactId>
    </dependency>
    <dependency>
        <groupId>io.micrometer</groupId>
        <version>1.0.2</version>
        <artifactId>micrometer-tracing</artifactId>
    </dependency>
    <dependency>
        <groupId>io.micrometer</groupId>
        <version>1.0.2</version>
        <artifactId>context-propagation</artifactId>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.micrometer</groupId>
            <version>1.10.4</version>
            <artifactId>micrometer-bom</artifactId>
            <scope>import</scope>
        <type>pom</type>
    </dependency>
    </dependencies>
</dependencyManagement>

And in the main class of the application make the following call:

import reactor.core.publisher.Hooks;

public static void main(String[] args) {
    Hooks.enableAutomaticContextPropagation();
    // ...
}

Spring Boot should soon integrate those versions of dependencies and make the call for you, but for now these manual steps should get you the logs with tracing info.

In reactor-core 3.5.3 we added automatic propagation of ThreadLocal values registered with context-propagation (optional) library which Micrometer also uses. For those interested in the details, check the PR introducing this feature. This is a general enhancement from what 3.5.0 brought, where the ThreadLocals were populated in special operators -> handle and tap (and its overloads).

Nummulite answered 22/2, 2023 at 9:16 Comment(0)
T
3

As an alternative to adding Hooks.enableAutomaticContextPropagation() in the application main function, spring boot created property config which you can specify in application.properties.

spring.reactor.context-propagation with the values AUTO and LIMITED. It defaults to LIMITED.

for example (incase you need trace id and span ids):

spring.reactor.context-propagation: AUTO

Follow the entire PR history: https://github.com/spring-projects/spring-boot/issues/34201

Hope it will help to someone.

Tortoiseshell answered 16/1, 2024 at 20:58 Comment(0)
C
2

simply explain

build.gradle

implementation 'io.micrometer:micrometer-tracing-bridge-brave'

and As explained in the answer above

import reactor.core.publisher.Hooks;

public static void main(String[] args) {
    Hooks.enableAutomaticContextPropagation();
    // ...
}

If logback is set up well, you can check the log by doing two things.

Chondrule answered 19/4, 2023 at 8:43 Comment(0)
M
0

@Dariusz Jędrzejczyk's is great. There are some dependencies that you may omit as it's already covered with other dependencies. If you're using FeignClient and want to see these traces in Zipkin UI, here are the needed dependencies.

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-openfeign-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-tracing-bridge-brave</artifactId>
    </dependency>
    <dependency>
        <groupId>io.zipkin.reporter2</groupId>
        <artifactId>zipkin-reporter-brave</artifactId>
    </dependency>
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-micrometer</artifactId>
    </dependency>
</dependencies>
Malissamalissia answered 24/4, 2023 at 12:45 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.