【问题标题】:Metric in micrometer in URI template . Some path variable needs to be replaced from URLURI 模板中以微米为单位的度量。一些路径变量需要从 URL 替换
【发布时间】:2018-10-04 07:22:17
【问题描述】:

我想收集特定 REST API 的指标 假设我有一个像 /company/{companyName}/person/{id}

这样的 URL

是否可以收集指标 /company/test/person/{id} /compaby/test2/person/{id}

【问题讨论】:

    标签: spring-micrometer


    【解决方案1】:

    没有开箱即用的支持,但您可以提供自己的 WebMvcTagsProvider 以通过 Spring bean 实现它。

    请注意,如果companyName路径变量因错误或攻击而爆炸可能会导致标签爆炸并最终导致OOM。

    【讨论】:

      【解决方案2】:

      如果您使用 Spring 和 RestTemplate 进行 http 调用,您可以使用 RestTemplate 注册 MetricsClientHttpRequestInterceptor 。

      import javax.annotation.PostConstruct;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
      import org.springframework.boot.actuate.metrics.web.client.MetricsRestTemplateCustomizer;
      import org.springframework.boot.autoconfigure.AutoConfigureAfter;
      import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
      import org.springframework.context.ApplicationContext;
      import org.springframework.stereotype.Component;
      import org.springframework.web.client.RestTemplate;
      
      @Component
      @AutoConfigureAfter({MetricsAutoConfiguration.class})
      public class RestClientMetricConfiguration {
      
        private final ApplicationContext applicationContext;
      
        @Autowired
        public RestClientMetricConfiguration(ApplicationContext applicationContext) {
          this.applicationContext = applicationContext;
        }
      
        @PostConstruct
        public void init() {
          MetricsRestTemplateCustomizer restTemplateCustomizer = 
          applicationContext.getBean(MetricsRestTemplateCustomizer.class);
          applicationContext.getBeansOfType(RestTemplate.class).values().forEach(restTemplateCustomizer::customize);
        }
      }
      

      并使用spring RestTemplate提供的Below方法进行http调用。

      public <T> ResponseEntity<T> exchange(String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, ParameterizedTypeReference<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
          Type type = responseType.getType();
          RequestCallback requestCallback = this.httpEntityCallback(requestEntity, type);
          ResponseExtractor<ResponseEntity<T>> responseExtractor = this.responseEntityExtractor(type);
          return (ResponseEntity)nonNull(this.execute(url, method, requestCallback, responseExtractor, uriVariables));
      }
      

      【讨论】:

        猜你喜欢
        • 2012-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-15
        • 2020-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多