0

I want to collect metrics for particular REST API Suppose I have a URL like /company/{companyName}/person/{id}

Is it possible to collect metrics across /company/test/person/{id} /compaby/test2/person/{id}

naval jain
  • 353
  • 3
  • 4
  • 14

2 Answers2

1

There's no out-of-the-box support for it but you can provide your own WebMvcTagsProvider to implement it via a Spring bean.

Note that it could lead to tag explosion and end up with OOM if there's any possibility to companyName path variable explosion by a mistake or attack.

Johnny Lim
  • 5,623
  • 8
  • 38
  • 53
1

In case you are using Spring and RestTemplate for http call, you can register MetricsClientHttpRequestInterceptor with your RestTemplate .

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);
  }
}

And use Below method provided by spring RestTemplate to make http call.

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));
}
SUMIT
  • 540
  • 1
  • 4
  • 19