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 bf7256c22644 CAMEL-28007: camel-micrometer - logMetricsOnShutdown and
unknown binders (#31258)
bf7256c22644 is described below
commit bf7256c22644430e13ad54f04e8b5fe5f594d8ca
Author: Guillaume Nodet <[email protected]>
AuthorDate: Wed Dec 2 21:37:32 2026 +0100
CAMEL-28007: camel-micrometer - logMetricsOnShutdown and unknown binders
(#31258)
Bug 1: logMetricsOnShutdown was silently ignored when
enableExchangeEventNotifier=false.
The logMetricsOnShutdown settings were wired exclusively into
MicrometerExchangeEventNotifier
inside the enableExchangeEventNotifier guard. When that flag is false, no
notifier is registered,
so MicrometerEventNotifierService is never created and shutdown logging
never fires.
Fix: when enableExchangeEventNotifier=false but logMetricsOnShutdown=true,
directly create and
register a MicrometerEventNotifierService to handle shutdown logging.
Bug 2: unrecognized binder names in BindersHelper.discoverBinders() were
silently skipped with
no diagnostic. Fix: log a WARN for each unrecognized binder name so users
can detect typos.
Follow-up commits:
- CAMEL-28007: address review: javadoc, metadata regen, blank binder skip,
pom cleanup, parity tags
Co-authored-by: Claude Sonnet 4.6 <[email protected]>
---
.../org/apache/camel/catalog/docs/main.adoc | 4 +-
.../main/camel-main-configuration-metadata.json | 4 +-
components/camel-micrometer-prometheus/pom.xml | 17 +++
.../micrometer/prometheus/BindersHelper.java | 9 +-
.../prometheus/MicrometerPrometheus.java | 22 ++++
.../micrometer/prometheus/BindersHelperTest.java | 89 +++++++++++++++
...crometerPrometheusLogMetricsOnShutdownTest.java | 125 +++++++++++++++++++++
.../camel-main-configuration-metadata.json | 4 +-
core/camel-main/src/main/docs/main.adoc | 4 +-
.../camel/main/MetricsConfigurationProperties.java | 10 +-
10 files changed, 275 insertions(+), 13 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/main.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/main.adoc
index a9fe8bf2357b..ffbd844071c3 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/main.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/main.adoc
@@ -704,7 +704,7 @@ The camel.metrics supports 18 options, which are listed
below.
|===
| Name | Description | Default | Type
| `camel.metrics.baseEndpointURIExchangeEventNotifier` | Whether the
endpointName tag of the exchange event metrics holds the endpoint URI without
its query parameters (true) or the full URI (false). The full URI can create
many tags, for example with a dynamic to (toD), which computes a URI for each
message. | true | boolean
-| `camel.metrics.binders` | Micrometer binders to add, separated by commas,
named after the binder class in dash case without the Metrics suffix:
class-loader, commons-object-pool2, file-descriptor, jvm-compilation, jvm-gc,
jvm-heap-pressure, jvm-info, jvm-memory, jvm-thread, jvm-thread-deadlock,
log4j2, logback, processor and uptime. A name that matches no binder is
ignored. | | String
+| `camel.metrics.binders` | Micrometer binders to add, separated by commas,
named after the binder class in dash case without the Metrics suffix:
class-loader, commons-object-pool2, file-descriptor, jvm-compilation, jvm-gc,
jvm-heap-pressure, jvm-info, jvm-memory, jvm-thread, jvm-thread-deadlock,
log4j2, logback, processor and uptime. A name that matches no binder is logged
as a warning and ignored. | | String
| `camel.metrics.clearOnReload` | Whether to remove the metrics of the routes
(the Camel metrics with a routeId tag, except the camel.exchanges ones) when
routes are reloaded, such as when the Camel CLI reloads changed routes. | true
| boolean
| `camel.metrics.enabled` | Whether to enable Micrometer metrics, with
camel-micrometer-prometheus, which must be on the classpath. Setting any other
camel.metrics option also enables it, unless this option is false. | false |
boolean
| `camel.metrics.enableExchangeEventNotifier` | Whether to capture metrics of
the exchange events, with the MicrometerExchangeEventNotifier: the time taken
to process an exchange and to send it to each endpoint, the inflight exchanges
of each route and the timestamp of the last exchange. | true | boolean
@@ -712,7 +712,7 @@ The camel.metrics supports 18 options, which are listed
below.
| `camel.metrics.enableMessageHistory` | Whether to capture the processing
time of each node of the routes, with the MicrometerMessageHistoryFactory,
which also turns on message history. With many routes and nodes this creates
many metrics, so it is off by default. | false | boolean
| `camel.metrics.enableRouteEventNotifier` | Whether to capture the number of
routes added, running and reloaded, with the MicrometerRouteEventNotifier. |
true | boolean
| `camel.metrics.enableRoutePolicy` | Whether to capture the processing times
and the exchange counts of the routes and the CamelContext, with the
MicrometerRoutePolicyFactory. routePolicyLevel selects which of them. | true |
boolean
-| `camel.metrics.logMetricsOnShutdown` | Whether to log the metrics when the
application shuts down. Only when enableExchangeEventNotifier is true. | false
| boolean
+| `camel.metrics.logMetricsOnShutdown` | Whether to log the metrics when the
application shuts down. This option works independently of
enableExchangeEventNotifier: the shutdown dump is produced even when the
exchange event notifier is disabled. | false | boolean
| `camel.metrics.logMetricsOnShutdownFilters` | The metrics to log on
shutdown, separated by commas; all metrics when not set. A filter with a
wildcard, such as camel.exchanges.*, matches every metric whose name contains
the filter without the wildcard; other filters must equal the name. | | String
| `camel.metrics.logMetricsOnShutdownFormat` | Format used to log metrics when
application is shutting down. Either json (default) or prometheus format. |
json | String
| `camel.metrics.namingStrategy` | The naming style of the metrics: default
uses the Micrometer naming convention (such as camel.exchanges.total), legacy
uses the older names in camel case (such as CamelExchangesTotal). | default |
String
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/main/camel-main-configuration-metadata.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/main/camel-main-configuration-metadata.json
index 0db9a6f15fd5..41e9de4da258 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/main/camel-main-configuration-metadata.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/main/camel-main-configuration-metadata.json
@@ -344,7 +344,7 @@
{ "name": "camel.mdc.customExchangeProperties", "required": false,
"description": "The exchange properties to also put in the MDC, with the
property name as key, separated by commas. A name may contain * as a wildcard,
and * alone adds all properties.", "sourceType":
"org.apache.camel.main.MdcConfigurationProperties", "type": "string",
"javaType": "java.lang.String", "secret": false },
{ "name": "camel.mdc.enabled", "required": false, "description": "Whether
to enable the MDC service of camel-mdc, which puts the exchange id, message id,
route id, CamelContext name, thread name, correlation id and breadcrumb id in
the logging MDC (Mapped Diagnostic Context) while an exchange is routed.
Setting any other camel.mdc option also enables it, unless this option is
false.", "sourceType": "org.apache.camel.main.MdcConfigurationProperties",
"type": "boolean", "javaType": "bo [...]
{ "name": "camel.metrics.baseEndpointURIExchangeEventNotifier",
"required": false, "description": "Whether the endpointName tag of the exchange
event metrics holds the endpoint URI without its query parameters (true) or the
full URI (false). The full URI can create many tags, for example with a dynamic
to (toD), which computes a URI for each message.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "boolean",
"javaType": "boolean", "defaultValue": true, [...]
- { "name": "camel.metrics.binders", "required": false, "description":
"Micrometer binders to add, separated by commas, named after the binder class
in dash case without the Metrics suffix: class-loader, commons-object-pool2,
file-descriptor, jvm-compilation, jvm-gc, jvm-heap-pressure, jvm-info,
jvm-memory, jvm-thread, jvm-thread-deadlock, log4j2, logback, processor and
uptime. A name that matches no binder is ignored.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperti [...]
+ { "name": "camel.metrics.binders", "required": false, "description":
"Micrometer binders to add, separated by commas, named after the binder class
in dash case without the Metrics suffix: class-loader, commons-object-pool2,
file-descriptor, jvm-compilation, jvm-gc, jvm-heap-pressure, jvm-info,
jvm-memory, jvm-thread, jvm-thread-deadlock, log4j2, logback, processor and
uptime. A name that matches no binder is logged as a warning and ignored.",
"sourceType": "org.apache.camel.main.Metr [...]
{ "name": "camel.metrics.clearOnReload", "required": false, "description":
"Whether to remove the metrics of the routes (the Camel metrics with a routeId
tag, except the camel.exchanges ones) when routes are reloaded, such as when
the Camel CLI reloads changed routes.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "boolean",
"javaType": "boolean", "defaultValue": true, "secret": false },
{ "name": "camel.metrics.enabled", "required": false, "description":
"Whether to enable Micrometer metrics, with camel-micrometer-prometheus, which
must be on the classpath. Setting any other camel.metrics option also enables
it, unless this option is false.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "boolean",
"javaType": "boolean", "defaultValue": false, "secret": false },
{ "name": "camel.metrics.enableExchangeEventNotifier", "required": false,
"description": "Whether to capture metrics of the exchange events, with the
MicrometerExchangeEventNotifier: the time taken to process an exchange and to
send it to each endpoint, the inflight exchanges of each route and the
timestamp of the last exchange.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "boolean",
"javaType": "boolean", "defaultValue": true, "secret": false },
@@ -352,7 +352,7 @@
{ "name": "camel.metrics.enableMessageHistory", "required": false,
"description": "Whether to capture the processing time of each node of the
routes, with the MicrometerMessageHistoryFactory, which also turns on message
history. With many routes and nodes this creates many metrics, so it is off by
default.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "boolean",
"javaType": "boolean", "defaultValue": false, "secret": false },
{ "name": "camel.metrics.enableRouteEventNotifier", "required": false,
"description": "Whether to capture the number of routes added, running and
reloaded, with the MicrometerRouteEventNotifier.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "boolean",
"javaType": "boolean", "defaultValue": true, "secret": false },
{ "name": "camel.metrics.enableRoutePolicy", "required": false,
"description": "Whether to capture the processing times and the exchange counts
of the routes and the CamelContext, with the MicrometerRoutePolicyFactory.
routePolicyLevel selects which of them.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "boolean",
"javaType": "boolean", "defaultValue": true, "secret": false },
- { "name": "camel.metrics.logMetricsOnShutdown", "required": false,
"description": "Whether to log the metrics when the application shuts down.
Only when enableExchangeEventNotifier is true.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "boolean",
"javaType": "boolean", "defaultValue": false, "secret": false },
+ { "name": "camel.metrics.logMetricsOnShutdown", "required": false,
"description": "Whether to log the metrics when the application shuts down.
This option works independently of enableExchangeEventNotifier: the shutdown
dump is produced even when the exchange event notifier is disabled.",
"sourceType": "org.apache.camel.main.MetricsConfigurationProperties", "type":
"boolean", "javaType": "boolean", "defaultValue": false, "secret": false },
{ "name": "camel.metrics.logMetricsOnShutdownFilters", "required": false,
"description": "The metrics to log on shutdown, separated by commas; all
metrics when not set. A filter with a wildcard, such as camel.exchanges.*,
matches every metric whose name contains the filter without the wildcard; other
filters must equal the name.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "string",
"javaType": "java.lang.String", "secret": false },
{ "name": "camel.metrics.logMetricsOnShutdownFormat", "required": false,
"description": "Format used to log metrics when application is shutting down.
Either json (default) or prometheus format.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "enum",
"javaType": "java.lang.String", "defaultValue": "json", "secret": false,
"enum": [ "json", "prometheus" ] },
{ "name": "camel.metrics.namingStrategy", "required": false,
"description": "The naming style of the metrics: default uses the Micrometer
naming convention (such as camel.exchanges.total), legacy uses the older names
in camel case (such as CamelExchangesTotal).", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "enum",
"javaType": "java.lang.String", "defaultValue": "default", "secret": false,
"enum": [ "default", "legacy" ] },
diff --git a/components/camel-micrometer-prometheus/pom.xml
b/components/camel-micrometer-prometheus/pom.xml
index f2fb3642e07f..0cf10d7c5e1e 100644
--- a/components/camel-micrometer-prometheus/pom.xml
+++ b/components/camel-micrometer-prometheus/pom.xml
@@ -61,6 +61,23 @@
<artifactId>jandex</artifactId>
<version>${jandex-version}</version>
</dependency>
+
+ <!-- test -->
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-test-junit6</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
diff --git
a/components/camel-micrometer-prometheus/src/main/java/org/apache/camel/component/micrometer/prometheus/BindersHelper.java
b/components/camel-micrometer-prometheus/src/main/java/org/apache/camel/component/micrometer/prometheus/BindersHelper.java
index 321db50f1787..cbec47d5f756 100644
---
a/components/camel-micrometer-prometheus/src/main/java/org/apache/camel/component/micrometer/prometheus/BindersHelper.java
+++
b/components/camel-micrometer-prometheus/src/main/java/org/apache/camel/component/micrometer/prometheus/BindersHelper.java
@@ -55,7 +55,11 @@ public final class BindersHelper {
LOG.debug("Found {} MeterBinder classes from {}", classes.size(),
JANDEX_INDEX);
for (String binder : names.split(",")) {
- binder = binder.trim();
+ String originalName = binder.trim();
+ if (originalName.isEmpty()) {
+ continue;
+ }
+ binder = originalName;
binder = StringHelper.dashToCamelCase(binder);
binder = binder.toLowerCase();
@@ -69,6 +73,9 @@ public final class BindersHelper {
if (found.isPresent()) {
String fqn = found.get().name().toString();
answer.add(fqn);
+ } else {
+ LOG.warn("Unknown MeterBinder name: {}. Skipping. Check
the binders configuration for valid names.",
+ originalName);
}
}
}
diff --git
a/components/camel-micrometer-prometheus/src/main/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheus.java
b/components/camel-micrometer-prometheus/src/main/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheus.java
index 0804f67b057a..b2a15dfb9f4a 100644
---
a/components/camel-micrometer-prometheus/src/main/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheus.java
+++
b/components/camel-micrometer-prometheus/src/main/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheus.java
@@ -23,6 +23,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringJoiner;
+import java.util.concurrent.TimeUnit;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
@@ -38,6 +39,7 @@ import org.apache.camel.StaticService;
import org.apache.camel.api.management.ManagedResource;
import org.apache.camel.component.micrometer.MicrometerConstants;
import org.apache.camel.component.micrometer.MicrometerUtils;
+import
org.apache.camel.component.micrometer.eventnotifier.MicrometerEventNotifierService;
import
org.apache.camel.component.micrometer.eventnotifier.MicrometerExchangeEventNotifier;
import
org.apache.camel.component.micrometer.eventnotifier.MicrometerExchangeEventNotifierNamingStrategyDefault;
import
org.apache.camel.component.micrometer.eventnotifier.MicrometerExchangeEventNotifierNamingStrategyLegacy;
@@ -404,6 +406,26 @@ public class MicrometerPrometheus extends ServiceSupport
implements CamelMetrics
}
notifier.setMeterRegistry(meterRegistry);
managementStrategy.addEventNotifier(notifier);
+ } else if (isLogMetricsOnShutdown()) {
+ // enableExchangeEventNotifier=false means no
MicrometerExchangeEventNotifier is registered,
+ // so we must create a MicrometerEventNotifierService directly to
handle logMetricsOnShutdown.
+ MicrometerEventNotifierService registryService =
camelContext.hasService(MicrometerEventNotifierService.class);
+ if (registryService == null) {
+ registryService = new MicrometerEventNotifierService();
+ registryService.setMeterRegistry(meterRegistry);
+ registryService.setSkipCamelInfo(isSkipCamelInfo());
+ registryService.setLogMetricsOnShutdown(true);
+
registryService.setLogMetricsOnShutdownFormat(getLogMetricsOnShutdownFormat());
+ registryService.setDurationUnit(TimeUnit.MILLISECONDS);
+ registryService.setMatchingTags(
+ io.micrometer.core.instrument.Tags.of(
+ MicrometerConstants.KIND,
MicrometerConstants.KIND_EXCHANGE));
+ if (getLogMetricsOnShutdownFilters() != null) {
+
registryService.setLogMetricsOnShutdownFilters(getLogMetricsOnShutdownFilters().split(","));
+ }
+ registryService.setCamelContext(camelContext);
+ camelContext.addService(registryService);
+ }
}
if (isEnableRouteEventNotifier()) {
diff --git
a/components/camel-micrometer-prometheus/src/test/java/org/apache/camel/component/micrometer/prometheus/BindersHelperTest.java
b/components/camel-micrometer-prometheus/src/test/java/org/apache/camel/component/micrometer/prometheus/BindersHelperTest.java
new file mode 100644
index 000000000000..cf38a6a5618c
--- /dev/null
+++
b/components/camel-micrometer-prometheus/src/test/java/org/apache/camel/component/micrometer/prometheus/BindersHelperTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.prometheus;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.spi.ClassResolver;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class BindersHelperTest {
+
+ private DefaultCamelContext camelContext;
+ private ClassResolver classResolver;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ camelContext = new DefaultCamelContext();
+ camelContext.disableJMX();
+ classResolver = camelContext.getClassResolver();
+ }
+
+ @AfterEach
+ void tearDown() throws Exception {
+ camelContext.stop();
+ }
+
+ /**
+ * CAMEL-28007 bug 2: unknown binder names must produce a warning
(verified here by asserting they are absent from
+ * the returned list) and not silently produce an empty result without any
diagnostic.
+ * <p>
+ * The warning itself is emitted to the logger; this test validates the
returned list contract — an unknown name
+ * must NOT appear in the result — and that recognised names (e.g.
"jvm-memory") ARE resolved.
+ */
+ @Test
+ void unknownBinderNameIsExcludedFromResult() throws IOException {
+ // "does-not-exist" is not a valid binder name; it must be excluded
from the result
+ List<String> result = BindersHelper.discoverBinders(classResolver,
"jvm-memory,does-not-exist");
+
+ // known binder resolves to a fully-qualified class name
+ assertThat(result).anyMatch(fqn -> fqn.contains("JvmMemoryMetrics"));
+ // The result must have exactly 1 entry — the unknown name must not
appear
+ assertThat(result).hasSize(1);
+ }
+
+ @Test
+ void blankEntriesAreSkipped() throws IOException {
+ // empty entry produced by "a,,b" split must not cause a warning or
appear in results
+ List<String> result = BindersHelper.discoverBinders(classResolver,
"jvm-memory,,processor");
+
+ assertThat(result).hasSize(2);
+ assertThat(result).anyMatch(fqn -> fqn.contains("JvmMemoryMetrics"));
+ assertThat(result).anyMatch(fqn -> fqn.contains("ProcessorMetrics"));
+ }
+
+ @Test
+ void allUnknownBindersYieldsEmptyList() throws IOException {
+ List<String> result = BindersHelper.discoverBinders(classResolver,
"no-such-binder,another-fake");
+
+ assertThat(result).isEmpty();
+ }
+
+ @Test
+ void knownBinderIsResolved() throws IOException {
+ List<String> result = BindersHelper.discoverBinders(classResolver,
"processor");
+
+ assertThat(result).hasSize(1);
+ assertThat(result.get(0)).contains("ProcessorMetrics");
+ }
+}
diff --git
a/components/camel-micrometer-prometheus/src/test/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheusLogMetricsOnShutdownTest.java
b/components/camel-micrometer-prometheus/src/test/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheusLogMetricsOnShutdownTest.java
new file mode 100644
index 000000000000..ae6e741814a5
--- /dev/null
+++
b/components/camel-micrometer-prometheus/src/test/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheusLogMetricsOnShutdownTest.java
@@ -0,0 +1,125 @@
+/*
+ * 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.prometheus;
+
+import
org.apache.camel.component.micrometer.eventnotifier.MicrometerEventNotifierService;
+import
org.apache.camel.component.micrometer.eventnotifier.MicrometerExchangeEventNotifier;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.spi.ManagementStrategy;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * CAMEL-28007: logMetricsOnShutdown must work regardless of the
enableExchangeEventNotifier setting.
+ */
+class MicrometerPrometheusLogMetricsOnShutdownTest {
+
+ private DefaultCamelContext camelContext;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ camelContext = new DefaultCamelContext();
+ camelContext.disableJMX();
+ }
+
+ @AfterEach
+ void tearDown() throws Exception {
+ camelContext.stop();
+ }
+
+ /**
+ * When enableExchangeEventNotifier=true (default), the existing path via
MicrometerExchangeEventNotifier still
+ * works: a MicrometerExchangeEventNotifier must be registered.
+ */
+ @Test
+ void logMetricsOnShutdownWithExchangeEventNotifierEnabled() throws
Exception {
+ MicrometerPrometheus mp = new MicrometerPrometheus();
+ mp.setCamelContext(camelContext);
+ mp.setEnableExchangeEventNotifier(true);
+ mp.setLogMetricsOnShutdown(true);
+ mp.setEnableRoutePolicy(false);
+ mp.setEnableRouteEventNotifier(false);
+
+ mp.doInit();
+
+ ManagementStrategy ms = camelContext.getManagementStrategy();
+ boolean hasExchangeNotifier = ms.getEventNotifiers().stream()
+ .anyMatch(n -> n instanceof MicrometerExchangeEventNotifier);
+ assertThat(hasExchangeNotifier)
+ .as("MicrometerExchangeEventNotifier must be registered when
enableExchangeEventNotifier=true")
+ .isTrue();
+ }
+
+ /**
+ * CAMEL-28007 bug 1: when enableExchangeEventNotifier=false,
logMetricsOnShutdown was silently ignored because the
+ * MicrometerEventNotifierService was never created. The fix must register
a MicrometerEventNotifierService directly
+ * so shutdown logging still fires.
+ */
+ @Test
+ void logMetricsOnShutdownWithExchangeEventNotifierDisabled() throws
Exception {
+ MicrometerPrometheus mp = new MicrometerPrometheus();
+ mp.setCamelContext(camelContext);
+ mp.setEnableExchangeEventNotifier(false);
+ mp.setLogMetricsOnShutdown(true);
+ mp.setEnableRoutePolicy(false);
+ mp.setEnableRouteEventNotifier(false);
+
+ mp.doInit();
+
+ // No MicrometerExchangeEventNotifier should be registered
+ ManagementStrategy ms = camelContext.getManagementStrategy();
+ boolean hasExchangeNotifier = ms.getEventNotifiers().stream()
+ .anyMatch(n -> n instanceof MicrometerExchangeEventNotifier);
+ assertThat(hasExchangeNotifier)
+ .as("MicrometerExchangeEventNotifier must NOT be registered
when enableExchangeEventNotifier=false")
+ .isFalse();
+
+ // But a MicrometerEventNotifierService must be registered for
shutdown logging
+ MicrometerEventNotifierService service =
camelContext.hasService(MicrometerEventNotifierService.class);
+ assertThat(service)
+ .as("MicrometerEventNotifierService must be registered when
logMetricsOnShutdown=true even if "
+ + "enableExchangeEventNotifier=false (CAMEL-28007)")
+ .isNotNull();
+ assertThat(service.isLogMetricsOnShutdown())
+ .as("MicrometerEventNotifierService must have
logMetricsOnShutdown=true")
+ .isTrue();
+ }
+
+ /**
+ * When both enableExchangeEventNotifier=false and
logMetricsOnShutdown=false, no MicrometerEventNotifierService
+ * should be created (no overhead).
+ */
+ @Test
+ void noServiceRegisteredWhenBothDisabled() throws Exception {
+ MicrometerPrometheus mp = new MicrometerPrometheus();
+ mp.setCamelContext(camelContext);
+ mp.setEnableExchangeEventNotifier(false);
+ mp.setLogMetricsOnShutdown(false);
+ mp.setEnableRoutePolicy(false);
+ mp.setEnableRouteEventNotifier(false);
+
+ mp.doInit();
+
+ MicrometerEventNotifierService service =
camelContext.hasService(MicrometerEventNotifierService.class);
+ assertThat(service)
+ .as("No MicrometerEventNotifierService should be created when
both flags are false")
+ .isNull();
+ }
+}
diff --git
a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
index 0db9a6f15fd5..41e9de4da258 100644
---
a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
+++
b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
@@ -344,7 +344,7 @@
{ "name": "camel.mdc.customExchangeProperties", "required": false,
"description": "The exchange properties to also put in the MDC, with the
property name as key, separated by commas. A name may contain * as a wildcard,
and * alone adds all properties.", "sourceType":
"org.apache.camel.main.MdcConfigurationProperties", "type": "string",
"javaType": "java.lang.String", "secret": false },
{ "name": "camel.mdc.enabled", "required": false, "description": "Whether
to enable the MDC service of camel-mdc, which puts the exchange id, message id,
route id, CamelContext name, thread name, correlation id and breadcrumb id in
the logging MDC (Mapped Diagnostic Context) while an exchange is routed.
Setting any other camel.mdc option also enables it, unless this option is
false.", "sourceType": "org.apache.camel.main.MdcConfigurationProperties",
"type": "boolean", "javaType": "bo [...]
{ "name": "camel.metrics.baseEndpointURIExchangeEventNotifier",
"required": false, "description": "Whether the endpointName tag of the exchange
event metrics holds the endpoint URI without its query parameters (true) or the
full URI (false). The full URI can create many tags, for example with a dynamic
to (toD), which computes a URI for each message.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "boolean",
"javaType": "boolean", "defaultValue": true, [...]
- { "name": "camel.metrics.binders", "required": false, "description":
"Micrometer binders to add, separated by commas, named after the binder class
in dash case without the Metrics suffix: class-loader, commons-object-pool2,
file-descriptor, jvm-compilation, jvm-gc, jvm-heap-pressure, jvm-info,
jvm-memory, jvm-thread, jvm-thread-deadlock, log4j2, logback, processor and
uptime. A name that matches no binder is ignored.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperti [...]
+ { "name": "camel.metrics.binders", "required": false, "description":
"Micrometer binders to add, separated by commas, named after the binder class
in dash case without the Metrics suffix: class-loader, commons-object-pool2,
file-descriptor, jvm-compilation, jvm-gc, jvm-heap-pressure, jvm-info,
jvm-memory, jvm-thread, jvm-thread-deadlock, log4j2, logback, processor and
uptime. A name that matches no binder is logged as a warning and ignored.",
"sourceType": "org.apache.camel.main.Metr [...]
{ "name": "camel.metrics.clearOnReload", "required": false, "description":
"Whether to remove the metrics of the routes (the Camel metrics with a routeId
tag, except the camel.exchanges ones) when routes are reloaded, such as when
the Camel CLI reloads changed routes.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "boolean",
"javaType": "boolean", "defaultValue": true, "secret": false },
{ "name": "camel.metrics.enabled", "required": false, "description":
"Whether to enable Micrometer metrics, with camel-micrometer-prometheus, which
must be on the classpath. Setting any other camel.metrics option also enables
it, unless this option is false.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "boolean",
"javaType": "boolean", "defaultValue": false, "secret": false },
{ "name": "camel.metrics.enableExchangeEventNotifier", "required": false,
"description": "Whether to capture metrics of the exchange events, with the
MicrometerExchangeEventNotifier: the time taken to process an exchange and to
send it to each endpoint, the inflight exchanges of each route and the
timestamp of the last exchange.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "boolean",
"javaType": "boolean", "defaultValue": true, "secret": false },
@@ -352,7 +352,7 @@
{ "name": "camel.metrics.enableMessageHistory", "required": false,
"description": "Whether to capture the processing time of each node of the
routes, with the MicrometerMessageHistoryFactory, which also turns on message
history. With many routes and nodes this creates many metrics, so it is off by
default.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "boolean",
"javaType": "boolean", "defaultValue": false, "secret": false },
{ "name": "camel.metrics.enableRouteEventNotifier", "required": false,
"description": "Whether to capture the number of routes added, running and
reloaded, with the MicrometerRouteEventNotifier.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "boolean",
"javaType": "boolean", "defaultValue": true, "secret": false },
{ "name": "camel.metrics.enableRoutePolicy", "required": false,
"description": "Whether to capture the processing times and the exchange counts
of the routes and the CamelContext, with the MicrometerRoutePolicyFactory.
routePolicyLevel selects which of them.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "boolean",
"javaType": "boolean", "defaultValue": true, "secret": false },
- { "name": "camel.metrics.logMetricsOnShutdown", "required": false,
"description": "Whether to log the metrics when the application shuts down.
Only when enableExchangeEventNotifier is true.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "boolean",
"javaType": "boolean", "defaultValue": false, "secret": false },
+ { "name": "camel.metrics.logMetricsOnShutdown", "required": false,
"description": "Whether to log the metrics when the application shuts down.
This option works independently of enableExchangeEventNotifier: the shutdown
dump is produced even when the exchange event notifier is disabled.",
"sourceType": "org.apache.camel.main.MetricsConfigurationProperties", "type":
"boolean", "javaType": "boolean", "defaultValue": false, "secret": false },
{ "name": "camel.metrics.logMetricsOnShutdownFilters", "required": false,
"description": "The metrics to log on shutdown, separated by commas; all
metrics when not set. A filter with a wildcard, such as camel.exchanges.*,
matches every metric whose name contains the filter without the wildcard; other
filters must equal the name.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "string",
"javaType": "java.lang.String", "secret": false },
{ "name": "camel.metrics.logMetricsOnShutdownFormat", "required": false,
"description": "Format used to log metrics when application is shutting down.
Either json (default) or prometheus format.", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "enum",
"javaType": "java.lang.String", "defaultValue": "json", "secret": false,
"enum": [ "json", "prometheus" ] },
{ "name": "camel.metrics.namingStrategy", "required": false,
"description": "The naming style of the metrics: default uses the Micrometer
naming convention (such as camel.exchanges.total), legacy uses the older names
in camel case (such as CamelExchangesTotal).", "sourceType":
"org.apache.camel.main.MetricsConfigurationProperties", "type": "enum",
"javaType": "java.lang.String", "defaultValue": "default", "secret": false,
"enum": [ "default", "legacy" ] },
diff --git a/core/camel-main/src/main/docs/main.adoc
b/core/camel-main/src/main/docs/main.adoc
index a9fe8bf2357b..ffbd844071c3 100644
--- a/core/camel-main/src/main/docs/main.adoc
+++ b/core/camel-main/src/main/docs/main.adoc
@@ -704,7 +704,7 @@ The camel.metrics supports 18 options, which are listed
below.
|===
| Name | Description | Default | Type
| `camel.metrics.baseEndpointURIExchangeEventNotifier` | Whether the
endpointName tag of the exchange event metrics holds the endpoint URI without
its query parameters (true) or the full URI (false). The full URI can create
many tags, for example with a dynamic to (toD), which computes a URI for each
message. | true | boolean
-| `camel.metrics.binders` | Micrometer binders to add, separated by commas,
named after the binder class in dash case without the Metrics suffix:
class-loader, commons-object-pool2, file-descriptor, jvm-compilation, jvm-gc,
jvm-heap-pressure, jvm-info, jvm-memory, jvm-thread, jvm-thread-deadlock,
log4j2, logback, processor and uptime. A name that matches no binder is
ignored. | | String
+| `camel.metrics.binders` | Micrometer binders to add, separated by commas,
named after the binder class in dash case without the Metrics suffix:
class-loader, commons-object-pool2, file-descriptor, jvm-compilation, jvm-gc,
jvm-heap-pressure, jvm-info, jvm-memory, jvm-thread, jvm-thread-deadlock,
log4j2, logback, processor and uptime. A name that matches no binder is logged
as a warning and ignored. | | String
| `camel.metrics.clearOnReload` | Whether to remove the metrics of the routes
(the Camel metrics with a routeId tag, except the camel.exchanges ones) when
routes are reloaded, such as when the Camel CLI reloads changed routes. | true
| boolean
| `camel.metrics.enabled` | Whether to enable Micrometer metrics, with
camel-micrometer-prometheus, which must be on the classpath. Setting any other
camel.metrics option also enables it, unless this option is false. | false |
boolean
| `camel.metrics.enableExchangeEventNotifier` | Whether to capture metrics of
the exchange events, with the MicrometerExchangeEventNotifier: the time taken
to process an exchange and to send it to each endpoint, the inflight exchanges
of each route and the timestamp of the last exchange. | true | boolean
@@ -712,7 +712,7 @@ The camel.metrics supports 18 options, which are listed
below.
| `camel.metrics.enableMessageHistory` | Whether to capture the processing
time of each node of the routes, with the MicrometerMessageHistoryFactory,
which also turns on message history. With many routes and nodes this creates
many metrics, so it is off by default. | false | boolean
| `camel.metrics.enableRouteEventNotifier` | Whether to capture the number of
routes added, running and reloaded, with the MicrometerRouteEventNotifier. |
true | boolean
| `camel.metrics.enableRoutePolicy` | Whether to capture the processing times
and the exchange counts of the routes and the CamelContext, with the
MicrometerRoutePolicyFactory. routePolicyLevel selects which of them. | true |
boolean
-| `camel.metrics.logMetricsOnShutdown` | Whether to log the metrics when the
application shuts down. Only when enableExchangeEventNotifier is true. | false
| boolean
+| `camel.metrics.logMetricsOnShutdown` | Whether to log the metrics when the
application shuts down. This option works independently of
enableExchangeEventNotifier: the shutdown dump is produced even when the
exchange event notifier is disabled. | false | boolean
| `camel.metrics.logMetricsOnShutdownFilters` | The metrics to log on
shutdown, separated by commas; all metrics when not set. A filter with a
wildcard, such as camel.exchanges.*, matches every metric whose name contains
the filter without the wildcard; other filters must equal the name. | | String
| `camel.metrics.logMetricsOnShutdownFormat` | Format used to log metrics when
application is shutting down. Either json (default) or prometheus format. |
json | String
| `camel.metrics.namingStrategy` | The naming style of the metrics: default
uses the Micrometer naming convention (such as camel.exchanges.total), legacy
uses the older names in camel case (such as CamelExchangesTotal). | default |
String
diff --git
a/core/camel-main/src/main/java/org/apache/camel/main/MetricsConfigurationProperties.java
b/core/camel-main/src/main/java/org/apache/camel/main/MetricsConfigurationProperties.java
index 7d7151eb5421..39cf7e75c058 100644
---
a/core/camel-main/src/main/java/org/apache/camel/main/MetricsConfigurationProperties.java
+++
b/core/camel-main/src/main/java/org/apache/camel/main/MetricsConfigurationProperties.java
@@ -221,7 +221,8 @@ public class MetricsConfigurationProperties implements
BootstrapCloseable {
}
/**
- * Whether to log the metrics when the application shuts down. Only when
enableExchangeEventNotifier is true.
+ * Whether to log the metrics when the application shuts down. This option
works independently of
+ * enableExchangeEventNotifier: the shutdown dump is produced even when
the exchange event notifier is disabled.
*/
public void setLogMetricsOnShutdown(boolean logMetricsOnShutdown) {
this.logMetricsOnShutdown = logMetricsOnShutdown;
@@ -259,7 +260,7 @@ public class MetricsConfigurationProperties implements
BootstrapCloseable {
* Micrometer binders to add, separated by commas, named after the binder
class in dash case without the Metrics
* suffix: class-loader, commons-object-pool2, file-descriptor,
jvm-compilation, jvm-gc, jvm-heap-pressure,
* jvm-info, jvm-memory, jvm-thread, jvm-thread-deadlock, log4j2, logback,
processor and uptime. A name that matches
- * no binder is ignored.
+ * no binder is logged as a warning and ignored.
*/
public void setBinders(String binders) {
this.binders = binders;
@@ -387,7 +388,8 @@ public class MetricsConfigurationProperties implements
BootstrapCloseable {
}
/**
- * Whether to log the metrics when the application shuts down. Only when
enableExchangeEventNotifier is true.
+ * Whether to log the metrics when the application shuts down. This option
works independently of
+ * enableExchangeEventNotifier: the shutdown dump is produced even when
the exchange event notifier is disabled.
*/
public MetricsConfigurationProperties withLogMetricsOnShutdown(boolean
logMetricsOnShutdown) {
this.logMetricsOnShutdown = logMetricsOnShutdown;
@@ -416,7 +418,7 @@ public class MetricsConfigurationProperties implements
BootstrapCloseable {
* Micrometer binders to add, separated by commas, named after the binder
class in dash case without the Metrics
* suffix: class-loader, commons-object-pool2, file-descriptor,
jvm-compilation, jvm-gc, jvm-heap-pressure,
* jvm-info, jvm-memory, jvm-thread, jvm-thread-deadlock, log4j2, logback,
processor and uptime. A name that matches
- * no binder is ignored.
+ * no binder is logged as a warning and ignored.
*/
public MetricsConfigurationProperties withBinders(String binders) {
this.binders = binders;