-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #615 from jkschneider/metrics
* metrics: Expanded metrics support to include Servo, Spectator, and Atlas
- Loading branch information
Showing
49 changed files
with
2,820 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...re/src/main/java/org/springframework/cloud/netflix/metrics/DefaultMetricsTagProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2013-2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.netflix.metrics; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.http.HttpRequest; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
import org.springframework.web.servlet.HandlerMapping; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
|
||
/** | ||
* @author Jon Schneider | ||
*/ | ||
public class DefaultMetricsTagProvider implements MetricsTagProvider { | ||
@Override | ||
public Map<String, String> clientHttpRequestTags(HttpRequest request, | ||
ClientHttpResponse response) { | ||
String urlTemplate = RestTemplateUrlTemplateHolder.getRestTemplateUrlTemplate(); | ||
if (urlTemplate == null) | ||
urlTemplate = "none"; | ||
|
||
String status; | ||
try { | ||
status = (response == null) ? "CLIENT_ERROR" : ((Integer) response | ||
.getRawStatusCode()).toString(); | ||
} | ||
catch (IOException e) { | ||
status = "IO_ERROR"; | ||
} | ||
|
||
String host = request.getURI().getHost(); | ||
|
||
return ImmutableMap.of("method", request.getMethod().name(), "uri", | ||
sanitizeUrlTemplate(urlTemplate.replaceAll("^https?://[^/]+/", "")), | ||
"status", status, "clientName", host != null ? host : "none"); | ||
} | ||
|
||
@Override | ||
public Map<String, String> httpRequestTags(HttpServletRequest request, | ||
HttpServletResponse response, Object handler, String caller) { | ||
Map<String, String> tags = new HashMap<>(); | ||
|
||
tags.put("method", request.getMethod()); | ||
tags.put("status", ((Integer) response.getStatus()).toString()); | ||
|
||
String uri = sanitizeUrlTemplate(request | ||
.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString() | ||
.substring(1)); | ||
tags.put("uri", uri.isEmpty() ? "root" : uri); | ||
|
||
Object exception = request.getAttribute("exception"); | ||
if (exception != null) | ||
tags.put("exception", exception.getClass().getSimpleName()); | ||
|
||
if (caller != null) | ||
tags.put("caller", caller); | ||
|
||
return tags; | ||
} | ||
|
||
/** | ||
* As is, the urlTemplate is not suitable for use with Atlas, as all interactions with | ||
* Atlas take place via query parameters | ||
*/ | ||
private String sanitizeUrlTemplate(String urlTemplate) { | ||
return urlTemplate.replaceAll("/", "_").replaceAll("[{}]", "-"); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...n/java/org/springframework/cloud/netflix/metrics/MetricsClientHttpRequestInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2013-2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.netflix.metrics; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.cloud.netflix.metrics.servo.ServoMonitorCache; | ||
import org.springframework.http.HttpRequest; | ||
import org.springframework.http.client.ClientHttpRequestExecution; | ||
import org.springframework.http.client.ClientHttpRequestInterceptor; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
|
||
import com.netflix.servo.MonitorRegistry; | ||
import com.netflix.servo.monitor.MonitorConfig; | ||
import com.netflix.servo.tag.SmallTagMap; | ||
import com.netflix.servo.tag.Tags; | ||
|
||
/** | ||
* Intercepts RestTemplate requests and records metrics about execution time and results. | ||
* | ||
* @author Jon Schneider | ||
*/ | ||
public class MetricsClientHttpRequestInterceptor implements ClientHttpRequestInterceptor { | ||
/** | ||
* The interceptor writes to a Servo MonitorRegistry, which we get away with for now | ||
* because our Spectator implementation is underpinned by a ServoRegistry. When Spring | ||
* Boot (Actuator) provides a more general purpose abstraction for dimensional metrics | ||
* systems, this can be moved there and rewritten against that abstraction. | ||
*/ | ||
@Autowired | ||
MonitorRegistry registry; | ||
|
||
@Autowired | ||
Collection<MetricsTagProvider> tagProviders; | ||
|
||
@Value("${netflix.metrics.restClient.metricName:restclient}") | ||
String metricName; | ||
|
||
@Override | ||
public ClientHttpResponse intercept(HttpRequest request, byte[] body, | ||
ClientHttpRequestExecution execution) throws IOException { | ||
long startTime = System.nanoTime(); | ||
|
||
ClientHttpResponse response = null; | ||
try { | ||
response = execution.execute(request, body); | ||
return response; | ||
} | ||
finally { | ||
SmallTagMap.Builder builder = SmallTagMap.builder(); | ||
for (MetricsTagProvider tagProvider : tagProviders) { | ||
for (Map.Entry<String, String> tag : tagProvider.clientHttpRequestTags( | ||
request, response).entrySet()) { | ||
builder.add(Tags.newTag(tag.getKey(), tag.getValue())); | ||
} | ||
} | ||
|
||
MonitorConfig.Builder monitorConfigBuilder = MonitorConfig | ||
.builder(metricName); | ||
monitorConfigBuilder.withTags(builder); | ||
|
||
ServoMonitorCache.getTimer(monitorConfigBuilder.build()).record( | ||
System.nanoTime() - startTime, TimeUnit.NANOSECONDS); | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...re/src/main/java/org/springframework/cloud/netflix/metrics/MetricsHandlerInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright 2013-2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.netflix.metrics; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.cloud.netflix.metrics.servo.ServoMonitorCache; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | ||
|
||
import com.netflix.servo.MonitorRegistry; | ||
import com.netflix.servo.monitor.MonitorConfig; | ||
import com.netflix.servo.tag.SmallTagMap; | ||
import com.netflix.servo.tag.Tags; | ||
|
||
import static org.springframework.web.context.request.RequestAttributes.SCOPE_REQUEST; | ||
|
||
/** | ||
* Intercepts incoming HTTP requests and records metrics about execution time and results. | ||
* | ||
* @author Jon Schneider | ||
*/ | ||
public class MetricsHandlerInterceptor extends HandlerInterceptorAdapter { | ||
@Value("${netflix.metrics.rest.metricName:rest}") | ||
String metricName; | ||
|
||
@Value("${netflix.metrics.rest.callerHeader:#{null}}") | ||
String callerHeader; | ||
|
||
@Autowired | ||
MonitorRegistry registry; | ||
|
||
@Autowired | ||
Collection<MetricsTagProvider> tagProviders; | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, | ||
Object handler) throws Exception { | ||
RequestContextHolder.getRequestAttributes().setAttribute("requestStartTime", | ||
System.nanoTime(), SCOPE_REQUEST); | ||
return super.preHandle(request, response, handler); | ||
} | ||
|
||
@Override | ||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, | ||
Object handler, Exception ex) throws Exception { | ||
RequestContextHolder.getRequestAttributes().setAttribute("exception", ex, | ||
SCOPE_REQUEST); | ||
Long startTime = (Long) RequestContextHolder.getRequestAttributes().getAttribute( | ||
"requestStartTime", SCOPE_REQUEST); | ||
if (startTime != null) | ||
recordMetric(request, response, handler, startTime); | ||
super.afterCompletion(request, response, handler, ex); | ||
} | ||
|
||
protected void recordMetric(HttpServletRequest request, HttpServletResponse response, | ||
Object handler, Long startTime) { | ||
String caller = null; | ||
if (callerHeader != null) { | ||
caller = request.getHeader(callerHeader); | ||
} | ||
|
||
SmallTagMap.Builder builder = SmallTagMap.builder(); | ||
for (MetricsTagProvider tagProvider : tagProviders) { | ||
Map<String, String> tags = tagProvider.httpRequestTags(request, response, | ||
handler, caller); | ||
for (Map.Entry<String, String> tag : tags.entrySet()) { | ||
builder.add(Tags.newTag(tag.getKey(), tag.getValue())); | ||
} | ||
} | ||
|
||
MonitorConfig.Builder monitorConfigBuilder = MonitorConfig.builder(metricName); | ||
monitorConfigBuilder.withTags(builder); | ||
|
||
ServoMonitorCache.getTimer(monitorConfigBuilder.build()).record( | ||
System.nanoTime() - startTime, TimeUnit.NANOSECONDS); | ||
} | ||
} |
Oops, something went wrong.