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

davsclaus pushed a commit to branch filter-kamelet2
in repository https://gitbox.apache.org/repos/asf/camel.git

commit cf38fd28d3b5144fe71217f9e72935979a398aa0
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Sat Feb 24 15:12:21 2024 +0100

    CAMEL-18858: camel-core - Mark route as created by Kamelet so we know this, 
so we can filter out in tooling and whereelse (kamelet is a blackbox)
---
 .../eventnotifier/MicrometerRouteEventNotifier.java  | 20 ++++++++++++++++++++
 .../routepolicy/MicrometerRoutePolicy.java           | 19 ++++++++++++++++++-
 .../camel/impl/engine/AbstractCamelContext.java      | 18 ++++++++++++++----
 3 files changed, 52 insertions(+), 5 deletions(-)

diff --git 
a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerRouteEventNotifier.java
 
b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerRouteEventNotifier.java
index 6c638e031ce..04b5cb07266 100644
--- 
a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerRouteEventNotifier.java
+++ 
b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerRouteEventNotifier.java
@@ -26,6 +26,7 @@ import org.apache.camel.spi.CamelEvent.RouteReloadedEvent;
 import org.apache.camel.spi.CamelEvent.RouteRemovedEvent;
 import org.apache.camel.spi.CamelEvent.RouteStartedEvent;
 import org.apache.camel.spi.CamelEvent.RouteStoppedEvent;
+import org.apache.camel.spi.ManagementStrategy;
 
 public class MicrometerRouteEventNotifier extends 
