可观测性-Metrics-WebClient异步Http远程Call

代码示例

1.依赖导入

xml 复制代码
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

2.定义一个远程call客户端

java 复制代码
@Service
public class BaiduClient {
    @Bean
    public WebClient baidu(WebClient.Builder webClientBuilder) {
        return  webClientBuilder.baseUrl("https://xxxx.cn").build();
    }
}

3.定义一个指标注册器(输出到日志文件)

java 复制代码
@Configuration
public class MetricsRegistryConfiguration {

    @ConditionalOnProperty(name = "metrics.export", havingValue = "log", matchIfMissing = true)
    @Bean
    public MeterRegistry meterRegistry() {
        return new LoggingMeterRegistry(new LoggingRegistryConfig() {
            @Override
            public String get(String key) {
                return null;
            }

            @Override
            public Duration step() {
                return Duration.ofSeconds(60);
            }
        }, Clock.SYSTEM);
    }

}

4.日志配置

xml 复制代码
 <!-- 定义metrics的输出为日志 INFO -->
    <logger name="io.micrometer.core.instrument.logging.LoggingMeterRegistry" level="INFO"/>

5.调用示例

java 复制代码
@Controller
public class XXXXController {
    @Autowired
    WebClient baidu;

    @GetMapping("/test")
    @ResponseBody
    public Response captcha() {
        baidu.get()
                .uri("/echo")
                .retrieve().bodyToMono(String.class)
                .block(Duration.ofSeconds(10));
    }

6.示例结果日志

ini 复制代码
http.client.requests{client.name=xxx.cn,error=none,exception=none,method=GET,outcome=SUCCESS,status=200,uri=/echo} throughput=0.3/s mean=0.271909994s max=0.6102772s

高级

你还可以使用ObservationWebClientCustomizer定制。

默认情况下,生成的指标名称为http.client.requests。您可以通过设置属性来自定义名称

  • management.observations.http.client.requests.name(3.X版本)
  • management.metrics.web.client.requests-metric-name(2.X)

自定义标签,提供一个实现自org.springframework.web.reactive.function.client包中的 ClientRequestObservationConvention 的 @Bean

低基数键

名称 描述
method(必需) HTTP 请求方法的名称,如果无法创建请求则为 "none"。
uri(必需) 用于 HTTP 请求的 URI 模板,如果未提供则为 "none"。仅考虑 URI 的路径部分。
client.name(必需) 从请求 URI 主机派生的客户端名称。
status(必需) HTTP 响应的原始状态代码,或者在发生 IOException 时为 "IO_ERROR",或者如果未接收到响应则为 "CLIENT_ERROR"。
outcome(必需) HTTP 客户端交换的结果。
exception(必需) 在交换过程中抛出的异常的名称,如果没有发生异常则为 "none"。

高基数键

名称 描述
http.url(必需) HTTP 请求的 URI。

参考

相关推荐
喜欢吃燃面几秒前
HTTP协议深度解析:从请求响应到状态管理与安全传输
linux·网络协议·学习
比兔代理30 分钟前
静态住宅IP在品牌保护中的角色:稳定出口与长期监测
服务器·网络·ip
傻啦嘿哟38 分钟前
某宝商品爬虫:破解反爬的终极方案(含IP代理池+User-Agent轮换)
爬虫·网络协议·tcp/ip
布莱克60540 分钟前
如何实现断点续传和分片上传?
网络·tcp/ip·状态模式
哈基咩1 小时前
Function Calling 与 Code Mode:AI Agent 工具调用的原理、对比与选型指南
网络·人工智能
优化Henry1 小时前
5G站点BBU功能模块集成化与偶发案例
运维·网络·学习·5g·tdd
小尘要自信2 小时前
软件远控失效以后怎么办?玩客云 + One-KVM 实现 BIOS 级远程画面与键鼠控制
服务器·网络·数据库
P1Browser2 小时前
指纹浏览器安全吗?从浏览器环境隔离、数据存储到安全风险解析
网络·tcp/ip·安全·网络安全·github·php
2401_895366502 小时前
BGP综合实验
网络
数据知道3 小时前
PHP 代码审计实战——ThinkPHP、Laravel 历史漏洞模式深度剖析
android·网络·web安全·网络安全·php·laravel