Skip to content

Commit

Permalink
AMQ-9552: Add metric lastMessageTimestamp to StatisticsBroker based o…
Browse files Browse the repository at this point in the history
…n lastSampleTime of metric enqueues,

 StatisticImpl#lastSampleTime is set to zero be default,
 add method StatisticImpl#getLastSampleTimeOrStartTime to maintain currect behaviour
  • Loading branch information
Grzegorz Kochański authored and Grzegorz Kochański committed Aug 23, 2024
1 parent e45ee4a commit e0bf09d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void messageSent(){
}

public long getLastAccessTime(){
return timeStatistic.getLastSampleTime();
return timeStatistic.getLastSampleTimeOrStartTime();
}

public void close(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ else if (dest instanceof Topic) {
tempFirstMessage.clear();
}
}
// NOTICE: Client-side, you may get the broker "now" Timestamp by msg.getJMSTimestamp()
// This allows for calculating inactivity.
// When no message is enqueued since broker start (or statistics reset) it returns 0
statsMessage.setLong("lastMessageTimestamp", stats.getEnqueues().getLastSampleTime());
statsMessage.setJMSCorrelationID(messageSend.getCorrelationId());
sendStats(producerExchange.getConnectionContext(), statsMessage, replyTo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public String toString() {

public void onMessage() {
if (enabled) {
long start = messageCount.getLastSampleTime();
long start = messageCount.getLastSampleTimeOrStartTime();
messageCount.increment();
long end = messageCount.getLastSampleTime();
messageRateTime.addTime(end - start);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public StatisticImpl(String name, String unit, String description) {
this.unit = unit;
this.description = description;
this.startTime = System.currentTimeMillis();
this.lastSampleTime = this.startTime;
this.lastSampleTime = 0;
}

public synchronized void reset() {
if(isDoReset()) {
this.startTime = System.currentTimeMillis();
this.lastSampleTime = this.startTime;
this.lastSampleTime = 0;
}
}

Expand Down Expand Up @@ -80,6 +80,9 @@ public synchronized long getLastSampleTime() {
return this.lastSampleTime;
}

public synchronized long getLastSampleTimeOrStartTime(){
return lastSampleTime == 0 ? startTime : lastSampleTime;
}
/**
* @return the enabled
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public void testStatistic() throws Exception {
TimeStatisticImpl stat = new TimeStatisticImpl("myTimer", "millis", "myDescription");
assertStatistic(stat, "myTimer", "millis", "myDescription");

assertEquals("Check if lastSimpleTime is 0 for new statistic", 0, stat.getLastSampleTime());

assertEquals(0, stat.getCount());

stat.addTime(100);
Expand Down Expand Up @@ -59,6 +61,7 @@ public void testStatistic() throws Exception {
LOG.info("Stat is: " + stat);

stat.reset();
assertEquals("Check if lastSimpleTime is 0 on statistic reset", 0, stat.getLastSampleTime());

assertEquals(0, stat.getCount());
assertEquals(0, stat.getMinTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ public void testDestinationStatsWithFirstMessageTimestamp() throws Exception {
assertEquals(1, reply.getLong("size"));
assertTrue(reply.getJMSTimestamp() > 0);
assertTrue(reply.getLong("firstMessageTimestamp") > 0);
assertTrue(reply.getLong("lastMessageTimestamp") >= msg.getJMSTimestamp());
// 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());
Expand Down

0 comments on commit e0bf09d

Please sign in to comment.