reactor.netty.http.client.HttpClient reactor.netty.http.server.HttpServe r metrics

对着背影说爱祢 提交于 2020-05-08 18:55:13

6.7. Metrics

HTTP客户端支持与Micrometer内置集成. 它使用前缀reactor.netty.http.client公开所有度量.

下表提供了HTTP客户端指标的信息:

指标名称 type description

reactor.netty.http.client.data.received

DistributionSummary

接收到的数据量,以字节为单位

reactor.netty.http.client.data.sent

DistributionSummary

发送的数据量(以字节为单位)

reactor.netty.http.client.errors

Counter

发生的错误数

reactor.netty.http.client.tls.handshake.time

Timer

TLS握手花费的时间

reactor.netty.http.client.connect.time

Timer

连接到远程地址所花费的时间

reactor.netty.http.client.address.resolver

Timer

解析地址所花费的时间

reactor.netty.http.client.data.received.time

Timer

消耗传入数据所花费的时间

reactor.netty.http.client.data.sent.time

Timer

发送传出数据所花费的时间

reactor.netty.http.client.response.time

Timer

请求/响应的总时间

这些其他指标也可用:

Pooled ConnectionProvider metrics

指标名称 type description

reactor.netty.connection.provider.total.connections

Gauge

所有连接数(活动或空闲)

reactor.netty.connection.provider.active.connections

Gauge

已成功获取并正在使用的连接数

reactor.netty.connection.provider.idle.connections

Gauge

空闲连接数

reactor.netty.connection.provider.pending.connections

Gauge

等待连接的请求数

ByteBufAllocator metrics

指标名称 type description

reactor.netty.bytebuf.allocator.used.heap.memory

Gauge

堆内存的字节数

reactor.netty.bytebuf.allocator.used.direct.memory

Gauge

直接存储器的字节数

reactor.netty.bytebuf.allocator.used.heap.arenas

Gauge

堆竞技场数(当PooledByteBufAllocator 

reactor.netty.bytebuf.allocator.used.direct.arenas

Gauge

直接竞技场数量(当PooledByteBufAllocator 

reactor.netty.bytebuf.allocator.used.threadlocal.caches

Gauge

线程本地缓存的数量(当PooledByteBufAllocator 

reactor.netty.bytebuf.allocator.used.tiny.cache.size

Gauge

微小缓存的大小(当PooledByteBufAllocator 

reactor.netty.bytebuf.allocator.used.small.cache.size

Gauge

小型缓存的大小(当PooledByteBufAllocator 

reactor.netty.bytebuf.allocator.used.normal.cache.size

Gauge

普通缓存的大小(当PooledByteBufAllocator 

reactor.netty.bytebuf.allocator.used.chunk.size

Gauge

竞技场的块大小(当PooledByteBufAllocator 

以下示例启用了该集成:

import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.config.MeterFilter;
import reactor.netty.http.client.HttpClient;

public class Application {

    public static void main(String[] args) {
        Metrics.globalRegistry 
               .config()
               .meterFilter(MeterFilter.maximumAllowableTags("reactor.netty.http.client", "URI", 100, MeterFilter.deny()));

        HttpClient client =
                HttpClient.create()
                          .metrics(true, s -> {
                              if (s.startsWith("/stream/")) { 
                                  return "/stream/{n}";
                              }
                              else if (s.startsWith("/bytes/")) {
                                  return "/bytes/{n}";
                              }
                              return s;
                          }); 

        client.get()
              .uri("http://httpbin.org/stream/2")
              .responseContent()
              .blockLast();

        client.get()
              .uri("http://httpbin.org/bytes/1024")
              .responseContent()
              .blockLast();
    }
}
  对带有URI标记的仪表应用上限
  如果可能,模板化的URI将用作URI标记值
  Enables the built-in integration with Micrometer
  为了避免启用的指标的内存和CPU开销,在可能的情况下将真实URI转换为模板化URI非常重要. 如果不转换为类似模板的形式,则每个不同的URI都会导致创建不同的标记,这会占用大量内存用于度量.
  始终对带有URI标记的仪表应用上限. 在无法对真实URI进行模板化的情况下,配置米数上限会有所帮助. 您可以在maximumAllowableTags找到更多信息.

当需要与Micrometer以外的系统集成时需要HTTP客户端指标,或者您想提供自己与Micrometer的集成时,可以提供自己的指标记录器,如下所示:

import reactor.netty.http.client.HttpClient;
import reactor.netty.http.client.HttpClientResponse;

import java.net.SocketAddress;
import java.time.Duration;

public class Application {

    public static void main(String[] args) {
        HttpClientResponse response =
                HttpClient.create()
                          .metrics(true, () -> new CustomHttpClientMetricsRecorder()) 
                          .get()
                          .uri("https://httpbin.org/stream/2")
                          .response()
                          .block();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!