|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.cxf.systest.jaxrs; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import jakarta.ws.rs.GET; |
| 26 | +import jakarta.ws.rs.Path; |
| 27 | +import jakarta.ws.rs.PathParam; |
| 28 | +import jakarta.ws.rs.Produces; |
| 29 | +import jakarta.ws.rs.core.MediaType; |
| 30 | +import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; |
| 31 | +import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean; |
| 32 | +import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider; |
| 33 | +import org.apache.cxf.metrics.MetricsFeature; |
| 34 | +import org.apache.cxf.metrics.micrometer.MicrometerMetricsProperties; |
| 35 | +import org.apache.cxf.metrics.micrometer.MicrometerMetricsProvider; |
| 36 | +import org.apache.cxf.metrics.micrometer.provider.DefaultExceptionClassProvider; |
| 37 | +import org.apache.cxf.metrics.micrometer.provider.DefaultTimedAnnotationProvider; |
| 38 | +import org.apache.cxf.metrics.micrometer.provider.StandardTags; |
| 39 | +import org.apache.cxf.metrics.micrometer.provider.StandardTagsProvider; |
| 40 | +import org.apache.cxf.metrics.micrometer.provider.jaxrs.JaxrsOperationTagsCustomizer; |
| 41 | +import org.apache.cxf.metrics.micrometer.provider.jaxrs.JaxrsTags; |
| 42 | +import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase; |
| 43 | +import org.apache.cxf.testutil.common.AbstractBusTestServerBase; |
| 44 | + |
| 45 | +import io.micrometer.core.instrument.MeterRegistry; |
| 46 | +import io.micrometer.core.instrument.Timer; |
| 47 | +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |
| 48 | + |
| 49 | +import org.junit.BeforeClass; |
| 50 | +import org.junit.Test; |
| 51 | + |
| 52 | +import static org.junit.Assert.assertEquals; |
| 53 | +import static org.junit.Assert.assertNotNull; |
| 54 | +import static org.junit.Assert.assertTrue; |
| 55 | + |
| 56 | +/** |
| 57 | + * Test for CXF-9206: cxf.server.requests metric must not be |
| 58 | + * counted twice for JAX-RS server endpoints when MetricsFeature is used. |
| 59 | + * |
| 60 | + * Root cause: the CXF-9168 fix (de39a25) made createEndpointContext() eagerly |
| 61 | + * return a MicrometerServerMetricsContext for all server-side endpoints so that |
| 62 | + * requests failing before a BindingOperationInfo is available are still counted. |
| 63 | + * For JAX-RS, createResourceContext() already returns its own MetricsContext per |
| 64 | + * request, so ExchangeMetrics ended up incrementing cxf.server.requests twice. |
| 65 | + * |
| 66 | + * The fix skips creating a server context in createEndpointContext() when the |
| 67 | + * endpoint uses the JAX-RS binding (JAXRSBindingFactory.JAXRS_BINDING_ID). |
| 68 | + */ |
| 69 | +public class JaxRsMetricsClientServerTest extends AbstractBusClientServerTestBase { |
| 70 | + |
| 71 | + private static final MeterRegistry METER_REGISTRY = new SimpleMeterRegistry(); |
| 72 | + private static final String PORT = allocatePort(JaxRsMetricsClientServerTest.class); |
| 73 | + |
| 74 | + |
| 75 | + @Path("/hello") |
| 76 | + public interface HelloService { |
| 77 | + @GET |
| 78 | + @Path("/{name}") |
| 79 | + @Produces(MediaType.TEXT_PLAIN) |
| 80 | + String sayHello(@PathParam("name") String name); |
| 81 | + } |
| 82 | + |
| 83 | + public static class HelloServiceImpl implements HelloService { |
| 84 | + @Override |
| 85 | + public String sayHello(String name) { |
| 86 | + return "Hello " + name; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + public static class Server extends AbstractBusTestServerBase { |
| 92 | + protected void run() { |
| 93 | + var jaxrsTags = new JaxrsTags(); |
| 94 | + var operationsCustomizer = new JaxrsOperationTagsCustomizer(jaxrsTags); |
| 95 | + var tagsProvider = new StandardTagsProvider( |
| 96 | + new DefaultExceptionClassProvider(), new StandardTags()); |
| 97 | + var properties = new MicrometerMetricsProperties(); |
| 98 | + |
| 99 | + var provider = new MicrometerMetricsProvider( |
| 100 | + METER_REGISTRY, |
| 101 | + tagsProvider, |
| 102 | + List.of(operationsCustomizer), |
| 103 | + new DefaultTimedAnnotationProvider(), |
| 104 | + properties |
| 105 | + ); |
| 106 | + |
| 107 | + JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean(); |
| 108 | + sf.setResourceClasses(HelloServiceImpl.class); |
| 109 | + sf.setResourceProvider(HelloServiceImpl.class, |
| 110 | + new SingletonResourceProvider(new HelloServiceImpl())); |
| 111 | + sf.setAddress("http://localhost:" + PORT + "/"); |
| 112 | + sf.setFeatures(Arrays.asList(new MetricsFeature(provider))); |
| 113 | + sf.create(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + @BeforeClass |
| 119 | + public static void startServers() throws Exception { |
| 120 | + createStaticBus(); |
| 121 | + assertTrue("server did not launch correctly", launchServer(Server.class, true)); |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + private HelloService createClient() { |
| 126 | + JAXRSClientFactoryBean factory = new JAXRSClientFactoryBean(); |
| 127 | + factory.setServiceClass(HelloService.class); |
| 128 | + factory.setAddress("http://localhost:" + PORT + "/"); |
| 129 | + factory.setHeaders(Collections.singletonMap("Accept", "text/plain")); |
| 130 | + return (HelloService) factory.create(); |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + /** |
| 135 | + * CXF-9206: a single successful JAX-RS request must produce exactly one |
| 136 | + * sample in cxf.server.requests (count == 1), not two. |
| 137 | + */ |
| 138 | + @Test |
| 139 | + public void testSuccessfulRequestIsCountedOnce() { |
| 140 | + METER_REGISTRY.clear(); |
| 141 | + |
| 142 | + String response = createClient().sayHello("world"); |
| 143 | + assertEquals("Hello world", response); |
| 144 | + |
| 145 | + Timer timer = METER_REGISTRY.find("cxf.server.requests").timer(); |
| 146 | + assertNotNull("Expected cxf.server.requests timer to be registered", timer); |
| 147 | + assertEquals( |
| 148 | + "cxf.server.requests count should be 1 for a single request", |
| 149 | + 1, |
| 150 | + timer.count() |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Two successive requests must accumulate to count == 2, not 4. |
| 156 | + */ |
| 157 | + @Test |
| 158 | + public void testTwoRequestsAreCountedTwice() { |
| 159 | + METER_REGISTRY.clear(); |
| 160 | + |
| 161 | + HelloService client = createClient(); |
| 162 | + client.sayHello("alice"); |
| 163 | + client.sayHello("bob"); |
| 164 | + |
| 165 | + Timer timer = METER_REGISTRY.find("cxf.server.requests").timer(); |
| 166 | + assertNotNull("Expected cxf.server.requests timer to be registered", timer); |
| 167 | + assertEquals( |
| 168 | + "cxf.server.requests count should be 2 for two requests", |
| 169 | + 2, |
| 170 | + timer.count() |
| 171 | + ); |
| 172 | + } |
| 173 | +} |
0 commit comments