Reached the maximum number of URI tags for http.client.requests

1,799 阅读1分钟

问题描述

线上收到 warn 级别的日志:Reached the maximum number of URI tags for http.client.requests。

http.client.requests 是 SpringBoot Actuator 暴露的一个 Prometheus 监控指标,http.client.requests 指标中有一个名为 uri 的属性,也就是说项目中 Controller 暴露的接口(URI tag)太多了,已经超过了 http.client.requests 配置的上限值,导致部分 uri tag 的指标丢失。

问题解决

在 WebMvcMetricsAutoConfiguration 中找到 http.client.requests 指标的配置代码:this.properties.getWeb().getServer().getRequest().getMetricName() 的值就是 "http.client.requests",this.properties.getWeb().getServer().getMaxUriTags() 就是 http.client.requests 能够采集的 uri tags 上限值。

@Bean
@Order(0)
public MeterFilter metricsHttpServerUriTagFilter() {
   String metricName = this.properties.getWeb().getServer().getRequest().getMetricName();
   MeterFilter filter = new OnlyOnceLoggingDenyMeterFilter(
         () -> String.format("Reached the maximum number of URI tags for '%s'.", metricName));
   return MeterFilter.maximumAllowableTags(metricName, "uri", this.properties.getWeb().getServer().getMaxUriTags(),
         filter);
}

查看 MetricsProperties.Web.Server 的默认配置,maxUriTags = 100,metricName = "http.server.requests"。

public static class Server {

   private final ServerRequest request = new ServerRequest();

   /**
    * Maximum number of unique URI tag values allowed. After the max number of
    * tag values is reached, metrics with additional tag values are denied by
    * filter.
    */
   private int maxUriTags = 100;

   public static class ServerRequest {

      /**
       * Name of the metric for received requests.
       */
      private String metricName = "http.server.requests";

解决:根据项目接口数量调整 application.yml 中的配置,接口数量可使用 IDEA Plugin RestfulTool 查看。

management:
  metrics:
    web:
      server:
        max-uri-tags: 500

注意事项:

  • 在 SpringBoot 中,若采用字符串拼接生成的 uri tag 会被当成不同的 uri tag,推荐做法是使用 path variable 填充路径中的变量。
  • http.client.requests 指标采用延迟初始化的方式,只有访问接口之后,才会初始化对应 uri tag 的指标。也就是说如果你没有访问接口,去 prometheus 里面查指标是查不到的。