httpclient的监控流程源码解读

 更新时间:2023年10月08日 08:31:23   作者:codecraft  
这篇文章主要为大家介绍了httpclient的监控流程源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

本文主要研究一下如何监控httpclient

MicrometerHttpRequestExecutor

micrometer-core-1.3.0-sources.jar!/io/micrometer/core/instrument/binder/httpcomponents/MicrometerHttpRequestExecutor.java

@Incubating(since = "1.2.0")
public class MicrometerHttpRequestExecutor extends HttpRequestExecutor {
    /**
     * Default header name for URI pattern.
     */
    public static final String DEFAULT_URI_PATTERN_HEADER = "URI_PATTERN";
    private static final String METER_NAME = "httpcomponents.httpclient.request";
    private static final String UNKNOWN = "UNKNOWN";
    private static final Tag STATUS_UNKNOWN = Tag.of("status", UNKNOWN);
    private static final Tag STATUS_CLIENT_ERROR = Tag.of("status", "CLIENT_ERROR");
    private static final Tag STATUS_IO_ERROR = Tag.of("status", "IO_ERROR");
    private final MeterRegistry registry;
    private final Function<HttpRequest, String> uriMapper;
    private final Iterable<Tag> extraTags;
    private final boolean exportTagsForRoute;
    /**
     * Use {@link #builder(MeterRegistry)} to create an instance of this class.
     */
    private MicrometerHttpRequestExecutor(int waitForContinue,
                                          MeterRegistry registry,
                                          Function<HttpRequest, String> uriMapper,
                                          Iterable<Tag> extraTags,
                                          boolean exportTagsForRoute) {
        super(waitForContinue);
        this.registry = Optional.ofNullable(registry).orElseThrow(() -> new IllegalArgumentException("registry is required but has been initialized with null"));
        this.uriMapper = Optional.ofNullable(uriMapper).orElseThrow(() -> new IllegalArgumentException("uriMapper is required but has been initialized with null"));
        this.extraTags = Optional.ofNullable(extraTags).orElse(Collections.emptyList());
        this.exportTagsForRoute = exportTagsForRoute;
    }
    //......
}
MicrometerHttpRequestExecutor继承了HttpRequestExecutor,它覆盖了execute方法,使用timer来监控,定义了method、uri、status、routeTags(target.scheme、target.host、target.port)、extraTags这几个tag,metric名为httpcomponents.httpclient.request。
HttpClientBuilder.create()
          .setRequestExecutor(MicrometerHttpRequestExecutor
                  .builder(meterRegistry)
                  .build())
          .build();
具体使用可以通过setRequestExecutor来设置MicrometerHttpRequestExecutor

Builder

public static Builder builder(MeterRegistry registry) {
        return new Builder(registry);
    }
    public static class Builder {
        private final MeterRegistry registry;
        private int waitForContinue = HttpRequestExecutor.DEFAULT_WAIT_FOR_CONTINUE;
        private Iterable<Tag> tags = Collections.emptyList();
        private Function<HttpRequest, String> uriMapper = new DefaultUriMapper();
        private boolean exportTagsForRoute = false;
        Builder(MeterRegistry registry) {
            this.registry = registry;
        }
        /**
         * @param waitForContinue Overrides the wait for continue time for this
         *                        request executor. See {@link HttpRequestExecutor}
         *                        for details.
         * @return This builder instance.
         */
        public Builder waitForContinue(int waitForContinue) {
            this.waitForContinue = waitForContinue;
            return this;
        }
        /**
         * @param tags Additional tags which should be exposed with every value.
         * @return This builder instance.
         */
        public Builder tags(Iterable<Tag> tags) {
            this.tags = tags;
            return this;
        }
        /**
         * Allows to register a mapping function for exposing request URIs. Be
         * careful, exposing request URIs could result in a huge number of tag
         * values, which could cause problems in your meter registry.
         *
         * By default, this feature is almost disabled. It only exposes values
         * of the {@link #DEFAULT_URI_PATTERN_HEADER} HTTP header.
         *
         * @param uriMapper A mapper that allows mapping and exposing request
         *                  paths.
         * @return This builder instance.
         * @see DefaultUriMapper
         */
        public Builder uriMapper(Function<HttpRequest, String> uriMapper) {
            this.uriMapper = uriMapper;
            return this;
        }
        /**
         * Allows to expose the target scheme, host and port with every metric.
         * Be careful with enabling this feature: If your client accesses a huge
         * number of remote servers, this would result in a huge number of tag
         * values, which could cause cardinality problems.
         *
         * By default, this feature is disabled.
         *
         * @param exportTagsForRoute Set this to true, if the metrics should be
         *                           tagged with the target route.
         * @return This builder instance.
         */
        public Builder exportTagsForRoute(boolean exportTagsForRoute) {
            this.exportTagsForRoute = exportTagsForRoute;
            return this;
        }
        /**
         * @return Creates an instance of {@link MicrometerHttpRequestExecutor}
         * with all the configured properties.
         */
        public MicrometerHttpRequestExecutor build() {
            return new MicrometerHttpRequestExecutor(waitForContinue, registry, uriMapper, tags, exportTagsForRoute);
        }
    }
