Skip to content

Commit

Permalink
ARTEMIS-5320 expose session count metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
jbertram authored and clebertsuconic committed Feb 26, 2025
1 parent 295f64b commit 09685c8
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2839,12 +2839,24 @@ static void getPrefetchPageMessages(Object source) {
@LogMessage(id = 601794, value = "User {} is getting prefetchPageMessages on target resource: {}", level = LogMessage.Level.INFO)
void getPrefetchPageMessages(String user, Object source);


static void getPrefetchPageBytes(Object source) {
BASE_LOGGER.getPrefetchPageBytes(getCaller(), source);
}

@LogMessage(id = 601795, value = "User {} is getting prefetchPageBytes on target resource: {}", level = LogMessage.Level.INFO)
void getPrefetchPageBytes(String user, Object source);

static void getSessionCount(Object source, Object... args) {
BASE_LOGGER.getSessionCount(getCaller(), source, parametersList(args));
}

@LogMessage(id = 601796, value = "User {} is getting session count on target resource: {} {}", level = LogMessage.Level.INFO)
void getSessionCount(String user, Object source, String args);

static void getTotalSessionCount(Object source) {
BASE_LOGGER.getTotalSessionCount(getCaller(), source);
}

@LogMessage(id = 601797, value = "User {} is getting total session count on target resource: {}", level = LogMessage.Level.INFO)
void getTotalSessionCount(String user, Object source);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
*/
public interface ActiveMQServerControl {
String CONNECTION_COUNT_DESCRIPTION = "Number of clients connected to this server";
String TOTAL_CONNECTION_COUNT_DESCRIPTION = "Number of clients which have connected to this server since it was started";
String TOTAL_CONNECTION_COUNT_DESCRIPTION = "Total number of clients which have connected to this server since it was started";
String SESSION_COUNT_DESCRIPTION = "Number of sessions on this server";
String TOTAL_SESSION_COUNT_DESCRIPTION = "Total number of sessions created on this server since it was started";
String ADDRESS_MEMORY_USAGE_DESCRIPTION = "Memory used by all the addresses on broker for in-memory messages";
String ADDRESS_MEMORY_USAGE_PERCENTAGE_DESCRIPTION = "Memory used by all the addresses on broker as a percentage of the global-max-size";
String DISK_STORE_USAGE_DESCRIPTION = "Fraction of total disk store used";
Expand Down Expand Up @@ -64,11 +66,23 @@ public interface ActiveMQServerControl {
int getConnectionCount();

/**
* {@return the number of clients which have connected to this server since it was started.}
* {@return the total number of clients which have connected to this server since it was started.}
*/
@Attribute(desc = TOTAL_CONNECTION_COUNT_DESCRIPTION)
long getTotalConnectionCount();

/**
* Returns the number of sessions on this server.
*/
@Attribute(desc = SESSION_COUNT_DESCRIPTION)
int getSessionCount();

/**
* Returns the total number of sessions created on this server since it was started.
*/
@Attribute(desc = TOTAL_SESSION_COUNT_DESCRIPTION)
long getTotalSessionCount();

/**
* {@return the number of messages in all queues currently on the server.}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,36 @@ public long getTotalConnectionCount() {
}
}

@Override
public int getSessionCount() {
if (AuditLogger.isBaseLoggingEnabled()) {
AuditLogger.getSessionCount(this.server);
}
checkStarted();

clearIO();
try {
return server.getSessionCount();
} finally {
blockOnIO();
}
}

@Override
public long getTotalSessionCount() {
if (AuditLogger.isBaseLoggingEnabled()) {
AuditLogger.getTotalSessionCount(this.server);
}
checkStarted();

clearIO();
try {
return server.getTotalSessionCount();
} finally {
blockOnIO();
}
}

@Override
public long getTotalMessageCount() {
if (AuditLogger.isBaseLoggingEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,10 @@ ServerSession createInternalSession(String name,

long getTotalConnectionCount();

int getSessionCount();

long getTotalSessionCount();

long getTotalMessageCount();

long getTotalMessagesAdded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import java.util.function.Supplier;
Expand Down Expand Up @@ -349,6 +350,8 @@ public class ActiveMQServerImpl implements ActiveMQServer {

private final ConcurrentMap<String, ServerSession> sessions = new ConcurrentHashMap<>();

private AtomicLong totalSessionCount = new AtomicLong(0);

private final Semaphore activationLock = new Semaphore(1);
/**
* This class here has the same principle of CountDownLatch but you can reuse the counters.
Expand Down Expand Up @@ -1866,6 +1869,7 @@ protected ServerSessionImpl internalCreateSession(String name,
ServerSessionImpl session = new ServerSessionImpl(name, username, password, validatedUser, minLargeMessageSize, autoCommitSends, autoCommitAcks, preAcknowledge, configuration.isPersistDeliveryCountBeforeDelivery(), xa, connection, storageManager, postOffice, resourceManager, securityStore, managementService, this, configuration.getManagementAddress(), defaultAddress == null ? null : SimpleString.of(defaultAddress), callback, context, pagingManager, prefixes, securityDomain, isLegacyProducer);

sessions.put(name, session);
totalSessionCount.incrementAndGet();

if (hasBrokerSessionPlugins()) {
callBrokerSessionPlugins(plugin -> plugin.afterCreateSession(session));
Expand Down Expand Up @@ -1942,6 +1946,16 @@ public long getTotalConnectionCount() {
return remotingService.getTotalConnectionCount();
}

@Override
public int getSessionCount() {
return sessions.size();
}

@Override
public long getTotalSessionCount() {
return totalSessionCount.get();
}

@Override
public long getTotalMessageCount() {
long total = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ private void registerBrokerMeters() {
metricsManager.registerBrokerGauge(builder -> {
builder.build(BrokerMetricNames.CONNECTION_COUNT, messagingServer, metrics -> (double) messagingServer.getConnectionCount(), ActiveMQServerControl.CONNECTION_COUNT_DESCRIPTION, Collections.emptyList());
builder.build(BrokerMetricNames.TOTAL_CONNECTION_COUNT, messagingServer, metrics -> (double) messagingServer.getTotalConnectionCount(), ActiveMQServerControl.TOTAL_CONNECTION_COUNT_DESCRIPTION, Collections.emptyList());
builder.build(BrokerMetricNames.SESSION_COUNT, messagingServer, metrics -> (double) messagingServer.getSessionCount(), ActiveMQServerControl.SESSION_COUNT_DESCRIPTION, Collections.emptyList());
builder.build(BrokerMetricNames.TOTAL_SESSION_COUNT, messagingServer, metrics -> (double) messagingServer.getTotalSessionCount(), ActiveMQServerControl.TOTAL_SESSION_COUNT_DESCRIPTION, Collections.emptyList());
builder.build(BrokerMetricNames.ADDRESS_MEMORY_USAGE, messagingServer, metrics -> (double) messagingServerControl.getAddressMemoryUsage(), ActiveMQServerControl.ADDRESS_MEMORY_USAGE_DESCRIPTION, Collections.emptyList());
builder.build(BrokerMetricNames.ADDRESS_MEMORY_USAGE_PERCENTAGE, messagingServer, metrics -> (double) messagingServerControl.getAddressMemoryUsagePercentage(), ActiveMQServerControl.ADDRESS_MEMORY_USAGE_PERCENTAGE_DESCRIPTION, Collections.emptyList());
builder.build(BrokerMetricNames.DISK_STORE_USAGE, messagingServer, metrics -> messagingServer.getDiskStoreUsage(), ActiveMQServerControl.DISK_STORE_USAGE_DESCRIPTION, Collections.emptyList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class BrokerMetricNames {

public static final String CONNECTION_COUNT = "connection.count";
public static final String TOTAL_CONNECTION_COUNT = "total.connection.count";
public static final String SESSION_COUNT = "session.count";
public static final String TOTAL_SESSION_COUNT = "total.session.count";
public static final String ADDRESS_MEMORY_USAGE = "address.memory.usage";
public static final String ADDRESS_MEMORY_USAGE_PERCENTAGE = "address.memory.usage.percentage";
public static final String DISK_STORE_USAGE = "disk.store.usage";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2520,7 +2520,7 @@ public void testTotalMessageCount() throws Exception {
}

@TestTemplate
public void testTotalConnectionCount() throws Exception {
public void testConnectionCounts() throws Exception {
final int CONNECTION_COUNT = 100;

ActiveMQServerControl serverControl = createManagementControl();
Expand All @@ -2536,6 +2536,37 @@ public void testTotalConnectionCount() throws Exception {
locator.close();
}

@TestTemplate
public void testSessionCounts() throws Exception {
// don't test this with management messages as it completely throws off the session counts
assumeFalse(usingCore());
final int SESSION_COUNT = 100;

ActiveMQServerControl serverControl = createManagementControl();
ServerLocator locator = createInVMNonHALocator();
ClientSessionFactory csf = createSessionFactory(locator);
List<ClientSession> sessions = new ArrayList<>(SESSION_COUNT);

assertEquals(0, serverControl.getSessionCount());

for (int i = 1; i <= SESSION_COUNT; i++) {
sessions.add(csf.createSession());
assertEquals(i, serverControl.getSessionCount());
}

assertEquals(SESSION_COUNT, serverControl.getTotalSessionCount());

for (int i = 1; i <= SESSION_COUNT; i++) {
sessions.get(i - 1).close();
assertEquals(SESSION_COUNT - i, serverControl.getSessionCount());
}

assertEquals(SESSION_COUNT, serverControl.getTotalSessionCount());
assertEquals(0, serverControl.getSessionCount());

locator.close();
}

@TestTemplate
public void testTotalMessagesAdded() throws Exception {
String random1 = RandomUtil.randomUUIDString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,16 @@ public long getTotalConnectionCount() {
return (Long) proxy.retrieveAttributeValue("totalConnectionCount", Long.class);
}

@Override
public int getSessionCount() {
return (Integer) proxy.retrieveAttributeValue("sessionCount", Integer.class);
}

@Override
public long getTotalSessionCount() {
return (Long) proxy.retrieveAttributeValue("totalSessionCount", Long.class);
}

@Override
public long getTotalMessageCount() {
return (Long) proxy.retrieveAttributeValue("totalMessageCount", Long.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public int hashCode() {
new Metric("artemis.address.memory.usage.percentage", 0.0, Arrays.asList(Tag.of("broker", "localhost"))),
new Metric("artemis.connection.count", 1.0, Arrays.asList(Tag.of("broker", "localhost"))),
new Metric("artemis.total.connection.count", 1.0, Arrays.asList(Tag.of("broker", "localhost"))),
new Metric("artemis.session.count", 1.0, Arrays.asList(Tag.of("broker", "localhost"))),
new Metric("artemis.total.session.count", 1.0, Arrays.asList(Tag.of("broker", "localhost"))),
new Metric("artemis.active", 1.0, Arrays.asList(Tag.of("broker", "localhost"))),
new Metric("artemis.replica.sync", 0.0, Arrays.asList(Tag.of("broker", "localhost"))),
new Metric("artemis.disk.store.usage", 0.0, Arrays.asList(Tag.of("broker", "localhost"))),
Expand Down

0 comments on commit 09685c8

Please sign in to comment.