You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/10/22 12:32:05 UTC

[camel] branch camel-3.x updated: CAMEL-19907: camel-micrometer - Add option to use old legacy naming s… (#11798)

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

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


The following commit(s) were added to refs/heads/camel-3.x by this push:
     new a80bfda9722 CAMEL-19907: camel-micrometer - Add option to use old legacy naming s… (#11798)
a80bfda9722 is described below

commit a80bfda9722752c433621324537d2ff5790321b3
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sun Oct 22 14:28:30 2023 +0200

    CAMEL-19907: camel-micrometer - Add option to use old legacy naming s… (#11798)
    
    CAMEL-19907: camel-micrometer - Add option to use old legacy naming style, that allows users that migrate to keep using old name as changing their monitoring systems can be harder.
---
 .../src/main/docs/micrometer-component.adoc        | 43 ++++++++++++++++++++
 .../component/micrometer/MicrometerUtils.java      | 16 +++++++-
 ...rometerExchangeEventNotifierNamingStrategy.java | 20 +++++++++-
 ...MicrometerRouteEventNotifierNamingStrategy.java | 29 ++++++++++++++
 .../MicrometerMessageHistoryNamingStrategy.java    | 19 +++++++++
 .../MicrometerRoutePolicyNamingStrategy.java       | 35 +++++++++++++---
 .../AbstractMicrometerRoutePolicyTest.java         |  4 ++
 .../LegacyMicrometerRoutePolicyTest.java           | 46 ++++++++++++++++++++++
 .../routepolicy/MicrometerRoutePolicyTest.java     |  6 ++-
 9 files changed, 208 insertions(+), 10 deletions(-)

diff --git a/components/camel-micrometer/src/main/docs/micrometer-component.adoc b/components/camel-micrometer/src/main/docs/micrometer-component.adoc
index 12e5fcbfbca..71ca2d39336 100644
--- a/components/camel-micrometer/src/main/docs/micrometer-component.adoc
+++ b/components/camel-micrometer/src/main/docs/micrometer-component.adoc
@@ -125,6 +125,49 @@ class MyBean extends RouteBuilder {
 }
 ----
 
+== Default Camel Metrics
+
+Some Camel specific metrics are available out of the box.
+
+[width="100%",options="header"]
+|=====================================================
+|Name |Type |Description
+|camel.message.history|timer |Sample of performance of each node in the route when message history is enabled
+|camel.routes.added |gauge |Number of routes added
+|camel.routes.running |gauge |Number of routes runnning
+|camel.exchanges.inflight |gauge |Route inflight messages
+|camel.exchanges.total |counter |Total number of processed exchanges
+|camel.exchanges.succeeded |counter |Number of successfully completed exchange
+|camel.exchanges.failed |counter |Number of failed exchanges
+|camel.exchanges.failures.handled |counter |Number of failures handled
+|camel.exchanges.external.redeliveries |counter |Number of external initiated redeliveries (such as from JMS broker)
+|camel.exchange.event.notifier |gauge + summary | Metrics for message created, sent, completed, and failed events
+|camel.route.policy |gauge + summary |Route performance metrics
+|camel.route.policy.long.task |gauge + summary |Route long task metric
+|=====================================================
+
+=== Using legacy metrics naming
+
+In Camel 3.20 or older, then the naming of metrics are using _camelCase_ style.
+However, since Camel 3.21 onwards, the naming is using micrometer convention style (see table above).
+
+To use the legacy naming, then you can use the `LEGACY` naming from the `xxxNamingStrategy` interfaces.
+
+For example:
+
+[source,java]
+----
+MicrometerRoutePolicyFactory factory = new MicrometerRoutePolicyFactory();
+factory.setNamingStrategy(MicrometerRoutePolicyNamingStrategy.LEGACY);
+----
+
+The naming style can be configured on:
+
+- `MicrometerRoutePolicyFactory`
+- `MicrometerExchangeEventNotifier`
+- `MicrometerRouteEventNotifier`
+- `MicrometerMessageHistoryFactory`
+
 == Usage of producers
 
 Each meter has type and name. Supported types are
diff --git a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/MicrometerUtils.java b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/MicrometerUtils.java
index 3a43725818b..609b1648dfb 100644
--- a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/MicrometerUtils.java
+++ b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/MicrometerUtils.java
@@ -24,6 +24,7 @@ import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
 import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.spi.Registry;
+import org.apache.camel.util.StringHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -32,7 +33,6 @@ public final class MicrometerUtils {
     private static final Logger LOG = LoggerFactory.getLogger(MicrometerUtils.class);
 
     private MicrometerUtils() {
-
     }
 
     public static Meter.Type getByName(String meterName) {
@@ -88,6 +88,20 @@ public final class MicrometerUtils {
         return new SimpleMeterRegistry();
     }
 
+    /**
+     * Converts the name to the legacy name
+     *
+     * @param  name the name
+     * @return      in legacy format (CamelCase with upper cased first letter)
+     */
+    public static String legacyName(String name) {
+        // "camel.route.policy" -> "camelRoutePolicy"
+        name = name.replace('.', '-');
+        name = StringHelper.dashToCamelCase(name);
+        // upper case first letter
+        return Character.toUpperCase(name.charAt(0)) + name.substring(1);
+    }
+
     private static MeterRegistry getMeterRegistryFromCamelRegistry(
             Registry camelRegistry, String registryName,
             Class<? extends MeterRegistry> registryType) {
diff --git a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifierNamingStrategy.java b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifierNamingStrategy.java
index c7f935a9569..fdaa1c91053 100644
--- a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifierNamingStrategy.java
+++ b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifierNamingStrategy.java
@@ -37,12 +37,30 @@ public interface MicrometerExchangeEventNotifierNamingStrategy {
 
     Predicate<Meter.Id> EVENT_NOTIFIERS
             = id -> MicrometerEventNotifierService.class.getSimpleName().equals(id.getTag(SERVICE_NAME));
+
+    /**
+     * Default naming strategy that uses micrometer naming convention.
+     */
     MicrometerExchangeEventNotifierNamingStrategy DEFAULT = (event, endpoint) -> DEFAULT_CAMEL_EXCHANGE_EVENT_METER_NAME;
 
+    /**
+     * Naming strategy that uses the classic/legacy naming style (camelCase)
+     */
+    MicrometerExchangeEventNotifierNamingStrategy LEGACY = new MicrometerExchangeEventNotifierNamingStrategy() {
+        @Override
+        public String getName(Exchange exchange, Endpoint endpoint) {
+            return formatName(DEFAULT_CAMEL_EXCHANGE_EVENT_METER_NAME);
+        }
+    };
+
     String getName(Exchange exchange, Endpoint endpoint);
 
+    default String formatName(String name) {
+        return name;
+    }
+
     default String getInflightExchangesName(Exchange exchange, Endpoint endpoint) {
-        return DEFAULT_CAMEL_ROUTES_EXCHANGES_INFLIGHT;
+        return formatName(DEFAULT_CAMEL_ROUTES_EXCHANGES_INFLIGHT);
     }
 
     default Tags getTags(ExchangeEvent event, Endpoint endpoint) {
diff --git a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerRouteEventNotifierNamingStrategy.java b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerRouteEventNotifierNamingStrategy.java
index ac1206772c1..094567ee790 100644
--- a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerRouteEventNotifierNamingStrategy.java
+++ b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerRouteEventNotifierNamingStrategy.java
@@ -21,6 +21,7 @@ import java.util.function.Predicate;
 import io.micrometer.core.instrument.Meter;
 import io.micrometer.core.instrument.Tags;
 import org.apache.camel.CamelContext;
+import org.apache.camel.component.micrometer.MicrometerUtils;
 import org.apache.camel.spi.CamelEvent.RouteEvent;
 
 import static org.apache.camel.component.micrometer.MicrometerConstants.CAMEL_CONTEXT_TAG;
@@ -33,6 +34,10 @@ public interface MicrometerRouteEventNotifierNamingStrategy {
 
     Predicate<Meter.Id> EVENT_NOTIFIERS
             = id -> MicrometerEventNotifierService.class.getSimpleName().equals(id.getTag(SERVICE_NAME));
+
+    /**
+     * Default naming strategy that uses micrometer naming convention.
+     */
     MicrometerRouteEventNotifierNamingStrategy DEFAULT = new MicrometerRouteEventNotifierNamingStrategy() {
         @Override
         public String getRouteAddedName() {
@@ -45,6 +50,30 @@ public interface MicrometerRouteEventNotifierNamingStrategy {
         }
     };
 
+    /**
+     * Naming strategy that uses the classic/legacy naming style (camelCase)
+     */
+    MicrometerRouteEventNotifierNamingStrategy LEGACY = new MicrometerRouteEventNotifierNamingStrategy() {
+        @Override
+        public String getRouteAddedName() {
+            return formatName(DEFAULT_CAMEL_ROUTES_ADDED);
+        }
+
+        @Override
+        public String getRouteRunningName() {
+            return formatName(DEFAULT_CAMEL_ROUTES_RUNNING);
+        }
+
+        @Override
+        public String formatName(String name) {
+            return MicrometerUtils.legacyName(name);
+        }
+    };
+
+    default String formatName(String name) {
+        return name;
+    }
+
     String getRouteAddedName();
 
     String getRouteRunningName();
diff --git a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/messagehistory/MicrometerMessageHistoryNamingStrategy.java b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/messagehistory/MicrometerMessageHistoryNamingStrategy.java
index 1fcda3eadb1..69fc05a1aa0 100644
--- a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/messagehistory/MicrometerMessageHistoryNamingStrategy.java
+++ b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/messagehistory/MicrometerMessageHistoryNamingStrategy.java
@@ -22,6 +22,7 @@ import io.micrometer.core.instrument.Meter;
 import io.micrometer.core.instrument.Tags;
 import org.apache.camel.NamedNode;
 import org.apache.camel.Route;
+import org.apache.camel.component.micrometer.MicrometerUtils;
 
 import static org.apache.camel.component.micrometer.MicrometerConstants.CAMEL_CONTEXT_TAG;
 import static org.apache.camel.component.micrometer.MicrometerConstants.DEFAULT_CAMEL_MESSAGE_HISTORY_METER_NAME;
@@ -36,10 +37,28 @@ public interface MicrometerMessageHistoryNamingStrategy {
 
     Predicate<Meter.Id> MESSAGE_HISTORIES
             = id -> MicrometerMessageHistoryService.class.getSimpleName().equals(id.getTag(SERVICE_NAME));
+
+    /**
+     * Default naming strategy that uses micrometer naming convention.
+     */
     MicrometerMessageHistoryNamingStrategy DEFAULT = (route, node) -> DEFAULT_CAMEL_MESSAGE_HISTORY_METER_NAME;
 
+    /**
+     * Naming strategy that uses the classic/legacy naming style (camelCase)
+     */
+    MicrometerMessageHistoryNamingStrategy LEGACY = new MicrometerMessageHistoryNamingStrategy() {
+        @Override
+        public String getName(Route route, NamedNode node) {
+            return MicrometerUtils.legacyName(DEFAULT_CAMEL_MESSAGE_HISTORY_METER_NAME);
+        }
+    };
+
     String getName(Route route, NamedNode node);
 
+    default String formatName(String name) {
+        return name;
+    }
+
     default Tags getTags(Route route, NamedNode node) {
         return Tags.of(
                 CAMEL_CONTEXT_TAG, route.getCamelContext().getName(),
diff --git a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/routepolicy/MicrometerRoutePolicyNamingStrategy.java b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/routepolicy/MicrometerRoutePolicyNamingStrategy.java
index 1b51c687965..f3e05e5e534 100644
--- a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/routepolicy/MicrometerRoutePolicyNamingStrategy.java
+++ b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/routepolicy/MicrometerRoutePolicyNamingStrategy.java
@@ -21,6 +21,7 @@ import java.util.function.Predicate;
 import io.micrometer.core.instrument.Meter;
 import io.micrometer.core.instrument.Tags;
 import org.apache.camel.Route;
+import org.apache.camel.component.micrometer.MicrometerUtils;
 
 import static org.apache.camel.component.micrometer.MicrometerConstants.*;
 
@@ -32,32 +33,54 @@ public interface MicrometerRoutePolicyNamingStrategy {
     Predicate<Meter.Id> ROUTE_POLICIES
             = id -> MicrometerRoutePolicyService.class.getSimpleName().equals(id.getTag(SERVICE_NAME));
 
+    /**
+     * Default naming strategy that uses micrometer naming convention.
+     */
     MicrometerRoutePolicyNamingStrategy DEFAULT = route -> DEFAULT_CAMEL_ROUTE_POLICY_METER_NAME;
 
+    /**
+     * Naming strategy that uses the classic/legacy naming style (camelCase)
+     */
+    MicrometerRoutePolicyNamingStrategy LEGACY = new MicrometerRoutePolicyNamingStrategy() {
+        @Override
+        public String getName(Route route) {
+            return formatName(DEFAULT_CAMEL_ROUTE_POLICY_METER_NAME);
+        }
+
+        @Override
+        public String formatName(String name) {
+            return MicrometerUtils.legacyName(name);
+        }
+    };
+
     String getName(Route route);
 
+    default String formatName(String name) {
+        return name;
+    }
+
     default String getExchangesSucceededName(Route route) {
-        return DEFAULT_CAMEL_ROUTE_POLICY_EXCHANGES_SUCCEEDED_METER_NAME;
+        return formatName(DEFAULT_CAMEL_ROUTE_POLICY_EXCHANGES_SUCCEEDED_METER_NAME);
     }
 
     default String getExchangesFailedName(Route route) {
-        return DEFAULT_CAMEL_ROUTE_POLICY_EXCHANGES_FAILED_METER_NAME;
+        return formatName(DEFAULT_CAMEL_ROUTE_POLICY_EXCHANGES_FAILED_METER_NAME);
     }
 
     default String getExchangesTotalName(Route route) {
-        return DEFAULT_CAMEL_ROUTE_POLICY_EXCHANGES_TOTAL_METER_NAME;
+        return formatName(DEFAULT_CAMEL_ROUTE_POLICY_EXCHANGES_TOTAL_METER_NAME);
     }
 
     default String getFailuresHandledName(Route route) {
-        return DEFAULT_CAMEL_ROUTE_POLICY_EXCHANGES_FAILURES_HANDLED_METER_NAME;
+        return formatName(DEFAULT_CAMEL_ROUTE_POLICY_EXCHANGES_FAILURES_HANDLED_METER_NAME);
     }
 
     default String getExternalRedeliveriesName(Route route) {
-        return DEFAULT_CAMEL_ROUTE_POLICY_EXCHANGES_EXTERNAL_REDELIVERIES_METER_NAME;
+        return formatName(DEFAULT_CAMEL_ROUTE_POLICY_EXCHANGES_EXTERNAL_REDELIVERIES_METER_NAME);
     }
 
     default String getLongTaskName(Route route) {
-        return DEFAULT_CAMEL_ROUTE_POLICY_LONGMETER_NAME;
+        return formatName(DEFAULT_CAMEL_ROUTE_POLICY_LONGMETER_NAME);
     }
 
     default Tags getTags(Route route) {
diff --git a/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/routepolicy/AbstractMicrometerRoutePolicyTest.java b/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/routepolicy/AbstractMicrometerRoutePolicyTest.java
index b2aa93976b3..feb1dd369c5 100644
--- a/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/routepolicy/AbstractMicrometerRoutePolicyTest.java
+++ b/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/routepolicy/AbstractMicrometerRoutePolicyTest.java
@@ -51,6 +51,10 @@ public abstract class AbstractMicrometerRoutePolicyTest extends CamelTestSupport
         return new MicrometerRoutePolicyFactory();
     }
 
+    protected String formatMetricName(String name) {
+        return name;
+    }
+
     @Override
     protected CamelContext createCamelContext() throws Exception {
         CamelContext context = super.createCamelContext();
diff --git a/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/routepolicy/LegacyMicrometerRoutePolicyTest.java b/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/routepolicy/LegacyMicrometerRoutePolicyTest.java
new file mode 100644
index 00000000000..f3989e47bd2
--- /dev/null
+++ b/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/routepolicy/LegacyMicrometerRoutePolicyTest.java
@@ -0,0 +1,46 @@
+/*
+ * 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.component.micrometer.routepolicy;
+
+import org.apache.camel.component.micrometer.MicrometerConstants;
+import org.apache.camel.component.micrometer.MicrometerUtils;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class LegacyMicrometerRoutePolicyTest extends MicrometerRoutePolicyTest {
+
+    @Override
+    protected MicrometerRoutePolicyFactory createMicrometerRoutePolicyFactory() {
+        MicrometerRoutePolicyFactory factory = new MicrometerRoutePolicyFactory();
+        factory.setNamingStrategy(MicrometerRoutePolicyNamingStrategy.LEGACY);
+        return factory;
+    }
+
+    @Override
+    protected String formatMetricName(String name) {
+        // use legacy name
+        return "CamelRoutePolicy";
+    }
+
+    @Test
+    public void testLegacyName() {
+        Assertions.assertEquals("CamelRoutePolicy",
+                MicrometerUtils.legacyName(MicrometerConstants.DEFAULT_CAMEL_ROUTE_POLICY_METER_NAME));
+        Assertions.assertEquals("CamelMessageHistory",
+                MicrometerUtils.legacyName(MicrometerConstants.DEFAULT_CAMEL_MESSAGE_HISTORY_METER_NAME));
+    }
+}
diff --git a/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/routepolicy/MicrometerRoutePolicyTest.java b/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/routepolicy/MicrometerRoutePolicyTest.java
index 2ad23ccb158..7096b9d4d90 100644
--- a/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/routepolicy/MicrometerRoutePolicyTest.java
+++ b/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/routepolicy/MicrometerRoutePolicyTest.java
@@ -49,13 +49,15 @@ public class MicrometerRoutePolicyTest extends AbstractMicrometerRoutePolicyTest
 
         MockEndpoint.assertIsSatisfied(context);
 
-        Timer fooTimer = meterRegistry.find(DEFAULT_CAMEL_ROUTE_POLICY_METER_NAME).tag(ROUTE_ID_TAG, "foo").timer();
+        Timer fooTimer
+                = meterRegistry.find(formatMetricName(DEFAULT_CAMEL_ROUTE_POLICY_METER_NAME)).tag(ROUTE_ID_TAG, "foo").timer();
         assertEquals(count / 2, fooTimer.count());
         assertTrue(fooTimer.mean(TimeUnit.MILLISECONDS) > DELAY_FOO);
         assertTrue(fooTimer.max(TimeUnit.MILLISECONDS) > DELAY_FOO);
         assertTrue(fooTimer.totalTime(TimeUnit.MILLISECONDS) > DELAY_FOO * count / 2);
 
-        Timer barTimer = meterRegistry.find(DEFAULT_CAMEL_ROUTE_POLICY_METER_NAME).tag(ROUTE_ID_TAG, "bar").timer();
+        Timer barTimer
+                = meterRegistry.find(formatMetricName(DEFAULT_CAMEL_ROUTE_POLICY_METER_NAME)).tag(ROUTE_ID_TAG, "bar").timer();
         assertEquals(count / 2, barTimer.count());
         assertTrue(barTimer.mean(TimeUnit.MILLISECONDS) > DELAY_BAR);
         assertTrue(barTimer.max(TimeUnit.MILLISECONDS) > DELAY_BAR);