MicrometerHttpRequestExecutor提供了builder方法,可以设置waitForContinue、tags、uriMapper(默认是DefaultUriMapper)、exportTagsForRoute

DefaultUriMapper

/**
     * Extracts the pattern from the request header of the request if available.
     */
    private static class DefaultUriMapper implements Function<HttpRequest, String> {
        @Override
        public String apply(HttpRequest httpRequest) {
            Header uriPattern = httpRequest.getLastHeader(DEFAULT_URI_PATTERN_HEADER);
            if (uriPattern != null && uriPattern.getValue() != null) {
                return uriPattern.getValue();
            }
            return UNKNOWN;
        }
    }
DefaultUriMapper用于获取uriPattern

MicrometerHttpClientInterceptor

micrometer-core-1.5.9.jar!/io/micrometer/core/instrument/binder/httpcomponents/MicrometerHttpClientInterceptor.java

@Incubating(
    since = "1.4.0"
)
public class MicrometerHttpClientInterceptor {
    private static final String METER_NAME = "httpcomponents.httpclient.request";
    private final Map<HttpContext, Timer.Sample> timerByHttpContext;
    private final HttpRequestInterceptor requestInterceptor;
    private final HttpResponseInterceptor responseInterceptor;
    public MicrometerHttpClientInterceptor(MeterRegistry meterRegistry, Function<HttpRequest, String> uriMapper, Iterable<Tag> extraTags, boolean exportTagsForRoute) {
        this.timerByHttpContext = new ConcurrentHashMap();
        this.requestInterceptor = (request, context) -> {
            this.timerByHttpContext.put(context, Timer.start(meterRegistry).tags(new String[]{"method", request.getRequestLine().getMethod(), "uri", (String)uriMapper.apply(request)}));
        };
        this.responseInterceptor = (response, context) -> {
            Timer.Sample sample = (Timer.Sample)this.timerByHttpContext.remove(context);
            sample.stop(meterRegistry, Timer.builder("httpcomponents.httpclient.request").tag("status", Integer.toString(response.getStatusLine().getStatusCode())).tags(exportTagsForRoute ? HttpContextUtils.generateTagsForRoute(context) : Tags.empty()).tags(extraTags));
        };
    }
    public MicrometerHttpClientInterceptor(MeterRegistry meterRegistry, Iterable<Tag> extraTags, boolean exportTagsForRoute) {
        this(meterRegistry, new DefaultUriMapper(), extraTags, exportTagsForRoute);
    }
    public HttpRequestInterceptor getRequestInterceptor() {
        return this.requestInterceptor;
    }
    public HttpResponseInterceptor getResponseInterceptor() {
        return this.responseInterceptor;
    }
}
micrometer1.4.0版本开始提供了MicrometerHttpClientInterceptor,它定义了requestInterceptor、responseInterceptor,通过timer来上报名为httpcomponents.httpclient.request,tag为method、uri、status、exportTagsForRoute、extraTags的指标

PoolingHttpClientConnectionManagerMetricsBinder

io/micrometer/core/instrument/binder/httpcomponents/PoolingHttpClientConnectionManagerMetricsBinder.java

