You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Feign.Client指标无法捕获ResponseCode 如何实现响应码指标采集

Feign Client 指标新增 Response Code 标签实现方案

根据你使用的Spring Boot版本不同,可选择对应适配方案:

方案1:Spring Boot 3.x / Spring Cloud 2022.0+ 版本(推荐)

该版本栈默认使用Observation API管理观测指标,Feign的标签生成逻辑由FeignObservationConvention控制,自定义扩展即可添加响应码标签:

  1. 自定义观测约定实现类
import io.micrometer.core.instrument.Tag;
import org.springframework.cloud.openfeign.observation.DefaultFeignObservationConvention;
import org.springframework.cloud.openfeign.observation.FeignObservationContext;
import java.util.List;

public class CustomFeignObservationConvention extends DefaultFeignObservationConvention {
    @Override
    public List<Tag> getLowCardinalityTags(FeignObservationContext context) {
        List<Tag> defaultTags = super.getLowCardinalityTags(context);
        // 提取响应状态码添加为标签
        if (context.getResponse() != null) {
            defaultTags.add(Tag.of("response_code", String.valueOf(context.getResponse().status())));
        } else {
            // 异常无响应的场景默认标记为500
            defaultTags.add(Tag.of("response_code", "500"));
        }
        return defaultTags;
    }
}
  1. 注册自定义实现为Bean
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignMetricsConfig {
    @Bean
    public FeignObservationConvention customFeignObservationConvention() {
        return new CustomFeignObservationConvention();
    }
}

方案2:Spring Boot 2.x 版本

旧版本栈使用MeterBinder管理Feign指标,可通过包装Feign原生Client自定义标签生成逻辑:

import feign.Client;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import org.springframework.cloud.openfeign.support.MeteredClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class FeignMetricsConfig {
    @Bean
    public Client meteredFeignClient(Client defaultClient, MeterRegistry meterRegistry) {
        return new MeteredClient(defaultClient, meterRegistry, (request, response, throwable) -> {
            List<Tag> tags = new ArrayList<>();
            // 保留原有标签
            tags.add(Tag.of("method", request.httpMethod().name()));
            tags.add(Tag.of("host", request.requestTemplate().feignTarget().url()));
            tags.add(Tag.of("client", request.requestTemplate().feignTarget().type().getName()));
            // 新增响应码标签
            if (response != null) {
                tags.add(Tag.of("response_code", String.valueOf(response.status())));
            } else {
                tags.add(Tag.of("response_code", "500"));
            }
            return tags;
        });
    }
}

效果验证

配置生效后,Feign指标将新增response_code标签,示例如下:

# TYPE feign_Feign_seconds_max gauge
feign_Feign_seconds_max{client="com.example.account.api.AccountApiClient",host="localhost",method="getByAddress",response_code="200",} 0.0
feign_Feign_seconds_max{client="com.example.account.api.AccountApiClient",host="localhost",method="getByAddress",response_code="404",} 0.015

内容的提问来源于stack exchange,提问作者vishal chandrola

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 14:15:01