You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2023/06/28 11:37:27 UTC

[camel-quarkus] branch main updated: Micrometer: Custom registry coverage is missing #5018

This is an automated email from the ASF dual-hosted git repository.

jamesnetherton pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/main by this push:
     new bd41c1e4ca Micrometer: Custom registry coverage is missing #5018
bd41c1e4ca is described below

commit bd41c1e4ca502906c48f9fdae41f8234021a402a
Author: JiriOndrusek <on...@gmail.com>
AuthorDate: Tue Jun 27 14:47:52 2023 +0200

    Micrometer: Custom registry coverage is missing #5018
---
 .../micrometer/it/MicrometerProducers.java         | 17 +++++-
 .../micrometer/it/MicrometerResource.java          | 60 ++++++++++++----------
 .../component/micrometer/it/MicrometerRoutes.java  |  6 +++
 .../micrometer/it/AbstractMicrometerTest.java      |  8 ++-
 .../component/micrometer/it/MicrometerTest.java    | 28 +++++++++-
 5 files changed, 89 insertions(+), 30 deletions(-)

diff --git a/integration-tests/micrometer/src/main/java/org/apache/camel/quarkus/component/micrometer/it/MicrometerProducers.java b/integration-tests/micrometer/src/main/java/org/apache/camel/quarkus/component/micrometer/it/MicrometerProducers.java
index f677005b44..c259593f32 100644
--- a/integration-tests/micrometer/src/main/java/org/apache/camel/quarkus/component/micrometer/it/MicrometerProducers.java
+++ b/integration-tests/micrometer/src/main/java/org/apache/camel/quarkus/component/micrometer/it/MicrometerProducers.java
@@ -23,13 +23,16 @@ import io.micrometer.core.instrument.Meter;
 import io.micrometer.core.instrument.MeterRegistry;
 import io.micrometer.core.instrument.Tag;
 import io.micrometer.core.instrument.config.MeterFilter;
+import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
 import io.micrometer.core.instrument.util.HierarchicalNameMapper;
 import io.micrometer.jmx.JmxMeterRegistry;
 import io.micrometer.prometheus.PrometheusMeterRegistry;
 import io.quarkus.micrometer.runtime.MeterFilterConstraint;
+import jakarta.enterprise.inject.Produces;
+import jakarta.inject.Named;
 import jakarta.inject.Singleton;
-import jakarta.ws.rs.Produces;
 import org.apache.camel.component.micrometer.CamelJmxConfig;
+import org.apache.camel.component.micrometer.MicrometerComponent;
 
 public class MicrometerProducers {
 
@@ -64,4 +67,16 @@ public class MicrometerProducers {
         };
     }
 
+    @Singleton
+    @Produces
+    @Named("micrometerCustom")
+    MicrometerComponent micrometerCustomComponent() {
+        MicrometerComponent component = new MicrometerComponent();
+
+        SimpleMeterRegistry registry = new SimpleMeterRegistry();
+        component.setMetricsRegistry(registry);
+
+        return component;
+    }
+
 }
diff --git a/integration-tests/micrometer/src/main/java/org/apache/camel/quarkus/component/micrometer/it/MicrometerResource.java b/integration-tests/micrometer/src/main/java/org/apache/camel/quarkus/component/micrometer/it/MicrometerResource.java
index 969ad98f8b..b82a5c0917 100644
--- a/integration-tests/micrometer/src/main/java/org/apache/camel/quarkus/component/micrometer/it/MicrometerResource.java
+++ b/integration-tests/micrometer/src/main/java/org/apache/camel/quarkus/component/micrometer/it/MicrometerResource.java
@@ -29,6 +29,7 @@ import io.micrometer.core.instrument.Tags;
 import io.micrometer.core.instrument.search.Search;
 import io.micrometer.prometheus.PrometheusMeterRegistry;
 import jakarta.inject.Inject;
+import jakarta.inject.Named;
 import jakarta.ws.rs.GET;
 import jakarta.ws.rs.Path;
 import jakarta.ws.rs.PathParam;
@@ -38,12 +39,9 @@ import jakarta.ws.rs.core.MediaType;
 import jakarta.ws.rs.core.Response;
 import org.apache.camel.CamelContext;
 import org.apache.camel.ProducerTemplate;
+import org.apache.camel.component.micrometer.MicrometerComponent;
 import org.apache.camel.component.micrometer.MicrometerConstants;
 import org.apache.camel.component.micrometer.eventnotifier.MicrometerEventNotifierService;
