davsclaus commented on code in PR #28729:
URL: https://github.com/apache/camel/pull/28729#discussion_r4452434572


##########
components/camel-micrometer/src/main/java/org/apache/camel/component/micrometer/eventnotifier/MicrometerExchangeEventNotifier.java:
##########
@@ -103,6 +112,17 @@ protected void doStart() throws Exception {
 
         inflightRepository = getCamelContext().getInflightRepository();
 
+        // Register once at startup — backing value updated via AtomicLong on 
each exchange
+        if (recordLastExchangeTimestamp) {
+            Meter meter = 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());
+            
meterMap.put(MicrometerConstants.CAMEL_EXCHANGE_LAST_TIME_METER_NAME, meter);

Review Comment:
   **Non-blocking: two CamelContexts that share one `MeterRegistry`**
   
   The gauge has no `camelContext` tag, so both contexts' notifiers register 
the same meter id:
   1. Context A registers the gauge, bound to A's `lastExchangeTimestampHolder`.
   2. Context B calls `register()` and gets A's gauge back. B's timestamps are 
never reported.
   3. Context B stops, and `doStop()` removes that shared gauge. Context A 
keeps running without the metric.
   
   Before this PR, the per-exchange `find()` re-registered a missing gauge, so 
this case recovered on its own. The register-once approach is right, but it 
makes the shared id visible.
   
   Tagging the gauge the same way as the other meters fixes both problems. Each 
context then owns its gauge, and `doStop()` removes only its own:
   
   ```java
   Gauge.builder(MicrometerConstants.CAMEL_EXCHANGE_LAST_TIME_METER_NAME, 
lastExchangeTimestampHolder, AtomicLong::get)
           .description("Last exchange processed time in milliseconds since the 
Unix epoch")
           .tag(MicrometerConstants.CAMEL_CONTEXT_TAG, 
getCamelContext().getName())
           .register(getMeterRegistry());
   ```
   
   This adds a label to a metric that shipped in 4.23. Queries by metric name 
keep working, so I don't think it needs more than a line in the upgrade guide. 
Smaller nit: a dedicated field avoids storing a meter name in the routeId-keyed 
`meterMap`, where a route with id `camel.exchanges.last.timestamp` would 
collide. That collision is unlikely, though.



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