AbstractMicrometerEventNotifier<RouteEvent> {
 
@@ -36,6 +37,8 @@ public class MicrometerRouteEventNotifier extends 
AbstractMicrometerEventNotifie
     private Gauge gaugeRunning;
     private Gauge gaugeReloaded;
     private MicrometerRouteEventNotifierNamingStrategy namingStrategy = 
MicrometerRouteEventNotifierNamingStrategy.DEFAULT;
+    boolean registerKamelets;
+    boolean registerTemplates = true;
 
     public MicrometerRouteEventNotifier() {
         super(RouteEvent.class);
@@ -49,6 +52,15 @@ public class MicrometerRouteEventNotifier extends 
AbstractMicrometerEventNotifie
         this.namingStrategy = namingStrategy;
     }
 
+    @Override
+    protected void doInit() throws Exception {
+        ManagementStrategy ms = getCamelContext().getManagementStrategy();
+        if (ms != null && ms.getManagementAgent() != null) {
+            registerKamelets = 
ms.getManagementAgent().getRegisterRoutesCreateByKamelet();
+            registerTemplates = 
ms.getManagementAgent().getRegisterRoutesCreateByTemplate();
+        }
+    }
+
     @Override
     protected void doStart() throws Exception {
         super.doStart();
@@ -84,6 +96,14 @@ public class MicrometerRouteEventNotifier extends 
AbstractMicrometerEventNotifie
 
     @Override
     public void notify(CamelEvent eventObject) {
+        if (eventObject instanceof RouteEvent re) {
+            // skip routes that should not be included
+            boolean skip = (re.getRoute().isCreatedByKamelet() && 
!registerKamelets)
+                    || (re.getRoute().isCreatedByRouteTemplate() && 
!registerTemplates);
+            if (skip) {
+                return;
+            }
+        }
         if (eventObject instanceof RouteAddedEvent) {
             routesAdded.incrementAndGet();
         } else if (eventObject instanceof RouteRemovedEvent) {
diff --git 
a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/routepolicy/MicrometerRoutePolicy.java
 
b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/routepolicy/MicrometerRoutePolicy.java
index 264cb58d6cf..74681d8797d 100644
--- 
a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/routepolicy/MicrometerRoutePolicy.java
+++ 
b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/routepolicy/MicrometerRoutePolicy.java
@@ -31,6 +31,7 @@ import org.apache.camel.NonManagedService;
 import org.apache.camel.Route;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.component.micrometer.MicrometerUtils;
+import org.apache.camel.spi.ManagementStrategy;
 import org.apache.camel.support.ExchangeHelper;
 import org.apache.camel.support.RoutePolicySupport;
 import org.apache.camel.support.service.ServiceHelper;
@@ -54,6 +55,8 @@ public class MicrometerRoutePolicy extends RoutePolicySupport 
implements NonMana
     private MicrometerRoutePolicyConfiguration configuration = 
MicrometerRoutePolicyConfiguration.DEFAULT;
 
     private final Map<Route, MetricsStatistics> statisticsMap = new 
HashMap<>();
+    boolean registerKamelets;
+    boolean registerTemplates = true;
 
     private static final class MetricsStatistics {
         private final MeterRegistry meterRegistry;
@@ -221,6 +224,12 @@ public class MicrometerRoutePolicy extends 
RoutePolicySupport implements NonMana
     public void onInit(Route route) {
         super.onInit(route);
 
+        ManagementStrategy ms = 
route.getCamelContext().getManagementStrategy();
+        if (ms != null && ms.getManagementAgent() != null) {
+            registerKamelets = 
ms.getManagementAgent().getRegisterRoutesCreateByKamelet();
+            registerTemplates = 
ms.getManagementAgent().getRegisterRoutesCreateByTemplate();
+        }
+
         if (getMeterRegistry() == null) {
             setMeterRegistry(MicrometerUtils.getOrCreateMeterRegistry(
                     route.getCamelContext().getRegistry(), 
METRICS_REGISTRY_NAME));
@@ -248,7 +257,15 @@ public class MicrometerRoutePolicy extends 
RoutePolicySupport implements NonMana
         // for now we record only all the timings of a complete exchange 
(responses)
         // we have in-flight / total statistics already from camel-core
         statisticsMap.computeIfAbsent(route,
-                it -> new MetricsStatistics(getMeterRegistry(), it, 
getNamingStrategy(), configuration));
+                it -> {
+                    // skip routes that should not be included
+                    boolean skip = (it.isCreatedByKamelet() && 
!registerKamelets)
+                            || (it.isCreatedByRouteTemplate() && 
!registerTemplates);
+                    if (skip) {
+                        return null;
+                    }
+                    return new MetricsStatistics(getMeterRegistry(), it, 
getNamingStrategy(), configuration);
+                });
     }
 
     @Override
diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 0b10e3ebe35..09a13fec427 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -2572,8 +2572,13 @@ public abstract class AbstractCamelContext extends 
BaseService
             int templates = 0;
             int rests = 0;
             int disabled = 0;
-            boolean registerKamelets = 
getManagementStrategy().getManagementAgent().getRegisterRoutesCreateByKamelet();
-            boolean registerTemplates = 
getManagementStrategy().getManagementAgent().getRegisterRoutesCreateByTemplate();
+            boolean registerKamelets = false;
+            boolean registerTemplates = true;
+            ManagementStrategy ms = getManagementStrategy();
+            if (ms != null && ms.getManagementAgent() != null) {
+                registerKamelets = 
ms.getManagementAgent().getRegisterRoutesCreateByKamelet();
+                registerTemplates = 
ms.getManagementAgent().getRegisterRoutesCreateByTemplate();
+            }
             List<String> lines = new ArrayList<>();
             List<String> configs = new ArrayList<>();
             
routeStartupOrder.sort(Comparator.comparingInt(RouteStartupOrder::getStartupOrder));
@@ -3074,8 +3079,13 @@ public abstract class AbstractCamelContext extends 
BaseService
             int kamelets = 0;
             int templates = 0;
             int rests = 0;
-            boolean registerKamelets = 
getManagementStrategy().getManagementAgent().getRegisterRoutesCreateByKamelet();
-            boolean registerTemplates = 
getManagementStrategy().getManagementAgent().getRegisterRoutesCreateByTemplate();
+            boolean registerKamelets = false;
+            boolean registerTemplates = true;
+            ManagementStrategy ms = getManagementStrategy();
+            if (ms != null && ms.getManagementAgent() != null) {
+                registerKamelets = 
ms.getManagementAgent().getRegisterRoutesCreateByKamelet();
+                registerTemplates = 
ms.getManagementAgent().getRegisterRoutesCreateByTemplate();
+            }
             List<String> lines = new ArrayList<>();
 
             final ShutdownStrategy shutdownStrategy = 
camelContextExtension.getShutdownStrategy();

Reply via email to