AMQ-9552: Add metric lastMessageTimestamp to StatisticsBroker based on lastSampleTime of metric enqueues (if hasUpdated)

This commit is contained in:
Grzegorz Kochański 2024-08-23 13:02:54 +02:00
parent e45ee4aae5
commit 4c307dfae5
4 changed files with 17 additions and 0 deletions

View File

@ -160,6 +160,11 @@ public class StatisticsBroker extends BrokerFilter {
tempFirstMessage.clear();
}
}
if ( stats.getEnqueues().hasUpdated() ) {
// NOTICE: Client-side, you may get the broker "now" Timestamp by msg.getJMSTimestamp()
// This allows for calculating inactivity.
statsMessage.setLong("lastMessageTimestamp", stats.getEnqueues().getLastSampleTime());
}
statsMessage.setJMSCorrelationID(messageSend.getCorrelationId());
sendStats(producerExchange.getConnectionContext(), statsMessage, replyTo);
}

View File

@ -30,6 +30,7 @@ public class StatisticImpl implements Statistic, Resettable {
private String description;
private long startTime;
private long lastSampleTime;
private boolean hasUpdated;
private boolean doReset = true;
public StatisticImpl(String name, String unit, String description) {
@ -38,17 +39,20 @@ public class StatisticImpl implements Statistic, Resettable {
this.description = description;
this.startTime = System.currentTimeMillis();
this.lastSampleTime = this.startTime;
this.hasUpdated = false;
}
public synchronized void reset() {
if(isDoReset()) {
this.startTime = System.currentTimeMillis();
this.lastSampleTime = this.startTime;
this.hasUpdated = false;
}
}
protected synchronized void updateSampleTime() {
this.lastSampleTime = System.currentTimeMillis();
this.hasUpdated = true;
}
public synchronized String toString() {
@ -101,6 +105,9 @@ public class StatisticImpl implements Statistic, Resettable {
return this.doReset;
}
public boolean hasUpdated(){
return hasUpdated;
}
/**
* @param doReset the doReset to set
*/

View File

@ -30,9 +30,12 @@ public class TimeStatisticTest extends StatisticTestSupport {
TimeStatisticImpl stat = new TimeStatisticImpl("myTimer", "millis", "myDescription");
assertStatistic(stat, "myTimer", "millis", "myDescription");
assertFalse(stat.hasUpdated());
assertEquals(0, stat.getCount());
stat.addTime(100);
assertTrue(stat.hasUpdated());
assertEquals(1, stat.getCount());
assertEquals(100, stat.getMinTime());
assertEquals(100, stat.getMaxTime());
@ -59,6 +62,7 @@ public class TimeStatisticTest extends StatisticTestSupport {
LOG.info("Stat is: " + stat);
stat.reset();
assertFalse(stat.hasUpdated());
assertEquals(0, stat.getCount());
assertEquals(0, stat.getMinTime());

View File

@ -218,6 +218,7 @@ public class BrokerStatisticsPluginTest extends TestCase{
assertEquals(1, reply.getLong("size"));
assertTrue(reply.getJMSTimestamp() > 0);
assertTrue(reply.getLong("firstMessageTimestamp") > 0);
assertTrue(reply.getLong("lastMessageTimestamp") > 0);
// Assert that we got the brokerInTime for the first message in queue as value of key "firstMessageTimestamp"
assertTrue(System.currentTimeMillis() >= reply.getLong("firstMessageTimestamp"));
assertEquals(Message.DEFAULT_PRIORITY, reply.getJMSPriority());