This is an automated email from the ASF dual-hosted git repository. squakez pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit c4198667c9dd41068df29180c3d3e8cb4ce9a2a3 Author: Pasquale Congiusti <[email protected]> AuthorDate: Fri Oct 30 09:52:54 2026 +0100 chore(micrometer): add parameter to disable last exchange setting Ref CAMEL-26415 --- .../MicrometerExchangeEventNotifier.java | 27 +++++--- ...angeEventNotifierLastTimestampDisabledTest.java | 72 ++++++++++++++++++++++ ...eterExchangeEventNotifierLastTimestampTest.java | 6 +- 3 files changed, 95 insertions(+), 10 deletions(-) diff --git a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifier.java b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifier.java index a55d177267ee..7937c8ca1115 100644 --- a/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifier.java +++ b/components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifier.java @@ -52,6 +52,7 @@ public class MicrometerExchangeEventNotifier extends AbstractMicrometerEventNoti boolean registerKamelets; boolean registerTemplates = true; boolean baseEndpointURI = true; + boolean recordLastExchangeTimestamp = true; public MicrometerExchangeEventNotifier() { super(ExchangeEvent.class); @@ -88,6 +89,14 @@ public class MicrometerExchangeEventNotifier extends AbstractMicrometerEventNoti this.namingStrategy = namingStrategy; } + public boolean isRecordLastExchangeTimestamp() { + return recordLastExchangeTimestamp; + } + + public void setRecordLastExchangeTimestamp(boolean recordLastExchangeTimestamp) { + this.recordLastExchangeTimestamp = recordLastExchangeTimestamp; + } + @Override protected void doInit() throws Exception { ManagementStrategy ms = getCamelContext().getManagementStrategy(); @@ -104,12 +113,14 @@ public class MicrometerExchangeEventNotifier extends AbstractMicrometerEventNoti inflightRepository = getCamelContext().getInflightRepository(); // Register once at startup — backing value updated via AtomicLong on each exchange - Gauge.builder( - MicrometerConstants.CAMEL_EXCHANGE_LAST_TIME_METER_NAME, - lastExchangeTimestampHolder, - AtomicLong::get) - .description("Last exchange processed time in milliseconds since the Unix epoch") - .register(getMeterRegistry()); + if (recordLastExchangeTimestamp) { + Gauge.builder( + MicrometerConstants.CAMEL_EXCHANGE_LAST_TIME_METER_NAME, + lastExchangeTimestampHolder, + AtomicLong::get) + .description("Last exchange processed time in milliseconds since the Unix epoch") + .register(getMeterRegistry()); + } // need to be able to add/remove meter accordingly to route changes getCamelContext().getManagementStrategy().addEventNotifier(new SimpleEventNotifierSupport() { @@ -214,7 +225,9 @@ public class MicrometerExchangeEventNotifier extends AbstractMicrometerEventNoti Timer timer = getOrCreateTimer(name, tags, "Time taken for exchange processing"); sample.stop(timer); } - setLastTimeExchange(); + if (recordLastExchangeTimestamp) { + setLastTimeExchange(); + } } private Timer getOrCreateTimer(final String name, final Tags tags, final String description) { diff --git a/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifierLastTimestampDisabledTest.java b/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifierLastTimestampDisabledTest.java new file mode 100644 index 000000000000..76e78a1a6698 --- /dev/null +++ b/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifierLastTimestampDisabledTest.java @@ -0,0 +1,72 @@ +/* + * 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.eventnotifier; + +import io.micrometer.core.instrument.Gauge; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.micrometer.MicrometerConstants; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Verifies that when {@code recordLastExchangeTimestamp} is set to {@code false} the gauge + * {@code camel.exchanges.last.timestamp} is never registered and the backing value is never written, regardless of how + * many exchanges complete. + */ +public class MicrometerExchangeEventNotifierLastTimestampDisabledTest extends AbstractMicrometerEventNotifierTest { + + private static final String DIRECT_IN = "direct://in"; + private static final String MOCK_OUT = "mock://out"; + private static final String ROUTE_ID = "test"; + + @Override + protected AbstractMicrometerEventNotifier<?> getEventNotifier() { + MicrometerExchangeEventNotifier notifier = new MicrometerExchangeEventNotifier(); + notifier.setRecordLastExchangeTimestamp(false); + return notifier; + } + + @Test + public void testLastTimestampGaugeAbsentWhenDisabled() throws Exception { + int count = 5; + MockEndpoint mock = getMockEndpoint(MOCK_OUT); + mock.expectedMessageCount(count); + + for (int i = 0; i < count; i++) { + template.sendBody(DIRECT_IN, "msg-" + i); + } + mock.assertIsSatisfied(); + + // Gauge must not exist in the registry at all + Gauge gauge = meterRegistry.find(MicrometerConstants.CAMEL_EXCHANGE_LAST_TIME_METER_NAME).gauge(); + assertThat(gauge) + .as("last-timestamp gauge must NOT be registered when recordLastExchangeTimestamp=false") + .isNull(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from(DIRECT_IN).routeId(ROUTE_ID).to(MOCK_OUT); + } + }; + } +} diff --git a/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifierLastTimestampTest.java b/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifierLastTimestampTest.java index 477d08c43888..b215b7ce616e 100644 --- a/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifierLastTimestampTest.java +++ b/components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifierLastTimestampTest.java @@ -29,9 +29,9 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; /** - * Proves that the current implementation does not perform a MeterRegistry.find() lookup on every exchange completion via - * setLastTimeExchange(). The test wraps the registry in a counting spy and asserts that after N completed exchanges the - * find() call count is 0 — i.e. the gauge is registered once at startup and never looked up again on the hot path. + * Proves that the current implementation does not perform a MeterRegistry.find() lookup on every exchange completion + * via setLastTimeExchange(). The test wraps the registry in a counting spy and asserts that after N completed exchanges + * the find() call count is 0 — i.e. the gauge is registered once at startup and never looked up again on the hot path. */ public class MicrometerExchangeEventNotifierLastTimestampTest extends AbstractMicrometerEventNotifierTest {
