From 4131c82faf6c62cf08342109df7260bfaab118c0 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Tue, 30 Jun 2015 09:31:30 -0400 Subject: [PATCH] INT-3753: Expose More Metrics via IMBE JIRA: https://jira.spring.io/browse/INT-3753 INT-3753: Add Channel Send Count to IMBE Missing accessor for channel send counts. Expose Full Metrics Objects --- .../monitor/IntegrationMBeanExporter.java | 48 +++++++++++++++++++ .../ChannelIntegrationTests-context.xml | 9 +++- .../monitor/ChannelIntegrationTests.java | 36 ++++++++++++-- 3 files changed, 88 insertions(+), 5 deletions(-) diff --git a/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java b/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java index 4d301e808e..40c720a3c3 100644 --- a/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java +++ b/spring-integration-jmx/src/main/java/org/springframework/integration/monitor/IntegrationMBeanExporter.java @@ -610,6 +610,14 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati return channelsByName.keySet().toArray(new String[channelsByName.size()]); } + public MessageHandlerMetrics getHandlerMetrics(String name) { + if (handlersByName.containsKey(name)) { + return handlersByName.get(name); + } + logger.debug("No handler found for (" + name + ")"); + return null; + } + public Statistics getHandlerDuration(String name) { if (handlersByName.containsKey(name)) { return handlersByName.get(name).getDuration(); @@ -618,6 +626,14 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati return null; } + public MessageSourceMetrics getSourceMetrics(String name) { + if (sourcesByName.containsKey(name)) { + return sourcesByName.get(name); + } + logger.debug("No source found for (" + name + ")"); + return null; + } + public int getSourceMessageCount(String name) { return (int) getSourceMessageCountLong(name); } @@ -630,6 +646,38 @@ public class IntegrationMBeanExporter extends MBeanExporter implements Applicati return -1; } + public MessageChannelMetrics getChannelMetrics(String name) { + if (channelsByName.containsKey(name)) { + return channelsByName.get(name); + } + logger.debug("No channel found for (" + name + ")"); + return null; + } + + public int getChannelSendCount(String name) { + return (int) getChannelSendCountLong(name); + } + + public long getChannelSendCountLong(String name) { + if (channelsByName.containsKey(name)) { + return channelsByName.get(name).getSendCountLong(); + } + logger.debug("No channel found for (" + name + ")"); + return -1; + } + + public int getChannelSendErrorCount(String name) { + return (int) getChannelSendErrorCountLong(name); + } + + public long getChannelSendErrorCountLong(String name) { + if (channelsByName.containsKey(name)) { + return channelsByName.get(name).getSendErrorCountLong(); + } + logger.debug("No channel found for (" + name + ")"); + return -1; + } + public int getChannelReceiveCount(String name) { return (int) getChannelReceiveCountLong(name); } diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests-context.xml b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests-context.xml index 1263451ade..b2bb2d3f14 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests-context.xml +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests-context.xml @@ -8,10 +8,11 @@ - + - + @@ -22,4 +23,8 @@ + + + + diff --git a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests.java b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests.java index faaa6919a6..10c7ec64f1 100644 --- a/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests.java +++ b/spring-integration-jmx/src/test/java/org/springframework/integration/monitor/ChannelIntegrationTests.java @@ -12,14 +12,18 @@ */ package org.springframework.integration.monitor; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.handler.management.MessageHandlerMetrics; import org.springframework.messaging.MessageChannel; +import org.springframework.messaging.MessageDeliveryException; import org.springframework.messaging.PollableChannel; import org.springframework.messaging.support.GenericMessage; import org.springframework.test.annotation.DirtiesContext; @@ -50,15 +54,41 @@ public class ChannelIntegrationTests { requests.send(new GenericMessage("foo")); + String intermediateChannelName = "" + intermediate; + + assertEquals(1, messageChannelsMonitor.getChannelSendCount(intermediateChannelName)); + double rate = messageChannelsMonitor.getChannelSendRate("" + requests).getMean(); assertTrue("No statistics for requests channel", rate >= 0); - rate = messageChannelsMonitor.getChannelSendRate("" + intermediate).getMean(); + rate = messageChannelsMonitor.getChannelSendRate(intermediateChannelName).getMean(); assertTrue("No statistics for intermediate channel", rate >= 0); assertNotNull(intermediate.receive(100L)); - double count = messageChannelsMonitor.getChannelReceiveCount("" + intermediate); - assertTrue("No statistics for intermediate channel", count >= 0); + assertEquals(1, messageChannelsMonitor.getChannelReceiveCount(intermediateChannelName)); + + requests.send(new GenericMessage("foo")); + try { + requests.send(new GenericMessage("foo")); + } + catch (MessageDeliveryException e) { + } + + assertEquals(3, messageChannelsMonitor.getChannelSendCount(intermediateChannelName)); + + assertEquals(1, messageChannelsMonitor.getChannelSendErrorCount(intermediateChannelName)); + + assertSame(intermediate, messageChannelsMonitor.getChannelMetrics(intermediateChannelName)); + + MessageHandlerMetrics handlerMetrics = messageChannelsMonitor.getHandlerMetrics("bridge"); + + assertEquals(3, handlerMetrics.getHandleCount()); + assertEquals(1, handlerMetrics.getErrorCount()); + + Thread.sleep(50); + + assertTrue(messageChannelsMonitor.getSourceMessageCount("source") > 0); + assertTrue(messageChannelsMonitor.getSourceMetrics("source").getMessageCount() > 0); }