|
| 1 | +/* |
| 2 | + * Copyright 2012-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.autoconfigure.metrics.jersey2.server; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | + |
| 21 | +import javax.ws.rs.GET; |
| 22 | +import javax.ws.rs.Path; |
| 23 | +import javax.ws.rs.PathParam; |
| 24 | + |
| 25 | +import io.micrometer.core.instrument.MeterRegistry; |
| 26 | +import io.micrometer.core.instrument.Tag; |
| 27 | +import io.micrometer.core.instrument.Timer; |
| 28 | +import io.micrometer.jersey2.server.DefaultJerseyTagsProvider; |
| 29 | +import io.micrometer.jersey2.server.JerseyTagsProvider; |
| 30 | +import io.micrometer.jersey2.server.MetricsApplicationEventListener; |
| 31 | +import org.glassfish.jersey.server.ResourceConfig; |
| 32 | +import org.glassfish.jersey.server.monitoring.RequestEvent; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; |
| 36 | +import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration; |
| 37 | +import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun; |
| 38 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 39 | +import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration; |
| 40 | +import org.springframework.boot.autoconfigure.jersey.ResourceConfigCustomizer; |
| 41 | +import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; |
| 42 | +import org.springframework.boot.test.context.FilteredClassLoader; |
| 43 | +import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext; |
| 44 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 45 | +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
| 46 | +import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; |
| 47 | +import org.springframework.context.annotation.Bean; |
| 48 | +import org.springframework.web.client.RestTemplate; |
| 49 | + |
| 50 | +import static org.assertj.core.api.Assertions.assertThat; |
| 51 | + |
| 52 | +/** |
| 53 | + * Tests for {@link JerseyServerMetricsAutoConfiguration}. |
| 54 | + * |
| 55 | + * @author Michael Weirauch |
| 56 | + * @author Michael Simons |
| 57 | + */ |
| 58 | +public class JerseyServerMetricsAutoConfigurationTests { |
| 59 | + |
| 60 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 61 | + .with(MetricsRun.simple()).withConfiguration( |
| 62 | + AutoConfigurations.of(JerseyServerMetricsAutoConfiguration.class)); |
| 63 | + |
| 64 | + private final WebApplicationContextRunner webContextRunner = new WebApplicationContextRunner( |
| 65 | + AnnotationConfigServletWebServerApplicationContext::new) |
| 66 | + .withConfiguration( |
| 67 | + AutoConfigurations.of(JerseyAutoConfiguration.class, |
| 68 | + JerseyServerMetricsAutoConfiguration.class, |
| 69 | + ServletWebServerFactoryAutoConfiguration.class, |
| 70 | + SimpleMetricsExportAutoConfiguration.class, |
| 71 | + MetricsAutoConfiguration.class)) |
| 72 | + .withUserConfiguration(ResourceConfiguration.class) |
| 73 | + .withPropertyValues("server.port:0"); |
| 74 | + |
| 75 | + @Test |
| 76 | + public void shouldOnlyBeActiveInWebApplicationContext() { |
| 77 | + this.contextRunner.run((context) -> assertThat(context) |
| 78 | + .doesNotHaveBean(ResourceConfigCustomizer.class)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void shouldProvideAllNecessaryBeans() { |
| 83 | + this.webContextRunner.run((context) -> assertThat(context) |
| 84 | + .hasSingleBean(DefaultJerseyTagsProvider.class) |
| 85 | + .hasSingleBean(ResourceConfigCustomizer.class)); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void shouldHonorExistingTagProvider() { |
| 90 | + this.webContextRunner |
| 91 | + .withUserConfiguration(CustomJerseyTagsProviderConfiguration.class) |
| 92 | + .run((context) -> assertThat(context) |
| 93 | + .hasSingleBean(CustomJerseyTagsProvider.class)); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void httpRequestsAreTimed() { |
| 98 | + this.webContextRunner.run((context) -> { |
| 99 | + doRequest(context); |
| 100 | + |
| 101 | + MeterRegistry registry = context.getBean(MeterRegistry.class); |
| 102 | + Timer timer = registry.get("http.server.requests").tag("uri", "/users/{id}") |
| 103 | + .timer(); |
| 104 | + assertThat(timer.count()).isEqualTo(1); |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void noHttpRequestsTimedWhenJerseyInstrumentationMissingFromClasspath() { |
| 110 | + this.webContextRunner |
| 111 | + .withClassLoader( |
| 112 | + new FilteredClassLoader(MetricsApplicationEventListener.class)) |
| 113 | + .run((context) -> { |
| 114 | + doRequest(context); |
| 115 | + |
| 116 | + MeterRegistry registry = context.getBean(MeterRegistry.class); |
| 117 | + assertThat(registry.find("http.server.requests").timer()).isNull(); |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + private static void doRequest(AssertableWebApplicationContext context) { |
| 122 | + int port = context |
| 123 | + .getSourceApplicationContext( |
| 124 | + AnnotationConfigServletWebServerApplicationContext.class) |
| 125 | + .getWebServer().getPort(); |
| 126 | + RestTemplate restTemplate = new RestTemplate(); |
| 127 | + restTemplate.getForEntity(URI.create("http://localhost:" + port + "/users/3"), |
| 128 | + String.class); |
| 129 | + } |
| 130 | + |
| 131 | + static class ResourceConfiguration { |
| 132 | + |
| 133 | + @Bean |
| 134 | + ResourceConfig resourceConfig() { |
| 135 | + return new ResourceConfig().register(new TestResource()); |
| 136 | + } |
| 137 | + |
| 138 | + @Path("/users") |
| 139 | + public class TestResource { |
| 140 | + |
| 141 | + @GET |
| 142 | + @Path("/{id}") |
| 143 | + public String getUser(@PathParam("id") String id) { |
| 144 | + return id; |
| 145 | + } |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + static class CustomJerseyTagsProviderConfiguration { |
| 152 | + |
| 153 | + @Bean |
| 154 | + JerseyTagsProvider customJerseyTagsProvider() { |
| 155 | + return new CustomJerseyTagsProvider(); |
| 156 | + } |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + static class CustomJerseyTagsProvider implements JerseyTagsProvider { |
| 161 | + |
| 162 | + @Override |
| 163 | + public Iterable<Tag> httpRequestTags(RequestEvent event) { |
| 164 | + return null; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public Iterable<Tag> httpLongRequestTags(RequestEvent event) { |
| 169 | + return null; |
| 170 | + } |
| 171 | + |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments