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

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


The following commit(s) were added to refs/heads/main by this push:
     new ca499e6e6544 CAMEL-27071: camel-micrometer / 
camel-opentelemetry-metrics - a route with both route policies fails to start 
(#30220)
ca499e6e6544 is described below

commit ca499e6e65442d58fe94faf3df1e99e5fd05f31f
Author: Serdar Gökay <[email protected]>
AuthorDate: Sun Nov 22 16:39:54 2026 +0300

    CAMEL-27071: camel-micrometer / camel-opentelemetry-metrics - a route with 
both route policies fails to start (#30220)
    
    MicrometerRoutePolicy and OpenTelemetryRoutePolicy both kept the timer of
    an exchange in the exchange property camel.route.policy-<routeId>-<id>,
    named after the meter name that the two modules share. With both policies
    on one route, each overwrote the timer of the other, and the policy that
    finished first failed with a ClassCastException, logged and ignored for
    every exchange, so three of the four route and CamelContext timers were
    lost.
    
    Each policy now names the property after itself, as the camel-metrics
    route policy does (MetricsRoutePolicy-<routeId>). The meter names do not
    change.
    
    MicrometerRoutePolicy and OpenTelemetryRoutePolicy measure the same route
    metrics, so a route can have only one of them. In onInit each policy looks
    at the route policies of its route and fails with an IllegalStateException
    when the other one is there. The check is in the policies, so it works on
    every runtime; the two modules do not depend on each other, so each policy
    knows the other by its class name.
    
    The route policy sections of both component docs say that the two cannot
    be combined, and the 4.24 upgrade guide has an entry, as a route that
    started before now fails to start.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../camel/catalog/docs/micrometer-component.adoc   |  4 ++
 .../docs/opentelemetry-metrics-component.adoc      |  3 +
 .../src/main/docs/micrometer-component.adoc        |  4 ++
 .../routepolicy/MicrometerRoutePolicy.java         | 16 ++++-
 components/camel-opentelemetry-metrics/pom.xml     |  5 ++
 .../main/docs/opentelemetry-metrics-component.adoc |  3 +
 .../routepolicy/OpenTelemetryRoutePolicy.java      | 17 ++++-
 .../OpenTelemetryAndMicrometerRoutePolicyTest.java | 82 ++++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |  9 +++
 9 files changed, 139 insertions(+), 4 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/micrometer-component.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/micrometer-component.adoc
index e699f1bdcd1e..0e04a150e170 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/micrometer-component.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/micrometer-component.adoc
@@ -747,6 +747,10 @@ dedicated `MicrometerRoutePolicy` per route you want to 
instrument, in case you
 want to instrument a few selected routes.
 ====
 
+The `MicrometerRoutePolicy` cannot be combined with the 
`OpenTelemetryRoutePolicy` of the
+xref:components::opentelemetry-metrics-component.adoc[OpenTelemetry Metrics] 
component:
+a route that has both fails to start.
+
 From Java, you add the factory to the `CamelContext` as shown below:
 
 ._Java-only: adding MicrometerRoutePolicyFactory to the CamelContext_
diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/opentelemetry-metrics-component.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/opentelemetry-metrics-component.adoc
index da424ad67647..c2d68b421d8b 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/opentelemetry-metrics-component.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/opentelemetry-metrics-component.adoc
@@ -715,6 +715,9 @@ You can define a dedicated `OpenTelemetryRoutePolicy` per 
route you want to inst
 in case you only want to instrument a few selected routes.
 ====
 
+The `OpenTelemetryRoutePolicy` cannot be combined with the 
`MicrometerRoutePolicy` of the
+xref:components::micrometer-component.adoc[Micrometer] component: a route that 
has both fails to start.
+
 Camel specific metrics that are available via the `OpenTelemetryRoutePolicy`:
 [width="100%",options="header"]
 |=====================================================
diff --git 
a/components/camel-micrometer/src/main/docs/micrometer-component.adoc 
b/components/camel-micrometer/src/main/docs/micrometer-component.adoc
index e699f1bdcd1e..0e04a150e170 100644
--- a/components/camel-micrometer/src/main/docs/micrometer-component.adoc
+++ b/components/camel-micrometer/src/main/docs/micrometer-component.adoc
@@ -747,6 +747,10 @@ dedicated `MicrometerRoutePolicy` per route you want to 
instrument, in case you
 want to instrument a few selected routes.
 ====
 
+The `MicrometerRoutePolicy` cannot be combined with the 
`OpenTelemetryRoutePolicy` of the
+xref:components::opentelemetry-metrics-component.adoc[OpenTelemetry Metrics] 
component:
+a route that has both fails to start.
+
 From Java, you add the factory to the `CamelContext` as shown below:
 
 ._Java-only: adding MicrometerRoutePolicyFactory to the CamelContext_
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 beaf0f6fa2d3..eaed190c9b93 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
@@ -41,7 +41,6 @@ import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static 
org.apache.camel.component.micrometer.MicrometerConstants.DEFAULT_CAMEL_ROUTE_POLICY_METER_NAME;
 import static org.apache.camel.component.micrometer.MicrometerConstants.KIND;
 import static 
org.apache.camel.component.micrometer.MicrometerConstants.KIND_ROUTE;
 import static 
org.apache.camel.component.micrometer.MicrometerConstants.METRICS_REGISTRY_NAME;
@@ -55,6 +54,10 @@ public class MicrometerRoutePolicy extends 
RoutePolicySupport implements NonMana
 
     private static final Logger LOG = 
LoggerFactory.getLogger(MicrometerRoutePolicy.class);
 
+    // the route policy of camel-opentelemetry-metrics, which camel-micrometer 
does not depend on
+    private static final String OPENTELEMETRY_ROUTE_POLICY
+            = 
"org.apache.camel.opentelemetry.metrics.routepolicy.OpenTelemetryRoutePolicy";
+
     private final MicrometerRoutePolicyFactory factory;
     private MeterRegistry meterRegistry;
     private boolean prettyPrint;
@@ -211,7 +214,8 @@ public class MicrometerRoutePolicy extends 
RoutePolicySupport implements NonMana
             } else {
                 id = "context:" + camelContext.getName();
             }
-            return String.format("%s-%s-%s", 
DEFAULT_CAMEL_ROUTE_POLICY_METER_NAME, id, exchange.getExchangeId());
+            // named after this policy, so another metrics route policy on the 
same route does not overwrite it
+            return "MicrometerRoutePolicy-" + id + "-" + 
exchange.getExchangeId();
         }
 
         private Counter createCounter(String meterName, String description) {
@@ -283,6 +287,14 @@ public class MicrometerRoutePolicy extends 
RoutePolicySupport implements NonMana
     public void onInit(Route route) {
         super.onInit(route);
 
+        // both route policies measure the same route metrics, so a route can 
have only one of them
+        if (route.getRoutePolicyList().stream()
+                .anyMatch(policy -> 
OPENTELEMETRY_ROUTE_POLICY.equals(policy.getClass().getName()))) {
+            throw new IllegalStateException(
+                    "Route " + route.getRouteId() + " has both 
MicrometerRoutePolicy and OpenTelemetryRoutePolicy."
+                                            + " Use only one of 
camel-micrometer or camel-opentelemetry-metrics.");
+        }
+
         ManagementStrategy ms = 
route.getCamelContext().getManagementStrategy();
         if (ms != null && ms.getManagementAgent() != null) {
             registerKamelets = 
ms.getManagementAgent().getRegisterRoutesCreateByKamelet();
diff --git a/components/camel-opentelemetry-metrics/pom.xml 
b/components/camel-opentelemetry-metrics/pom.xml
index 47b7f0d4d7f8..2652fd890ba9 100644
--- a/components/camel-opentelemetry-metrics/pom.xml
+++ b/components/camel-opentelemetry-metrics/pom.xml
@@ -70,6 +70,11 @@
             <artifactId>camel-test-spring-junit6</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-micrometer</artifactId>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.mockito</groupId>
             <artifactId>mockito-junit-jupiter</artifactId>
diff --git 
a/components/camel-opentelemetry-metrics/src/main/docs/opentelemetry-metrics-component.adoc
 
b/components/camel-opentelemetry-metrics/src/main/docs/opentelemetry-metrics-component.adoc
index da424ad67647..c2d68b421d8b 100644
--- 
a/components/camel-opentelemetry-metrics/src/main/docs/opentelemetry-metrics-component.adoc
+++ 
b/components/camel-opentelemetry-metrics/src/main/docs/opentelemetry-metrics-component.adoc
@@ -715,6 +715,9 @@ You can define a dedicated `OpenTelemetryRoutePolicy` per 
route you want to inst
 in case you only want to instrument a few selected routes.
 ====
 
+The `OpenTelemetryRoutePolicy` cannot be combined with the 
`MicrometerRoutePolicy` of the
+xref:components::micrometer-component.adoc[Micrometer] component: a route that 
has both fails to start.
+
 Camel specific metrics that are available via the `OpenTelemetryRoutePolicy`:
 [width="100%",options="header"]
 |=====================================================
diff --git 
a/components/camel-opentelemetry-metrics/src/main/java/org/apache/camel/opentelemetry/metrics/routepolicy/OpenTelemetryRoutePolicy.java
 
b/components/camel-opentelemetry-metrics/src/main/java/org/apache/camel/opentelemetry/metrics/routepolicy/OpenTelemetryRoutePolicy.java
index 886cf3fff69d..9fba3f41ad1f 100644
--- 
a/components/camel-opentelemetry-metrics/src/main/java/org/apache/camel/opentelemetry/metrics/routepolicy/OpenTelemetryRoutePolicy.java
+++ 
b/components/camel-opentelemetry-metrics/src/main/java/org/apache/camel/opentelemetry/metrics/routepolicy/OpenTelemetryRoutePolicy.java
@@ -43,7 +43,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import static 
org.apache.camel.opentelemetry.metrics.OpenTelemetryConstants.CAMEL_CONTEXT_ATTRIBUTE;
-import static 
org.apache.camel.opentelemetry.metrics.OpenTelemetryConstants.DEFAULT_CAMEL_ROUTE_POLICY_METER_NAME;
 import static 
org.apache.camel.opentelemetry.metrics.OpenTelemetryConstants.EVENT_TYPE_ATTRIBUTE;
 import static 
org.apache.camel.opentelemetry.metrics.OpenTelemetryConstants.KIND_ATTRIBUTE;
 import static 
org.apache.camel.opentelemetry.metrics.OpenTelemetryConstants.KIND_ROUTE;
@@ -57,6 +56,10 @@ public class OpenTelemetryRoutePolicy extends 
RoutePolicySupport implements NonM
 
     private static final Logger LOG = 
LoggerFactory.getLogger(OpenTelemetryRoutePolicy.class);
 
+    // the route policy of camel-micrometer, which camel-opentelemetry-metrics 
does not depend on
+    private static final String MICROMETER_ROUTE_POLICY
+            = 
"org.apache.camel.component.micrometer.routepolicy.MicrometerRoutePolicy";
+
     private final OpenTelemetryRoutePolicyFactory factory;
     private Meter meter;
     boolean registerKamelets;
@@ -125,6 +128,15 @@ public class OpenTelemetryRoutePolicy extends 
RoutePolicySupport implements NonM
     @Override
     public void onInit(Route route) {
         super.onInit(route);
+
+        // both route policies measure the same route metrics, so a route can 
have only one of them
+        if (route.getRoutePolicyList().stream()
+                .anyMatch(policy -> 
MICROMETER_ROUTE_POLICY.equals(policy.getClass().getName()))) {
+            throw new IllegalStateException(
+                    "Route " + route.getRouteId() + " has both 
OpenTelemetryRoutePolicy and MicrometerRoutePolicy."
+                                            + " Use only one of 
camel-opentelemetry-metrics or camel-micrometer.");
+        }
+
         if (meter == null) {
             this.meter = 
CamelContextHelper.findSingleByType(route.getCamelContext(), Meter.class);
         }
@@ -330,7 +342,8 @@ public class OpenTelemetryRoutePolicy extends 
RoutePolicySupport implements NonM
 
         private String propertyName(Exchange exchange) {
             String id = route != null ? route.getId() : "context:" + 
camelContext.getName();
-            return String.format("%s-%s-%s", 
DEFAULT_CAMEL_ROUTE_POLICY_METER_NAME, id, exchange.getExchangeId());
+            // named after this policy, so another metrics route policy on the 
same route does not overwrite it
+            return "OpenTelemetryRoutePolicy-" + id + "-" + 
exchange.getExchangeId();
         }
 
         private LongCounter createCounter(String meterName, String 
description) {
diff --git 
a/components/camel-opentelemetry-metrics/src/test/java/org/apache/camel/opentelemetry/metrics/routepolicy/OpenTelemetryAndMicrometerRoutePolicyTest.java
 
b/components/camel-opentelemetry-metrics/src/test/java/org/apache/camel/opentelemetry/metrics/routepolicy/OpenTelemetryAndMicrometerRoutePolicyTest.java
new file mode 100644
index 000000000000..7cee64b30cf4
--- /dev/null
+++ 
b/components/camel-opentelemetry-metrics/src/test/java/org/apache/camel/opentelemetry/metrics/routepolicy/OpenTelemetryAndMicrometerRoutePolicyTest.java
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.opentelemetry.metrics.routepolicy;
+
+import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
+import io.opentelemetry.api.OpenTelemetry;
+import org.apache.camel.CamelContext;
+import org.apache.camel.FailedToCreateRouteException;
+import org.apache.camel.builder.RouteBuilder;
+import 
org.apache.camel.component.micrometer.routepolicy.MicrometerRoutePolicyFactory;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.spi.RoutePolicyFactory;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * Verifies that a route with both the Micrometer and the OpenTelemetry route 
policy fails to start, and that the policy
+ * that comes first on the route is the one that refuses it.
+ */
+public class OpenTelemetryAndMicrometerRoutePolicyTest {
+
+    @Test
+    public void testRouteFailsToStartWithMicrometerFirst() throws Exception {
+        String message = "Route foo has both MicrometerRoutePolicy and 
OpenTelemetryRoutePolicy."
+                         + " Use only one of camel-micrometer or 
camel-opentelemetry-metrics.";
+        assertRouteFailsToStart(micrometerFactory(), openTelemetryFactory(), 
message);
+    }
+
+    @Test
+    public void testRouteFailsToStartWithOpenTelemetryFirst() throws Exception 
{
+        String message = "Route foo has both OpenTelemetryRoutePolicy and 
MicrometerRoutePolicy."
+                         + " Use only one of camel-opentelemetry-metrics or 
camel-micrometer.";
+        assertRouteFailsToStart(openTelemetryFactory(), micrometerFactory(), 
message);
+    }
+
+    private static RoutePolicyFactory micrometerFactory() {
+        MicrometerRoutePolicyFactory factory = new 
MicrometerRoutePolicyFactory();
+        factory.setMeterRegistry(new SimpleMeterRegistry());
+        return factory;
+    }
+
+    private static RoutePolicyFactory openTelemetryFactory() {
+        OpenTelemetryRoutePolicyFactory factory = new 
OpenTelemetryRoutePolicyFactory();
+        factory.setMeter(OpenTelemetry.noop().getMeter("test"));
+        return factory;
+    }
+
+    private static void assertRouteFailsToStart(RoutePolicyFactory first, 
RoutePolicyFactory second, String message)
+            throws Exception {
+        try (CamelContext context = new DefaultCamelContext()) {
+            context.addRoutePolicyFactory(first);
+            context.addRoutePolicyFactory(second);
+            context.addRoutes(new RouteBuilder() {
+                @Override
+                public void configure() {
+                    from("direct:foo").routeId("foo").to("mock:result");
+                }
+            });
+
+            FailedToCreateRouteException e = 
assertThrows(FailedToCreateRouteException.class, context::start);
+            IllegalStateException cause = 
assertInstanceOf(IllegalStateException.class, e.getCause());
+            assertEquals(message, cause.getMessage());
+        }
+    }
+}
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index cbb7a138efd6..cdfb92c13282 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -587,6 +587,15 @@ A route that relied on a Simple expression in a header tag 
or attribute should u
 option instead (for example `tags.customer=${header.customerId}`), or compute 
the value in the route before
 setting the header.
 
+=== camel-micrometer, camel-opentelemetry-metrics - a route cannot have both 
route policies
+
+A route that has both the `MicrometerRoutePolicy` and the 
`OpenTelemetryRoutePolicy` now fails to start, with an
+error that names both policies. Before, the route started, but the two 
policies kept their timers in the same exchange
+property, so most of the timings were lost and a `ClassCastException` was 
logged for every exchange. Use the route
+policy of only one of the two components. With Camel Main, 
`camel.metrics.enabled=true`, which
+camel-observability-services also sets, adds the Micrometer route policy to 
every route;
+`camel.metrics.enableRoutePolicy=false` keeps the other Micrometer metrics 
without it.
+
 === camel-elasticsearch-rest-client - the document id and the index name are 
each sent as one path segment
 
 The producer now percent-encodes the document id and the index name, so each 
is sent as one path segment of the

Reply via email to