Apache HttpComponents 客户端监控
important
本节内容要求至少使用 Apache HttpComponents Client 5.0 及以上版本。
Apache HttpComponents Client 是一个符合 HTTP/1.1 标准的 HTTP 代理实现。
下面你可以找到一个示例,展示如何使用 Micrometer Observation 来对 Apache HttpComponents Client 进行仪表化。这意味着根据你的 Observation Handler 配置,你只需要仪表化一次,就可以从中获得多种好处(例如指标、分布式追踪)。
仪器化
经典阻塞式 HTTP 客户端示例。
// Setting up instrumentation (you need to create a client from the builder)
HttpClientBuilder clientBuilder = HttpClients.custom()
.setRetryStrategy(retryStrategy)
.addExecInterceptorAfter(ChainElement.RETRY.name(), "micrometer",
new ObservationExecChainHandler(observationRegistry))
.setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create()
.setDefaultConnectionConfig(connectionConfig)
.build());
// Usage example
try (CloseableHttpClient client = classicClient()) {
executeClassic(client, new HttpGet(server.baseUrl()));
}
assertThat(observationRegistry).hasObservationWithNameEqualTo(DEFAULT_METER_NAME)
.that()
.hasLowCardinalityKeyValue(OUTCOME.withValue("SUCCESS"))
.hasLowCardinalityKeyValue(STATUS.withValue("200"))
.hasLowCardinalityKeyValue(METHOD.withValue("GET"));
异步 HTTP 客户端示例
// Setting up instrumentation (you need to create a client from the builder)
HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom()
.addExecInterceptorAfter(ChainElement.RETRY.name(), "micrometer",
new ObservationExecChainHandler(observationRegistry))
.setRetryStrategy(retryStrategy)
.setConnectionManager(PoolingAsyncClientConnectionManagerBuilder.create()
.setDefaultConnectionConfig(connectionConfig)
.build());
// Usage example
try (CloseableHttpAsyncClient client = asyncClient()) {
SimpleHttpRequest request = SimpleRequestBuilder.get(server.baseUrl()).build();
executeAsync(client, request);
}
assertThat(observationRegistry).hasObservationWithNameEqualTo(DEFAULT_METER_NAME)
.that()
.hasLowCardinalityKeyValue(OUTCOME.withValue("SUCCESS"))
.hasLowCardinalityKeyValue(STATUS.withValue("200"))
.hasLowCardinalityKeyValue(METHOD.withValue("GET"));
重试策略考虑因素
HttpClient 支持内置的请求重试处理。与 micrometer 的观测功能类似,此功能通过一个名为 ChainElement.RETRY
的 ExecChainHandler
实现。在对客户端进行插桩时,你需要决定是否观测每次重试。如果 ObservationExecChainHandler
放置在重试处理器 之前,micrometer 将看到初始请求和最后一次重试后的最终结果。耗时将包括重试处理器施加的任何延迟。另一方面,如果 micrometer 注册在重试处理器 之后,它将在每次重试时被调用。耗时将测量单个 HTTP 请求,但 不包括 重试处理器施加的回退延迟。对于经典客户端和异步客户端,上述行为及相关配置是相同的——除了以下说明中的一个例外情况。
important
在使用 异步客户端 时,将 micrometer 放置在重试处理器之前,需要至少 5.3.x 版本的 HttpComponents 客户端库。经典客户端没有其他限制。
示例检测代码。
// Example: setup instrumentation to meter retries individually
HttpClientBuilder clientBuilder = HttpClients.custom()
.setRetryStrategy(retryStrategy)
.addExecInterceptorAfter(ChainElement.RETRY.name(), "micrometer",
new ObservationExecChainHandler(observationRegistry))
.setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create()
.setDefaultConnectionConfig(connectionConfig)
.build());
// Example: setup instrumentation to aggregate retries
HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom()
.addExecInterceptorFirst("micrometer", new ObservationExecChainHandler(observationRegistry))
.setRetryStrategy(retryStrategy)
.setConnectionManager(PoolingAsyncClientConnectionManagerBuilder.create()
.setDefaultConnectionConfig(connectionConfig)
.build());