Using Feign builder requests doesn't send trace-id, span-id to child clients but using rest template is showing all headers on child clients
Asked Answered
C

1

6

I'm making a sequential request using Feign Builder. There are no x-b3-traceid,x-b3-spanid .. in the title of the request. That's why the log my last client appears on the zipkin.

I use spring boot 2.4.2 , spring cloud 2020.0.0 , feign-core 10.10.1 , feign-okhttp 10.10.1. I have tried spring-cloud-openfeign and i achieved wanted result. But i don't want to use this lib. There are requests when using Feign Builder and Rest Template in here. I dont' see same log at zipkin.

My Client1 App. I am sending request http://localhost:8082/

import feign.Client;
import feign.Feign;
import feign.RequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import sample.feign2.Client2Feign;

import javax.servlet.http.HttpServletRequest;
@RestController
public class SampleController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private Client client;

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    @Bean
    public OkHttpClient okHttpClient(){
        return new OkHttpClient();
    }

    @GetMapping("/hello/feignBuilder")
    public String sayHelloFeignBuilder(HttpServletRequest httpServletRequest){
        logger.info("Send to request client2");
        Client2Feign client2Feign = Feign.builder()
                .client(client)
                .decoder(new Decoder.Default())
                .encoder(new Encoder.Default())
                .target(Client2Feign.class, "http://localhost:8082/");
       return client2Feign.sayHelloFeignBuilder();
    }

    @GetMapping("/hello/say")
    public String sayHelloFeignBuilder1(){
        return "Hello";
    }

    @GetMapping("/hello/rest")
    public String sayHelloRest(){
        System.out.println(tracer.currentSpan());
        logger.info("Inside rest 1");
        String baseUrl = "http://localhost:8082/sayHelloRest";
        String response = (String) restTemplate.exchange(baseUrl, HttpMethod.GET, null, String.class).getBody();
        logger.info("The response received by client2 is " + response);
        return response;
    }

}

This is yml of my client1 app. I use same yml conf on other client apps which client2 and clint3. Only changes port and app name.


server:
  port: 8081
spring:
  application:
    name: euraka-client1
  zipkin:
    enabled: true
    service.name: euraka-client1
    sender.type: web
    base-url: http://localhost:9411
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
  instance:
    preferIpAddress: true

This is my Feign at Client2 app.

import feign.RequestLine;

public interface Client2Feign {

    @RequestLine("GET /sayHelloBuilder")
    String sayHelloFeignBuilder();

}

Here impl of ClientFeign2.

import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;

public interface FeignBuilderController {

    @GetMapping("/sayHelloBuilder")
    String sayHelloBuilder(HttpServletRequest httpServletRequest);

    @GetMapping("/sayHelloRest")
    String sayHelloRest(HttpServletRequest httpServletRequest);
}
import feign.Client;
import feign.Feign;
import feign.RequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import sample.feign.Client3Feign;
import sample.feign2.Client2Feign;

import javax.servlet.http.HttpServletRequest;

@RestController
public class FeignBuilderControllerImpl implements FeignBuilderController{

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private Client client;

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    @Bean
    public OkHttpClient okHttpClient(){
        return new OkHttpClient();
    }

    @Override
    public String sayHelloBuilder(HttpServletRequest httpServletRequest) {
        logger.info("Send to request client3");
        Client3Feign client3Feign = Feign.builder()
                .client(client)
                .decoder(new Decoder.Default())
                .encoder(new Encoder.Default())
                .target(Client3Feign.class, "http://localhost:8083/");
        return client3Feign.sayHelloFeignBuilder3();
    }

    @Override
    public String sayHelloRest(HttpServletRequest httpServletRequest) {
        logger.info("Inside rest 2");
        String baseUrl = "http://localhost:8083/sayHelloRestClient3";
        String response = (String) restTemplate.exchange(baseUrl, HttpMethod.GET, null, String.class).getBody();
        logger.info("The response received by client3 is " + response);
        return response;
    }

This is my Feign at Client3 app.

import feign.RequestLine;

public interface Client3Feign {

    @RequestLine("GET /sayHelloBuilderClient3")
    String sayHelloFeignBuilder3();

}

Here impl of Client3 Feign.

import javax.servlet.http.HttpServletRequest;

public interface FeignController3 {

    @GetMapping("/sayHelloBuilderClient3")
    String sayHello3(HttpServletRequest httpServletRequest);

    @GetMapping("/sayHelloRestClient3")
    String sayHelloRest3(HttpServletRequest httpServletRequest);
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping
public class FeignController3Impl implements FeignController3 {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public String sayHello3(HttpServletRequest httpServletRequest) {
        logger.info("Response returned from client3");
        return "Hello from client3 using FeignClientBuilder";
    }

    @Override
    public String sayHelloRest3(HttpServletRequest httpServletRequest) {
        logger.info("Response returned from client3");
        return "Hello from client3 using RestClient";
    }
}

pom.xml from client3 and i use client3 at client2/pom and the same as client1.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>oguzhan.example</groupId>
    <artifactId>client3</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>11</java.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.4.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2020.0.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-sleuth</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-core</artifactId>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-okhttp</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>build-info</id>
                        <goals>
                            <goal>build-info</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <release>${java.version}</release>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

diff-feign-rest-image

feign-zipkin-img

feign-request-img

rstlet-rest-tmplte-img

rest-zipkin-img

Cheryl answered 12/2, 2021 at 7:36 Comment(0)
M
3

The problem might be related to the fact that you're creating the Feign builder manually via Feign.builder() factory method. We're unable to instrument that call. You should create a bean (via SleuthFeignBuilder.builder) and inject that into your code.

Merrimerriam answered 15/2, 2021 at 10:1 Comment(5)
First of all, thank you very much for the answer. Unfortunately it didn't happen. I created the bean like this. After I inject it. @Bean public Feign.Builder builder(){ return Feign.builder(); } @Autowired private Feign.Builder builder;Cheryl
We don't wrap the builders. We wrap whatever the builder producesMerrimerriam
Sorry Marcin i don't understand how to wrap builder produces. Also i try below code. @Bean public Client2Feign client2Feign(){ return Feign.builder() .client(client) .target(Client2Feign.class,"http://localhost:8082"); }Cheryl
Marcin , I understood clearly now. It was solved when I used SleuthFeignBuilder.builder. Thanks.Cheryl
No problem - can we mark this as solved please?Merrimerriam

© 2022 - 2025 — McMap. All rights reserved.