public class PoolingHttpClientConnectionManagerMetricsBinder implements MeterBinder {
    private final PoolingHttpClientConnectionManager connectionManager;
    private final Iterable<Tag> tags;
    /**
     * Creates a metrics binder for the given pooling connection manager.
     *
     * @param connectionManager The connection manager to monitor.
     * @param name Name of the connection manager. Will be added as tag with the
     *             key "httpclient".
     * @param tags Tags to apply to all recorded metrics. Must be an even number
     *             of arguments representing key/value pairs of tags.
     */
    @SuppressWarnings("WeakerAccess")
    public PoolingHttpClientConnectionManagerMetricsBinder(PoolingHttpClientConnectionManager connectionManager, String name, String... tags) {
        this(connectionManager, name, Tags.of(tags));
    }
    /**
     * Creates a metrics binder for the given pooling connection manager.
     *
     * @param connectionManager The connection manager to monitor.
     * @param name Name of the connection manager. Will be added as tag with the
     *             key "httpclient".
     * @param tags Tags to apply to all recorded metrics.
     */
    @SuppressWarnings("WeakerAccess")
    public PoolingHttpClientConnectionManagerMetricsBinder(PoolingHttpClientConnectionManager connectionManager, String name, Iterable<Tag> tags) {
        this.connectionManager = connectionManager;
        this.tags = Tags.concat(tags, "httpclient", name);
    }
    @Override
    public void bindTo(@NonNull MeterRegistry registry) {
        registerTotalMetrics(registry);
    }
    private void registerTotalMetrics(MeterRegistry registry) {
        Gauge.builder("httpcomponents.httpclient.pool.total.max",
            connectionManager,
            (connectionManager) -> connectionManager.getTotalStats().getMax())
            .description("The configured maximum number of allowed persistent connections for all routes.")
            .tags(tags)
            .register(registry);
        Gauge.builder("httpcomponents.httpclient.pool.total.connections",
            connectionManager,
            (connectionManager) -> connectionManager.getTotalStats().getAvailable())
            .description("The number of persistent and leased connections for all routes.")
            .tags(tags).tag("state", "available")
            .register(registry);
        Gauge.builder("httpcomponents.httpclient.pool.total.connections",
            connectionManager,
            (connectionManager) -> connectionManager.getTotalStats().getLeased())
            .description("The number of persistent and leased connections for all routes.")
            .tags(tags).tag("state", "leased")
            .register(registry);
        Gauge.builder("httpcomponents.httpclient.pool.total.pending",
            connectionManager,
            (connectionManager) -> connectionManager.getTotalStats().getPending())
            .description("The number of connection requests being blocked awaiting a free connection for all routes.")
            .tags(tags)
            .register(registry);
        Gauge.builder("httpcomponents.httpclient.pool.route.max.default",
            connectionManager,
            PoolingHttpClientConnectionManager::getDefaultMaxPerRoute)
            .description("The configured default maximum number of allowed persistent connections per route.")
            .tags(tags)
            .register(registry);
    }
}
PoolingHttpClientConnectionManagerMetricsBinder实现了MeterBinder接口,它构造器接收PoolingHttpClientConnectionManager、name、tags,其bindTo方法定义了如下几个metrics
  • httpcomponents.httpclient.pool.total.max
  • httpcomponents.httpclient.pool.total.connections, tag:state=available
  • httpcomponents.httpclient.pool.total.connections, tag:state=leased
  • httpcomponents.httpclient.pool.total.pending
  • httpcomponents.httpclient.pool.route.max.default

小结

micrometer为apache httpclient提供了对应的binder,用于上报相关metrics,其中1.2.0版本提供了MicrometerHttpRequestExecutor(httpcomponents.httpclient.request),1.4.0版本提供了MicrometerHttpClientInterceptor(httpcomponents.httpclient.request),另外对于连接池提供了PoolingHttpClientConnectionManagerMetricsBinder(httpcomponents.httpclient.pool)进行监控上报。

以上就是httpclient的监控的详细内容,更多关于httpclient监控的资料请关注脚本之家其它相关文章!

相关文章

  • Mybatis中的高级映射一对一、一对多、多对多

    Mybatis中的高级映射一对一、一对多、多对多

    这篇文章主要介绍了Mybatis中的高级映射一对一、一对多、多对多的相关资料,需要的朋友可以参考下
    2016-08-08
  • Java实现读取SFTP服务器指定目录文件的方法

    Java实现读取SFTP服务器指定目录文件的方法

    SFTP是一种在安全通道上传输文件的协议,它是基于SSH(Secure Shell)协议的扩展,用于在客户端和服务器之间进行加密的文件传输,这篇文章主要介绍了Java实现读取SFTP服务器指定目录文件,感兴趣的朋友跟随小编一起看看吧
    2023-08-08
  • Feign调用传输文件异常的解决

    Feign调用传输文件异常的解决

    这篇文章主要介绍了Feign调用传输文件异常的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • SpringCloud Eureka服务发现实现过程

    SpringCloud Eureka服务发现实现过程

    这篇文章主要介绍了SpringCloud Eureka服务发现实现过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • SpringBoot 集成Kaptcha实现验证码功能实例详解

    SpringBoot 集成Kaptcha实现验证码功能实例详解

    在一个web应用中验证码是一个常见的元素。今天给大家介绍一下kaptcha的和springboot一起使用的简单例子。感兴趣的朋友参考下吧
    2017-08-08
  • JAVA内部类示例详解及练习

    JAVA内部类示例详解及练习

    这篇文章主要为大家详细介绍了Java的内部类,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • 关于java.util.Random的实现原理详解

    关于java.util.Random的实现原理详解

    Java实用工具类库中的类java.util.Random提供了产生各种类型随机数的方法,下面这篇文章主要给大家介绍了关于java.util.Random实现原理的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下。
    2017-08-08
  • Java中的transient关键字介绍

    Java中的transient关键字介绍

    这篇文章主要介绍了Java中的transient关键字介绍,需要的朋友可以参考下
    2015-03-03
  • mybatis log4j2打印sql+日志实例代码

    mybatis log4j2打印sql+日志实例代码

    在学习mybatis的时候,如果用log4j2来协助查看调试信息,则会大大提高学习的效率,加快debug速度,下面这篇文章主要给大家介绍了关于mybatis log4j2打印sql+日志的相关资料,需要的朋友可以参考下
    2022-08-08
  • springboot多个service互相调用的事务处理方式

    springboot多个service互相调用的事务处理方式

    这篇文章主要介绍了springboot多个service互相调用的事务处理方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02

最新评论