|  | 
|  | 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.jersey; | 
|  | 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 | +			MeterRegistry registry = context.getBean(MeterRegistry.class); | 
|  | 101 | +			Timer timer = registry.get("http.server.requests").tag("uri", "/users/{id}") | 
|  | 102 | +					.timer(); | 
|  | 103 | +			assertThat(timer.count()).isEqualTo(1); | 
|  | 104 | +		}); | 
|  | 105 | +	} | 
|  | 106 | + | 
|  | 107 | +	@Test | 
|  | 108 | +	public void noHttpRequestsTimedWhenJerseyInstrumentationMissingFromClasspath() { | 
|  | 109 | +		this.webContextRunner | 
|  | 110 | +				.withClassLoader( | 
|  | 111 | +						new FilteredClassLoader(MetricsApplicationEventListener.class)) | 
|  | 112 | +				.run((context) -> { | 
|  | 113 | +					doRequest(context); | 
|  | 114 | + | 
|  | 115 | +					MeterRegistry registry = context.getBean(MeterRegistry.class); | 
|  | 116 | +					assertThat(registry.find("http.server.requests").timer()).isNull(); | 
|  | 117 | +				}); | 
|  | 118 | +	} | 
|  | 119 | + | 
|  | 120 | +	private static void doRequest(AssertableWebApplicationContext context) { | 
|  | 121 | +		int port = context | 
|  | 122 | +				.getSourceApplicationContext( | 
|  | 123 | +						AnnotationConfigServletWebServerApplicationContext.class) | 
|  | 124 | +				.getWebServer().getPort(); | 
|  | 125 | +		RestTemplate restTemplate = new RestTemplate(); | 
|  | 126 | +		restTemplate.getForEntity(URI.create("http://localhost:" + port + "/users/3"), | 
|  | 127 | +				String.class); | 
|  | 128 | +	} | 
|  | 129 | + | 
|  | 130 | +	static class ResourceConfiguration { | 
|  | 131 | + | 
|  | 132 | +		@Bean | 
|  | 133 | +		ResourceConfig resourceConfig() { | 
|  | 134 | +			return new ResourceConfig().register(new TestResource()); | 
|  | 135 | +		} | 
|  | 136 | + | 
|  | 137 | +		@Path("/users") | 
|  | 138 | +		public class TestResource { | 
|  | 139 | + | 
|  | 140 | +			@GET | 
|  | 141 | +			@Path("/{id}") | 
|  | 142 | +			public String getUser(@PathParam("id") String id) { | 
|  | 143 | +				return id; | 
|  | 144 | +			} | 
|  | 145 | + | 
|  | 146 | +		} | 
|  | 147 | + | 
|  | 148 | +	} | 
|  | 149 | + | 
|  | 150 | +	static class CustomJerseyTagsProviderConfiguration { | 
|  | 151 | + | 
|  | 152 | +		@Bean | 
|  | 153 | +		JerseyTagsProvider customJerseyTagsProvider() { | 
|  | 154 | +			return new CustomJerseyTagsProvider(); | 
|  | 155 | +		} | 
|  | 156 | + | 
|  | 157 | +	} | 
|  | 158 | + | 
|  | 159 | +	static class CustomJerseyTagsProvider implements JerseyTagsProvider { | 
|  | 160 | + | 
|  | 161 | +		@Override | 
|  | 162 | +		public Iterable<Tag> httpRequestTags(RequestEvent event) { | 
|  | 163 | +			return null; | 
|  | 164 | +		} | 
|  | 165 | + | 
|  | 166 | +		@Override | 
|  | 167 | +		public Iterable<Tag> httpLongRequestTags(RequestEvent event) { | 
|  | 168 | +			return null; | 
|  | 169 | +		} | 
|  | 170 | + | 
|  | 171 | +	} | 
|  | 172 | + | 
|  | 173 | +} | 
0 commit comments