oscerd opened a new pull request, #28102:
URL: https://github.com/apache/camel/pull/28102

   Fixes [CAMEL-25804](https://issues.apache.org/jira/browse/CAMEL-25804).
   
   ## Problem
   
   ```java
   statefulKeyGauge = Gauge.builder(METRIC_STATEFUL_REMAINING, 
remainingSupplier, LongSupplier::getAsLong)
   ```
   
   Micrometer holds the state object of a gauge through a **weak** reference. 
From the `micrometer-core` jar:
   
   ```
   public class io.micrometer.core.instrument.internal.DefaultGauge<T> ... {
     private final java.lang.ref.WeakReference<T> ref;
   ```
   
   The only reference to the supplier was the inline lambda the producer passes 
in at `PQCProducer`:
   
   ```java
   metrics.registerStatefulKeyGauge(signatureAlgorithm, () -> {
       KeyPair kp = getRuntimeKeyPair();
       return kp != null && kp.getPrivate() != null ? 
getStatefulKeyRemaining(kp.getPrivate()) : -1;
   });
   ```
   
   Neither the producer nor `PQCMicrometerMetrics` kept it — only the `Gauge` 
was stored. Once collected, the gauge
   reports `NaN`, so `camel.pqc.stateful.key.remaining` — the one metric that 
tells an operator a one-time-use LMS/XMSS
   key is running out of signatures — silently goes dark.
   
   ## Fix
   
   The suppliers are held in a map for the lifetime of the metrics instance.
   
   The single `statefulKeyGauge` field is also replaced by a map keyed by 
algorithm: a second
   `registerStatefulKeyGauge` call overwrote the field and left the first gauge 
registered but unreachable, so
   `close()` could not remove it.
   
   ## Deliberately not changed
   
   A `MeterRegistry` is shared and identifies meters by id, so two producers 
whose `operation|algorithm|outcome` tags
   coincide share one meter instance — closing either de-registers it for both. 
Fixing that means giving each producer a
   distinguishing tag, which changes the published tag set and is a 
user-visible decision. I recorded it in the class
   javadoc rather than changing it unilaterally; happy to follow up if you want 
it.
   
   ## Tests
   
   New `PQCMicrometerMetricsTest`:
   
   - `theStatefulGaugeKeepsReportingAfterTheSupplierBecomesUnreachable` — 
against `main` this reads
     `expected: <42.0> but was: <NaN>`. It collects until a canary 
`WeakReference` is actually cleared and asserts that
     it was, so the test cannot pass by a collection simply not having run;
   - `registeringASecondGaugeDoesNotOrphanTheFirst` — against `main`, `close()` 
leaves the first gauge registered;
   - `operationCountersAreRecordedAndRemovedOnClose` and a no-op guard, 
covering the rest of the class, which had no
     unit test at all.
   
   Full `camel-pqc` build green: 243 tests, 0 failures.
   
   No upgrade-guide entry: a metric that was `NaN` now reports a value is a 
fix, not a migration step.
   
   ---
   _Reported, analysed and fixed by Claude Code (AI agent) on behalf of 
@oscerd._
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


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