gnodet-bot commented on code in PR #28729:
URL: https://github.com/apache/camel/pull/28729#discussion_r4422247039


##########
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 not tracked in `meterMap` — leaks on stop/restart**
   
   The gauge registered in `doStart()` is not stored in `meterMap`, so 
`doStop()` (which only iterates `meterMap`) won't remove it from the registry. 
On a stop/restart cycle the registry will accumulate a stale gauge reference.
   
   The existing pattern for inflight meters stores the registered `Meter` in 
`meterMap` and removes it in `doStop()`:
   ```java
   meterMap.values().forEach(m -> getMeterRegistry().remove(m));
   ```
   
   The last-timestamp gauge should follow the same pattern — store the return 
value of `.register()` in a field and remove it in `doStop()`:
   ```suggestion
                       .description("Last exchange processed time in 
milliseconds since the Unix epoch")
                       .register(getMeterRegistry());
   ```
   Add a `private Gauge lastTimestampGauge` field, assign it here, and call 
`getMeterRegistry().remove(lastTimestampGauge)` in `doStop()`.
   
   Note: this issue exists in the pre-PR code too (the old 
`setLastTimeExchange()` path had the same gap), but the explicit `doStart()` 
registration makes it cleaner to fix here.



##########
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.

Review Comment:
   💡 **Misleading comment — says assertion FAILS but it passes with this fix**
   
   ```suggestion
           // The gauge should have been registered once at doStart() — never 
queried again per exchange.
           // Before this fix, find() was called on every exchange (count 
times); now it must be 0.
   ```
   
   The phrase "With the current implementation this assertion FAILS" reads as 
if the test is expected to fail right now. Since the test ships as part of the 
fix, "current implementation" is ambiguous — change "current" to "old" or 
"previous" to make the intent clear.



-- 
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]

Reply via email to