-import org.apache.camel.component.micrometer.spi.InstrumentedThreadPoolFactory;
-import org.apache.camel.impl.engine.DefaultExecutorServiceManager;
-import org.apache.camel.spi.ThreadPoolFactory;
-import org.apache.camel.support.DefaultThreadPoolFactory;
 
 @Path("/micrometer")
 public class MicrometerResource {
@@ -54,6 +52,10 @@ public class MicrometerResource {
     @Inject
     MeterRegistry meterRegistry;
 
+    @Inject
+    @Named("micrometerCustom")
+    MicrometerComponent micrometerCustomComponent;
+
     @Inject
     PrometheusMeterRegistry prometheusMeterRegistry;
 
@@ -67,10 +69,12 @@ public class MicrometerResource {
         meterRegistry.gaugeCollectionSize("example.list.size", Tags.empty(), list);
     }
 
-    @Path("/metric/{type}/{name}")
+    // registry values are: custom, standard
+    @Path("/metric/{type}/{name}/{registry}")
     @Produces(MediaType.APPLICATION_JSON)
     @GET
     public Response getMetricValue(@PathParam("type") String type, @PathParam("name") String name,
+            @PathParam("registry") String registry,
             @QueryParam("tags") String tagValues) {
         List<Tag> tags = new ArrayList<>();
         if (tagValues.length() > 0) {
@@ -81,7 +85,8 @@ public class MicrometerResource {
             }
         }
         //search only in prometheus registry (not in jmx one), counter test covers among others the unexpected behavior of prometheus
-        Search search = prometheusMeterRegistry.find(name).tags(tags);
+        Search search = ("custom".equals(registry) ? micrometerCustomComponent.getMetricsRegistry() : prometheusMeterRegistry)
+                .find(name).tags(tags);
         if (search == null) {
             return Response.status(404).build();
         }
@@ -105,24 +110,41 @@ public class MicrometerResource {
         }
     }
 
+    @Path("/counter/{inc}")
+    @GET
+    public Response counter(@PathParam("inc") int increment) {
+        return counter(increment, "counter");
+    }
+
+    @Path("/counterCustom/{inc}")
+    @GET
+    public Response counterCustom(@PathParam("inc") int increment) {
+        return counter(increment, "counterCustom");
+    }
+
+    @Path("/counterComposite/{inc}")
+    @GET
+    public Response counterComposite(@PathParam("inc") int increment) {
+        return counter(increment, "counterComposite");
+    }
+
     /**
      * If inc is > 0, MicrometerConstants.HEADER_COUNTER_INCREMENT is used
      * If inc is < 0, MicrometerConstants.HEADER_COUNTER_DECREMENT is used (with positive value)
      * If inc == 0, no header is added.
      */
-    @Path("/counter/{inc}")
-    @GET
-    public Response counter(@PathParam("inc") int increment) {
+    Response counter(int increment, String route) {
+        String path = "direct:" + route;
         if (increment > 0) {
-            producerTemplate.sendBodyAndHeader("direct:counter", null, MicrometerConstants.HEADER_COUNTER_INCREMENT, increment);
+            producerTemplate.sendBodyAndHeader(path, null, MicrometerConstants.HEADER_COUNTER_INCREMENT, increment);
         } else if (increment < 0) {
-            producerTemplate.sendBodyAndHeader("direct:counter", null, MicrometerConstants.HEADER_COUNTER_DECREMENT,
+            producerTemplate.sendBodyAndHeader(path, null, MicrometerConstants.HEADER_COUNTER_DECREMENT,
                     0 - increment);
             List l = meterRegistry.getMeters().stream().filter(m -> m.getId().getName().contains("executor"))
                     .collect(Collectors.toList());//forEach(System.out::println);
             System.out.println(l);
         } else {
-            producerTemplate.sendBody("direct:counter", null);
+            producerTemplate.sendBody(path, null);
         }
         return Response.ok().build();
     }
@@ -148,20 +170,6 @@ public class MicrometerResource {
         return Response.ok().build();
     }
 
-    @Path("/setInstrumentedThreadPoolFactory")
-    @GET
-    public Response setInstrumentedThreadPoolFactory() {
-        ThreadPoolFactory threadPoolFactory = new DefaultThreadPoolFactory();
-        InstrumentedThreadPoolFactory instrumentedThreadPoolFactory = new InstrumentedThreadPoolFactory(meterRegistry,
-                threadPoolFactory);
-
-        DefaultExecutorServiceManager executorServiceManager = new DefaultExecutorServiceManager(camelContext);
-        executorServiceManager.setThreadPoolFactory(instrumentedThreadPoolFactory);
-        camelContext.setExecutorServiceManager(executorServiceManager);
-
-        return Response.ok().build();
-    }
-
     @Path("/statistics")
     @GET
     public Response statistics() {
diff --git a/integration-tests/micrometer/src/main/java/org/apache/camel/quarkus/component/micrometer/it/MicrometerRoutes.java b/integration-tests/micrometer/src/main/java/org/apache/camel/quarkus/component/micrometer/it/MicrometerRoutes.java
index 8e8517e587..5e4bba44b4 100644
--- a/integration-tests/micrometer/src/main/java/org/apache/camel/quarkus/component/micrometer/it/MicrometerRoutes.java
+++ b/integration-tests/micrometer/src/main/java/org/apache/camel/quarkus/component/micrometer/it/MicrometerRoutes.java
@@ -27,6 +27,12 @@ public class MicrometerRoutes extends RouteBuilder {
         from("direct:counter")
                 .to("micrometer:counter:camel-quarkus-counter");
 
+        from("direct:counterComposite")
+                .to("micrometer:counter:camel-quarkus-custom-counter");
+
+        from("direct:counterCustom")
+                .to("micrometerCustom:counter:camel-quarkus-custom-counter");
+
         from("direct:summary")
                 .setHeader(HEADER_HISTOGRAM_VALUE, simple("${body}"))
                 .to("micrometer:summary:camel-quarkus-summary");
diff --git a/integration-tests/micrometer/src/test/java/org/apache/camel/quarkus/component/micrometer/it/AbstractMicrometerTest.java b/integration-tests/micrometer/src/test/java/org/apache/camel/quarkus/component/micrometer/it/AbstractMicrometerTest.java
index 84606884e0..28d77a46a8 100644
--- a/integration-tests/micrometer/src/test/java/org/apache/camel/quarkus/component/micrometer/it/AbstractMicrometerTest.java
+++ b/integration-tests/micrometer/src/test/java/org/apache/camel/quarkus/component/micrometer/it/AbstractMicrometerTest.java
@@ -32,10 +32,16 @@ abstract class AbstractMicrometerTest {
     }
 
     <T> T getMetricValue(Class<T> as, String type, String name, String tags, int statusCode) {
+        return getMetricValue(as, type, name, tags, statusCode, null);
+    }
+
+    <T> T getMetricValue(Class<T> as, String type, String name, String tags, int statusCode, String registry) {
+        String r = (registry == null ? "standard" : registry);
+
         ResponseBodyExtractionOptions resp = RestAssured.given()
                 .queryParam("tags", tags)
                 .when()
-                .get("/micrometer/metric/" + type + "/" + name)
+                .get("/micrometer/metric/" + type + "/" + name + "/" + r)
                 .then()
                 .statusCode(statusCode)
                 .extract()
diff --git a/integration-tests/micrometer/src/test/java/org/apache/camel/quarkus/component/micrometer/it/MicrometerTest.java b/integration-tests/micrometer/src/test/java/org/apache/camel/quarkus/component/micrometer/it/MicrometerTest.java
index 36b4b832bf..5589f43a4a 100644
--- a/integration-tests/micrometer/src/test/java/org/apache/camel/quarkus/component/micrometer/it/MicrometerTest.java
+++ b/integration-tests/micrometer/src/test/java/org/apache/camel/quarkus/component/micrometer/it/MicrometerTest.java
@@ -59,6 +59,30 @@ class MicrometerTest extends AbstractMicrometerTest {
         assertEquals(6, getMetricValue(Integer.class, "counter", "camel-quarkus-counter"));
     }
 
+    @Test
+    public void testMicrometerCustomMetrics() throws Exception {
+        //add 10 to custom component
+        RestAssured.get("/micrometer/counterCustom/10")
+                .then()
+                .statusCode(200);
+
+        //increment via standard component should not modify custom registry
+        RestAssured.get("/micrometer/counterComposite/1")
+                .then()
+                .statusCode(200);
+        //custom registry starts with value 10 so addition of not-custom component should not affect it
+        assertEquals(10, getMetricValue(Integer.class, "counter", "camel-quarkus-custom-counter", null, 200, "custom"));
+
+        //add 1 to custom component
+        RestAssured.get("/micrometer/counterCustom/1")
+                .then()
+                .statusCode(200);
+        //component with Quarkus's registry should still see only 1 from the first call
+        assertEquals(1, getMetricValue(Integer.class, "counter", "camel-quarkus-custom-counter", null, 200, null));
+        //component with custom registry should be on 11 see only 1 from the first call
+        assertEquals(11, getMetricValue(Integer.class, "counter", "camel-quarkus-custom-counter", null, 200, "custom"));
+    }
+
     @Test
     public void testMicrometerSummary() {
         RestAssured.get("/micrometer/summary?value=10")
@@ -167,9 +191,9 @@ class MicrometerTest extends AbstractMicrometerTest {
 
         assertEquals(result.size(), 2);
         assertTrue(result.containsKey("camel.routes.running"));
-        assertEquals(result.get("camel.routes.running"), "5.0");
+        assertEquals(result.get("camel.routes.running"), "7.0");
         assertTrue(result.containsKey("camel.routes.added"));
-        assertEquals(result.get("camel.routes.added"), "5.0");
+        assertEquals(result.get("camel.routes.added"), "7.0");
     }
 
     @Test