gnodet-bot commented on code in PR #28729:
URL: https://github.com/apache/camel/pull/28729#discussion_r4422463030
##########
components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifier.java:
##########
@@ -103,6 +112,16 @@ protected void doStart() throws Exception {
inflightRepository = getCamelContext().getInflightRepository();
+ // Register once at startup — backing value updated via AtomicLong on
each exchange
+ 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());
Review Comment:
⚠️ **Gauge still not tracked in `meterMap` — open from previous review**
The return value of `.register()` is not captured, so `doStop()` (which only
iterates `meterMap`) never removes this gauge. On stop/restart the gauge
persists in the registry; a second `.register()` call on restart will either
silently deduplicate (Prometheus/SimpleMeterRegistry) or accumulate stale
entries depending on the registry implementation.
Fix: store the returned `Meter` and remove it in `doStop()`:
```suggestion
.description("Last exchange processed time in
milliseconds since the Unix epoch")
.register(getMeterRegistry());
meterMap.put(MicrometerConstants.CAMEL_EXCHANGE_LAST_TIME_METER_NAME,
getMeterRegistry().find(MicrometerConstants.CAMEL_EXCHANGE_LAST_TIME_METER_NAME).meter());
```
Or capture directly from `.register()` as `Meter gauge =
Gauge.builder(...).register(getMeterRegistry())` and put it into `meterMap`.
##########
components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifierLastTimestampTest.java:
##########
@@ -0,0 +1,121 @@
+/*
+ * 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 java.util.concurrent.atomic.AtomicInteger;
+
+import io.micrometer.core.instrument.Gauge;
+import io.micrometer.core.instrument.search.RequiredSearch;
+import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
+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;
+
+/**
+ * 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 {
+
+ private static final String DIRECT_IN = "direct://in";
+ private static final String MOCK_OUT = "mock://out";
+ private static final String ROUTE_ID = "test";
+
+ /** Counts calls to find(CAMEL_EXCHANGE_LAST_TIME_METER_NAME) made after
context startup. */
+ private final AtomicInteger findCallsAfterStart = new AtomicInteger(0);
+
+ @Override
+ public void addRegistry() {
+ meterRegistry = new
CountingCompositeMeterRegistry(findCallsAfterStart);
+ }
+
+ @Override
+ protected AbstractMicrometerEventNotifier<?> getEventNotifier() {
+ return new MicrometerExchangeEventNotifier();
+ }
+
+ @Test
+ public void testLastTimestampGaugeIsNotLookedUpOnEveryExchange() throws
Exception {
+ int count = 5;
+ MockEndpoint mock = getMockEndpoint(MOCK_OUT);
+ mock.expectedMessageCount(count);
+
+ // Reset counter after startup so we only measure hot-path lookups
+ findCallsAfterStart.set(0);
+
+ for (int i = 0; i < count; i++) {
+ template.sendBody(DIRECT_IN, "msg-" + i);
+ }
+ mock.assertIsSatisfied();
+
+ // The gauge should have been registered once at doStart() — never
queried again per exchange.
+ // With the current implementation this assertion FAILS because find()
is called `count` times.
+ assertThat(findCallsAfterStart.get())
+ .as("MeterRegistry.find() for the last-timestamp gauge must
not be called on every exchange")
+ .isEqualTo(0);
+
+ // Sanity: the gauge itself must exist and hold a value > 0
+ Gauge gauge =
meterRegistry.find(MicrometerConstants.CAMEL_EXCHANGE_LAST_TIME_METER_NAME).gauge();
+ assertThat(gauge).as("last-timestamp gauge must be
registered").isNotNull();
+ assertThat(gauge.value()).as("last-timestamp gauge must hold a
non-zero timestamp").isGreaterThan(0);
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from(DIRECT_IN).routeId(ROUTE_ID).to(MOCK_OUT);
+ }
+ };
+ }
+
+ //
---------------------------------------------------------------------------
+ // Spy registry that intercepts find() calls for the last-timestamp meter
name
+ //
---------------------------------------------------------------------------
+
+ private static class CountingCompositeMeterRegistry extends
io.micrometer.core.instrument.composite.CompositeMeterRegistry {
Review Comment:
⚠️ **FQCN violation — open from previous review (CI will fail)**
`io.micrometer.core.instrument.composite.CompositeMeterRegistry` is used
inline without an import. CLAUDE.md (Imports section): "Do NOT use fully
qualified class names in Java code; import and use the simple name."
OpenRewrite will rewrite this and CI fails on uncommitted changes.
Add to the file's imports:
```suggestion
private static class CountingCompositeMeterRegistry extends
CompositeMeterRegistry {
```
And add `import
io.micrometer.core.instrument.composite.CompositeMeterRegistry;` to the import
block.
##########
components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifierLastTimestampTest.java:
##########
@@ -0,0 +1,121 @@
+/*
+ * 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 java.util.concurrent.atomic.AtomicInteger;
+
+import io.micrometer.core.instrument.Gauge;
+import io.micrometer.core.instrument.search.RequiredSearch;
+import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
+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;
+
+/**
+ * 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 {
+
+ private static final String DIRECT_IN = "direct://in";
+ private static final String MOCK_OUT = "mock://out";
+ private static final String ROUTE_ID = "test";
+
+ /** Counts calls to find(CAMEL_EXCHANGE_LAST_TIME_METER_NAME) made after
context startup. */
+ private final AtomicInteger findCallsAfterStart = new AtomicInteger(0);
+
+ @Override
+ public void addRegistry() {
+ meterRegistry = new
CountingCompositeMeterRegistry(findCallsAfterStart);
+ }
+
+ @Override
+ protected AbstractMicrometerEventNotifier<?> getEventNotifier() {
+ return new MicrometerExchangeEventNotifier();
+ }
+
+ @Test
+ public void testLastTimestampGaugeIsNotLookedUpOnEveryExchange() throws
Exception {
+ int count = 5;
+ MockEndpoint mock = getMockEndpoint(MOCK_OUT);
+ mock.expectedMessageCount(count);
+
+ // Reset counter after startup so we only measure hot-path lookups
+ findCallsAfterStart.set(0);
+
+ for (int i = 0; i < count; i++) {
+ template.sendBody(DIRECT_IN, "msg-" + i);
+ }
+ mock.assertIsSatisfied();
+
+ // The gauge should have been registered once at doStart() — never
queried again per exchange.
+ // With the current implementation this assertion FAILS because find()
is called `count` times.
+ assertThat(findCallsAfterStart.get())
+ .as("MeterRegistry.find() for the last-timestamp gauge must
not be called on every exchange")
+ .isEqualTo(0);
+
+ // Sanity: the gauge itself must exist and hold a value > 0
+ Gauge gauge =
meterRegistry.find(MicrometerConstants.CAMEL_EXCHANGE_LAST_TIME_METER_NAME).gauge();
+ assertThat(gauge).as("last-timestamp gauge must be
registered").isNotNull();
+ assertThat(gauge.value()).as("last-timestamp gauge must hold a
non-zero timestamp").isGreaterThan(0);
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from(DIRECT_IN).routeId(ROUTE_ID).to(MOCK_OUT);
+ }
+ };
+ }
+
+ //
---------------------------------------------------------------------------
+ // Spy registry that intercepts find() calls for the last-timestamp meter
name
+ //
---------------------------------------------------------------------------
+
+ private static class CountingCompositeMeterRegistry extends
io.micrometer.core.instrument.composite.CompositeMeterRegistry {
+
+ private final AtomicInteger counter;
+
+ CountingCompositeMeterRegistry(AtomicInteger counter) {
+ super();
+ add(new SimpleMeterRegistry());
+ this.counter = counter;
+ }
+
+ @Override
+ public RequiredSearch get(String name) {
+ if
(MicrometerConstants.CAMEL_EXCHANGE_LAST_TIME_METER_NAME.equals(name)) {
+ counter.incrementAndGet();
+ }
+ return super.get(name);
+ }
+
+ @Override
+ public io.micrometer.core.instrument.search.Search find(String name) {
Review Comment:
⚠️ **FQCN violation — open from previous review (CI will fail)**
`io.micrometer.core.instrument.search.Search` is used inline without an
import (`RequiredSearch` is imported but that's a different type). Same
CLAUDE.md rule as above.
```suggestion
public Search find(String name) {
```
And add `import io.micrometer.core.instrument.search.Search;` to the import
block.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]