Remove spring-boot metrics information
This commit is contained in:
@@ -1,142 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2017 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 static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.cloud.netflix.metrics.servo.ServoMetricsAutoConfiguration;
|
||||
import org.springframework.cloud.netflix.metrics.servo.ServoMonitorCache;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
import org.springframework.test.web.client.match.MockRestRequestMatchers;
|
||||
import org.springframework.test.web.client.response.MockRestResponseCreators;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.netflix.servo.MonitorRegistry;
|
||||
import com.netflix.servo.monitor.BasicTimer;
|
||||
import com.netflix.servo.monitor.MonitorConfig;
|
||||
|
||||
/**
|
||||
* @author Jon Schneider
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { MetricsRestTemplateRestTemplateConfig.class,
|
||||
MetricsRestTemplateTestConfig.class })
|
||||
@TestPropertySource(properties = { "netflix.metrics.restClient.metricName=metricName",
|
||||
"spring.aop.proxy-target-class=true" })
|
||||
public class MetricsClientHttpRequestInterceptorTests {
|
||||
@Autowired
|
||||
MonitorRegistry registry;
|
||||
|
||||
@Autowired
|
||||
ServoMonitorCache servoMonitorCache;
|
||||
|
||||
@Autowired
|
||||
RestTemplate restTemplate;
|
||||
|
||||
@Autowired
|
||||
RestTemplate restTemplateWithFakeInterceptorsList;
|
||||
|
||||
@Test
|
||||
public void metricsGatheredWhenSuccessful() {
|
||||
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
|
||||
mockServer.expect(MockRestRequestMatchers.requestTo("/test/123"))
|
||||
.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
|
||||
.andRespond(MockRestResponseCreators.withSuccess("OK", MediaType.APPLICATION_JSON));
|
||||
String s = restTemplate.getForObject("/test/{id}", String.class, 123);
|
||||
|
||||
MonitorConfig.Builder builder = new MonitorConfig.Builder("metricName")
|
||||
.withTag("method", "GET")
|
||||
.withTag("uri", "_test_-id-")
|
||||
.withTag("status", "200")
|
||||
.withTag("clientName", "none");
|
||||
|
||||
BasicTimer timer = servoMonitorCache.getTimer(builder.build());
|
||||
|
||||
assertEquals(1L, (long) timer.getCount());
|
||||
assertEquals("OK", s);
|
||||
|
||||
mockServer.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void restTemplateWithFakeInterceptorList() {
|
||||
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplateWithFakeInterceptorsList);
|
||||
mockServer.expect(MockRestRequestMatchers.requestTo("/test/123"))
|
||||
.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
|
||||
.andRespond(MockRestResponseCreators.withSuccess("OK", MediaType.APPLICATION_JSON));
|
||||
String s = restTemplate.getForObject("/test/{id}", String.class, 123);
|
||||
|
||||
MonitorConfig.Builder builder = new MonitorConfig.Builder("metricName")
|
||||
.withTag("method", "GET")
|
||||
.withTag("uri", "_test_-id-")
|
||||
.withTag("status", "200")
|
||||
.withTag("clientName", "none");
|
||||
|
||||
BasicTimer timer = servoMonitorCache.getTimer(builder.build());
|
||||
|
||||
assertEquals(2L, (long) timer.getCount());
|
||||
assertEquals("OK", s);
|
||||
|
||||
mockServer.verify();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ImportAutoConfiguration({ ServoMetricsAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class, AopAutoConfiguration.class })
|
||||
class MetricsRestTemplateTestConfig {
|
||||
@Bean
|
||||
@Primary
|
||||
public MonitorRegistry monitorRegistry() {
|
||||
return new SimpleMonitorRegistry();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
class MetricsRestTemplateRestTemplateConfig {
|
||||
@Bean
|
||||
@Primary
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
@Bean(name="restTemplateWithFakeInterceptorsList")
|
||||
RestTemplate restTemplateWithFakeInterceptorsList() {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
restTemplate.setInterceptors(Arrays.asList(Mockito.mock(ClientHttpRequestInterceptor.class)));
|
||||
return restTemplate;
|
||||
}
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2017 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 static org.junit.Assert.assertFalse;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.cloud.netflix.metrics.servo.ServoMetricsAutoConfiguration;
|
||||
import org.springframework.cloud.netflix.metrics.servo.ServoMonitorCache;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import com.netflix.servo.MonitorRegistry;
|
||||
import com.netflix.servo.monitor.BasicTimer;
|
||||
import com.netflix.servo.monitor.MonitorConfig;
|
||||
|
||||
/**
|
||||
* @author Jon Schneider
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = MetricsTestConfig.class)
|
||||
@WebAppConfiguration
|
||||
@TestPropertySource(properties = "netflix.metrics.rest.metricName=metricName")
|
||||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
|
||||
public class MetricsHandlerInterceptorIntegrationTests {
|
||||
@Autowired
|
||||
WebApplicationContext webAppContext;
|
||||
|
||||
@Autowired
|
||||
MonitorRegistry registry;
|
||||
|
||||
@Autowired
|
||||
ServoMonitorCache servoMonitorCache;
|
||||
|
||||
MockMvc mvc;
|
||||
|
||||
@Test
|
||||
public void autoConfigurationWiresTheMetricsInterceptor() {
|
||||
assertFalse(webAppContext.getBeansOfType(MetricsHandlerInterceptor.class)
|
||||
.isEmpty());
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void metricsGatheredWhenSuccess() throws Exception {
|
||||
mvc.perform(get("/test/some/request/10")).andExpect(status().isOk());
|
||||
assertTimer("test_some_request_-id-", null, 200);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void metricsGatheredWhenClientRequestBad() throws Exception {
|
||||
mvc.perform(get("/test/some/request/oops"))
|
||||
.andExpect(status().is4xxClientError());
|
||||
assertTimer("test_some_request_-id-", null, 400);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void metricsGatheredWhenUnhandledError() throws Exception {
|
||||
try {
|
||||
mvc.perform(get("/test/some/unhandledError/10")).andExpect(status().isOk());
|
||||
}
|
||||
catch (Exception e) {
|
||||
}
|
||||
assertTimer("test_some_unhandledError_-id-", "RuntimeException", 200);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void metricsGatheredWhenHandledError() throws Exception {
|
||||
mvc.perform(get("/test/some/error/10")).andExpect(status().is4xxClientError());
|
||||
assertTimer("test_some_error_-id-", null, 422);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void metricsGatheredOnRequestMappingWithRegex() throws Exception {
|
||||
mvc.perform(get("/test/some/regex/.aa")).andExpect(status().isOk());
|
||||
assertTimer("test_some_regex_-id-", null, 200);
|
||||
}
|
||||
|
||||
protected void assertTimer(String uriTag, String exceptionType, Integer status) {
|
||||
MonitorConfig.Builder builder = new MonitorConfig.Builder("metricName")
|
||||
.withTag("method", "GET").withTag("uri", uriTag)
|
||||
.withTag("status", status.toString());
|
||||
|
||||
if (exceptionType != null)
|
||||
builder = builder.withTag("exception", exceptionType);
|
||||
|
||||
BasicTimer timer = servoMonitorCache.getTimer(builder.build());
|
||||
Assert.assertEquals(1L, (long) timer.getCount());
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ImportAutoConfiguration({ ServoMetricsAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class })
|
||||
class MetricsTestConfig {
|
||||
@Bean
|
||||
MetricsTestController testController() {
|
||||
return new MetricsTestController();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public MonitorRegistry monitorRegistry() {
|
||||
return new SimpleMonitorRegistry();
|
||||
}
|
||||
}
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/test/some")
|
||||
@ControllerAdvice
|
||||
class MetricsTestController {
|
||||
@RequestMapping("/request/{id}")
|
||||
public String testSomeRequest(@PathVariable Long id) {
|
||||
return id.toString();
|
||||
}
|
||||
|
||||
@RequestMapping("/error/{id}")
|
||||
public String testSomeHandledError(@PathVariable Long id) {
|
||||
throw new IllegalStateException("Boom on $id!");
|
||||
}
|
||||
|
||||
@RequestMapping("/unhandledError/{id}")
|
||||
public String testSomeUnhandledError(@PathVariable Long id) {
|
||||
throw new RuntimeException("Boom on $id!");
|
||||
}
|
||||
|
||||
@RequestMapping("/regex/{id:\\.[a-z]+}")
|
||||
public String testSomeRegexRequest(@PathVariable String id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = IllegalStateException.class)
|
||||
@ResponseStatus(code = HttpStatus.UNPROCESSABLE_ENTITY)
|
||||
ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) {
|
||||
return new ModelAndView("error");
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
/*
|
||||
* 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 static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactory;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactoryTests;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = MetricsRestTemplateTests.App.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = {
|
||||
"spring.application.name=ribbonclienttest", "spring.jmx.enabled=false",
|
||||
"ribbon.http.client.enabled=true" })
|
||||
@DirtiesContext
|
||||
public class MetricsRestTemplateTests extends RibbonClientHttpRequestFactoryTests {
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void requestFactoryIsRibbon() {
|
||||
ClientHttpRequestFactory requestFactory = this.restTemplate.getRequestFactory();
|
||||
assertThat("wrong RequestFactory type: " + requestFactory.getClass(),
|
||||
requestFactory,
|
||||
is(instanceOf(InterceptingClientHttpRequestFactory.class)));
|
||||
|
||||
InterceptingClientHttpRequestFactory intercepting = (InterceptingClientHttpRequestFactory) requestFactory;
|
||||
Object interceptorsField = ReflectionTestUtils.getField(intercepting,
|
||||
"interceptors");
|
||||
assertThat("wrong interceptors type: " + interceptorsField.getClass(),
|
||||
interceptorsField, is(instanceOf(List.class)));
|
||||
@SuppressWarnings("unchecked")
|
||||
List<ClientHttpRequestInterceptor> interceptors = (List<ClientHttpRequestInterceptor>) interceptorsField;
|
||||
assertThat("interceptors is wrong size", interceptors, hasSize(1));
|
||||
assertThat("wrong interceptor type", interceptors.get(0),
|
||||
is(instanceOf(MetricsClientHttpRequestInterceptor.class)));
|
||||
|
||||
Object realRequestFactory = ReflectionTestUtils.getField(intercepting,
|
||||
"requestFactory");
|
||||
assertThat("wrong RequestFactory type: " + realRequestFactory.getClass(),
|
||||
realRequestFactory, is(instanceOf(RibbonClientHttpRequestFactory.class)));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* 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.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import com.netflix.servo.MonitorRegistry;
|
||||
import com.netflix.servo.monitor.Monitor;
|
||||
|
||||
/**
|
||||
* @author Jon Schneider
|
||||
*/
|
||||
public class SimpleMonitorRegistry implements MonitorRegistry {
|
||||
List<Monitor<?>> monitors = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public Collection<Monitor<?>> getRegisteredMonitors() {
|
||||
return monitors;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(Monitor<?> monitor) {
|
||||
monitors.add(monitor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregister(Monitor<?> monitor) {
|
||||
monitors.remove(monitor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRegistered(Monitor<?> monitor) {
|
||||
for (Monitor<?> m : monitors) {
|
||||
if (m.equals(monitor))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* 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.servo;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.springframework.web.servlet.HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cloud.netflix.metrics.DefaultMetricsTagProvider;
|
||||
import org.springframework.cloud.netflix.metrics.MetricsTagProvider;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
|
||||
public class DefaultMetricsTagProviderTests {
|
||||
MetricsTagProvider provider = new DefaultMetricsTagProvider();
|
||||
HttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
@Test
|
||||
public void encodeSlashes() {
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/test/some");
|
||||
request.setAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE, "/test/some");
|
||||
assertUriTagIsEqualTo(request, "test_some");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodePathVariables() {
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/test/some/1");
|
||||
request.setAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE, "/test/some/{id}");
|
||||
assertUriTagIsEqualTo(request, "test_some_-id-");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeRegexBasedUri() {
|
||||
HttpServletRequest request = new MockHttpServletRequest("GET", "/test/some/regex/a{}");
|
||||
request.setAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE, "/test/some/regex/{id:.+{}}");
|
||||
assertUriTagIsEqualTo(request, "test_some_regex_-id-");
|
||||
}
|
||||
|
||||
private void assertUriTagIsEqualTo(HttpServletRequest request, String expectedTag) {
|
||||
assertThat(provider.httpRequestTags(request, response, null, null).get("uri"), is(equalTo(expectedTag)));
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
package org.springframework.cloud.netflix.metrics.servo;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.netflix.servo.annotations.DataSourceType;
|
||||
import com.netflix.servo.monitor.AbstractMonitor;
|
||||
import com.netflix.servo.monitor.MonitorConfig;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class DimensionalServoMetricNamingTests {
|
||||
private DimensionalServoMetricNaming naming = new DimensionalServoMetricNaming();
|
||||
|
||||
@Test
|
||||
public void vanillaServoMetricWorks() {
|
||||
MonitorConfig config = MonitorConfig.builder("testMetric").build();
|
||||
String name = naming.asHierarchicalName(new FixedValueMonitor<>(config, 0));
|
||||
assertThat(name, is(equalTo("testMetric()")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nameWithPeriodWorks() {
|
||||
MonitorConfig config = MonitorConfig.builder("test.Metric").build();
|
||||
String name = naming.asHierarchicalName(new FixedValueMonitor<>(config, 0));
|
||||
assertThat(name, is(equalTo("test.Metric()")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typeTagWorks() {
|
||||
MonitorConfig config = MonitorConfig.builder("testMetric")
|
||||
.withTag(DataSourceType.KEY, DataSourceType.COUNTER.getValue()).build();
|
||||
|
||||
String name = naming.asHierarchicalName(new FixedValueMonitor<>(config, 0));
|
||||
assertThat(name, is(equalTo("testMetric(type=COUNTER)")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void instanceTagWorks() {
|
||||
MonitorConfig config = MonitorConfig.builder("testMetric")
|
||||
.withTag("instance", "instance0").build();
|
||||
String name = naming.asHierarchicalName(new FixedValueMonitor<>(config, 0));
|
||||
assertThat(name, is(equalTo("testMetric(instance=instance0)")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void statisticTagWorks() {
|
||||
MonitorConfig config = MonitorConfig.builder("testMetric")
|
||||
.withTag("statistic", "min").build();
|
||||
String name = naming.asHierarchicalName(new FixedValueMonitor<>(config, 0));
|
||||
assertThat(name, is(equalTo("testMetric(statistic=min)")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allTagsWork() {
|
||||
MonitorConfig config = MonitorConfig.builder("testMetric")
|
||||
.withTag(DataSourceType.KEY, DataSourceType.COUNTER.getValue())
|
||||
.withTag("instance", "instance0").withTag("statistic", "min").build();
|
||||
String name = naming.asHierarchicalName(new FixedValueMonitor<>(config, 0));
|
||||
assertThat(name,
|
||||
is(equalTo("testMetric(instance=instance0,statistic=min,type=COUNTER)")));
|
||||
}
|
||||
|
||||
private class FixedValueMonitor<T> extends AbstractMonitor<T> {
|
||||
T value;
|
||||
|
||||
protected FixedValueMonitor(MonitorConfig config, T value) {
|
||||
super(config);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getValue(int pollerIndex) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
/*
|
||||
* 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.servo;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.netflix.servo.annotations.DataSourceType;
|
||||
import com.netflix.servo.monitor.AbstractMonitor;
|
||||
import com.netflix.servo.monitor.MonitorConfig;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class HierarchicalServoMetricNamingTests {
|
||||
|
||||
private HierarchicalServoMetricNaming naming = new HierarchicalServoMetricNaming();
|
||||
|
||||
@Test
|
||||
public void vanillaServoMetricWorks() {
|
||||
MonitorConfig config = MonitorConfig.builder("testMetric").build();
|
||||
String name = naming.asHierarchicalName(new FixedValueMonitor<>(config, 0));
|
||||
assertThat(name, is(equalTo("servo.testmetric")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nameWithPeriodWorks() {
|
||||
MonitorConfig config = MonitorConfig.builder("test.Metric").build();
|
||||
String name = naming.asHierarchicalName(new FixedValueMonitor<>(config, 0));
|
||||
assertThat(name, is(equalTo("servo.test.metric")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typeTagWorks() {
|
||||
MonitorConfig config = MonitorConfig.builder("testMetric")
|
||||
.withTag(DataSourceType.KEY, DataSourceType.COUNTER.getValue()).build();
|
||||
|
||||
String name = naming.asHierarchicalName(new FixedValueMonitor<>(config, 0));
|
||||
assertThat(name, is(equalTo("counter.servo.testmetric")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void instanceTagWorks() {
|
||||
MonitorConfig config = MonitorConfig.builder("testMetric")
|
||||
.withTag("instance", "instance0").build();
|
||||
String name = naming.asHierarchicalName(new FixedValueMonitor<>(config, 0));
|
||||
assertThat(name, is(equalTo("servo.instance0.testmetric")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void statisticTagWorks() {
|
||||
MonitorConfig config = MonitorConfig.builder("testMetric")
|
||||
.withTag("statistic", "min").build();
|
||||
String name = naming.asHierarchicalName(new FixedValueMonitor<>(config, 0));
|
||||
assertThat(name, is(equalTo("servo.testmetric.min")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void allTagsWork() {
|
||||
MonitorConfig config = MonitorConfig.builder("testMetric")
|
||||
.withTag(DataSourceType.KEY, DataSourceType.COUNTER.getValue())
|
||||
.withTag("instance", "instance0").withTag("statistic", "min").build();
|
||||
String name = naming.asHierarchicalName(new FixedValueMonitor<>(config, 0));
|
||||
assertThat(name, is(equalTo("counter.servo.instance0.testmetric.min")));
|
||||
}
|
||||
|
||||
private class FixedValueMonitor<T> extends AbstractMonitor<T> {
|
||||
T value;
|
||||
|
||||
protected FixedValueMonitor(MonitorConfig config, T value) {
|
||||
super(config);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getValue(int pollerIndex) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* 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.servo;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.ExportMetricReader;
|
||||
import org.springframework.boot.actuate.metrics.reader.MetricReader;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@SpringBootTest
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class NoServoMetricsAutoConfigurationTests {
|
||||
|
||||
@Autowired(required = false)
|
||||
private ServoMetricNaming naming;
|
||||
|
||||
@Autowired(required = false)
|
||||
@ExportMetricReader
|
||||
private MetricReader reader;
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
assertNull(this.naming);
|
||||
assertNotNull(this.reader);
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration(exclude = ServoMetricsAutoConfiguration.class)
|
||||
@Configuration
|
||||
protected static class TestConfiguration {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package org.springframework.cloud.netflix.metrics.servo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.actuate.metrics.Metric;
|
||||
import org.springframework.cloud.netflix.metrics.SimpleMonitorRegistry;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.netflix.servo.monitor.MonitorConfig;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ServoMetricReaderTests {
|
||||
@Test
|
||||
public void singleCompositeMonitorYieldsMultipleActuatorMetrics() {
|
||||
SimpleMonitorRegistry registry = new SimpleMonitorRegistry();
|
||||
ServoMetricReader reader = new ServoMetricReader(registry,
|
||||
new DimensionalServoMetricNaming());
|
||||
|
||||
MonitorConfig.Builder builder = new MonitorConfig.Builder("metricName");
|
||||
ServoMonitorCache servoMonitorCache = new ServoMonitorCache(registry, new ServoMetricsConfigBean());
|
||||
servoMonitorCache.getTimer(builder.build());
|
||||
|
||||
List<Metric<?>> metrics = Lists.newArrayList(reader.findAll());
|
||||
|
||||
List<String> metricNames = new ArrayList<>();
|
||||
for (Metric<?> metric : metrics) {
|
||||
metricNames.add(metric.getName());
|
||||
}
|
||||
Collections.sort(metricNames);
|
||||
|
||||
assertEquals(4, metrics.size());
|
||||
assertEquals("metricName(statistic=count,type=NORMALIZED,unit=MILLISECONDS)",
|
||||
metricNames.get(0));
|
||||
assertEquals("metricName(statistic=max,type=GAUGE,unit=MILLISECONDS)",
|
||||
metricNames.get(1));
|
||||
assertEquals("metricName(statistic=min,type=GAUGE,unit=MILLISECONDS)",
|
||||
metricNames.get(2));
|
||||
assertEquals("metricName(statistic=totalTime,type=NORMALIZED,unit=MILLISECONDS)",
|
||||
metricNames.get(3));
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* 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.servo;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.ExportMetricReader;
|
||||
import org.springframework.boot.actuate.metrics.reader.MetricReader;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@SpringBootTest
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class ServoMetricsAutoConfigurationTests {
|
||||
|
||||
@Autowired(required = false)
|
||||
private ServoMetricNaming naming;
|
||||
|
||||
@Autowired(required = false)
|
||||
@ExportMetricReader
|
||||
private MetricReader reader;
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
assertNotNull(this.naming);
|
||||
assertNotNull(this.reader);
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@Configuration
|
||||
protected static class TestConfiguration {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package org.springframework.cloud.netflix.metrics.servo;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.boot.test.rule.OutputCapture;
|
||||
|
||||
import com.netflix.servo.MonitorRegistry;
|
||||
import com.netflix.servo.monitor.MonitorConfig;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class ServoMonitorCacheTests {
|
||||
|
||||
@Rule
|
||||
public OutputCapture capture = new OutputCapture();
|
||||
|
||||
@Mock
|
||||
MonitorRegistry monitorRegistry;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCacheWarningLog() {
|
||||
ServoMetricsConfigBean config = new ServoMetricsConfigBean();
|
||||
config.setCacheWarningThreshold(1);
|
||||
ServoMonitorCache cache = new ServoMonitorCache(monitorRegistry, config);
|
||||
|
||||
cache.getTimer(MonitorConfig.builder("monitorA").build());
|
||||
|
||||
assertThat(this.capture.toString(), not(containsString("timerCache is above the warning threshold")));
|
||||
|
||||
cache.getTimer(MonitorConfig.builder("monitorB").build());
|
||||
|
||||
assertThat(this.capture.toString(), containsString("timerCache is above the warning threshold"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user