Browse Source

Defensive coding to avoid potential NPE

Fixes gh-803
pull/6/head
Dave Syer 9 years ago
parent
commit
82d8a11021
  1. 23
      spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/metrics/DefaultMetricsTagProvider.java

23
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/metrics/DefaultMetricsTagProvider.java

@ -16,6 +16,7 @@ package org.springframework.cloud.netflix.metrics;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -33,13 +34,14 @@ public class DefaultMetricsTagProvider implements MetricsTagProvider {
public Map<String, String> clientHttpRequestTags(HttpRequest request, public Map<String, String> clientHttpRequestTags(HttpRequest request,
ClientHttpResponse response) { ClientHttpResponse response) {
String urlTemplate = RestTemplateUrlTemplateHolder.getRestTemplateUrlTemplate(); String urlTemplate = RestTemplateUrlTemplateHolder.getRestTemplateUrlTemplate();
if (urlTemplate == null) if (urlTemplate == null) {
urlTemplate = "none"; urlTemplate = "none";
}
String status; String status;
try { try {
status = (response == null) ? "CLIENT_ERROR" : ((Integer) response status = (response == null) ? "CLIENT_ERROR"
.getRawStatusCode()).toString(); : ((Integer) response.getRawStatusCode()).toString();
} }
catch (IOException e) { catch (IOException e) {
status = "IO_ERROR"; status = "IO_ERROR";
@ -60,17 +62,22 @@ public class DefaultMetricsTagProvider implements MetricsTagProvider {
tags.put("method", request.getMethod()); tags.put("method", request.getMethod());
tags.put("status", ((Integer) response.getStatus()).toString()); tags.put("status", ((Integer) response.getStatus()).toString());
String uri = sanitizeUrlTemplate(request String uri = (String) request
.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString() .getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
.substring(1)); if (uri == null) {
uri = request.getPathInfo();
}
uri = sanitizeUrlTemplate(uri.substring(1));
tags.put("uri", uri.isEmpty() ? "root" : uri); tags.put("uri", uri.isEmpty() ? "root" : uri);
Object exception = request.getAttribute("exception"); Object exception = request.getAttribute("exception");
if (exception != null) if (exception != null) {
tags.put("exception", exception.getClass().getSimpleName()); tags.put("exception", exception.getClass().getSimpleName());
}
if (caller != null) if (caller != null) {
tags.put("caller", caller); tags.put("caller", caller);
}
return tags; return tags;
} }

Loading…
Cancel
Save