Updated Branches: refs/heads/trunk d1a017c94 -> 44f87608f
initial draft of monitoring Project: http://git-wip-us.apache.org/repos/asf/mina/repo Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/44f87608 Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/44f87608 Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/44f87608 Branch: refs/heads/trunk Commit: 44f87608fe356594445221b163f171a18e84d3dc Parents: d1a017c Author: paliwalashish <[email protected]> Authored: Mon Jul 29 19:13:55 2013 +0530 Committer: paliwalashish <[email protected]> Committed: Mon Jul 29 19:13:55 2013 +0530 ---------------------------------------------------------------------- monitoring/pom.xml | 68 ++++++++++ .../mina/monitoring/MonitoringFilter.java | 109 +++++++++++++++ .../NioTcpEchoServerWithMonitoring.java | 131 +++++++++++++++++++ monitoring/src/test/resources/log4j.properties | 9 ++ pom.xml | 1 + 5 files changed, 318 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina/blob/44f87608/monitoring/pom.xml ---------------------------------------------------------------------- diff --git a/monitoring/pom.xml b/monitoring/pom.xml new file mode 100644 index 0000000..92fc945 --- /dev/null +++ b/monitoring/pom.xml @@ -0,0 +1,68 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.mina</groupId> + <artifactId>mina-parent</artifactId> + <version>3.0.0-M3-SNAPSHOT</version> + </parent> + + <artifactId>mina-monitoring</artifactId> + <name>Apache MINA Monitoring::Monitoring ${project.version}</name> + <packaging>jar</packaging> + + <properties> + <symbolicName>${project.groupId}.avro</symbolicName> + <exportedPackage>${project.groupId}</exportedPackage> + <metrics.version>3.0.1</metrics.version> + </properties> + + <dependencies> + <dependency> + <groupId>com.codahale.metrics</groupId> + <artifactId>metrics-core</artifactId> + <version>${metrics.version}</version> + </dependency> + <!--<dependency>--> + <!--<groupId>com.codahale.metrics</groupId>--> + <!--<artifactId>metrics-jvm</artifactId>--> + <!--<version>${metrics.version}</version>--> + <!--</dependency>--> + <dependency> + <groupId>org.apache.mina</groupId> + <artifactId>mina-core</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <scope>test</scope> + <version>1.7.5</version> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + <version>1.7.5</version> + <scope>test</scope> + </dependency> + </dependencies> +</project> + http://git-wip-us.apache.org/repos/asf/mina/blob/44f87608/monitoring/src/main/java/org/apache/mina/monitoring/MonitoringFilter.java ---------------------------------------------------------------------- diff --git a/monitoring/src/main/java/org/apache/mina/monitoring/MonitoringFilter.java b/monitoring/src/main/java/org/apache/mina/monitoring/MonitoringFilter.java new file mode 100644 index 0000000..f6a81c6 --- /dev/null +++ b/monitoring/src/main/java/org/apache/mina/monitoring/MonitoringFilter.java @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.mina.monitoring; + +import com.codahale.metrics.Counter; +import com.codahale.metrics.MetricRegistry; +import org.apache.mina.api.AbstractIoFilter; +import org.apache.mina.api.IdleStatus; +import org.apache.mina.api.IoSession; +import org.apache.mina.filterchain.ReadFilterChainController; +import org.apache.mina.filterchain.WriteFilterChainController; +import org.apache.mina.session.WriteRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.nio.ByteBuffer; + +/** + * Monitoring Filter. Captures the basic events. It needs to be the first Filter in the Chain + * + * MonitoringFilter -> Custom Filter/codec -> IoHandler + */ +public class MonitoringFilter extends AbstractIoFilter { + public static final Logger LOG = LoggerFactory.getLogger(MonitoringFilter.class); + + // Counter to monitor Events + private final Counter sessionOpenedCounter; + private final Counter sessionClosedCounter; + private final Counter sessionIdleCounter; + private final Counter rawMessagesReceivedCounter; + private final Counter messagesSentCounter; + private final Counter bytesIn; + private final Counter bytesOut; + + // Metrics Resgistry instance + private MetricRegistry metricRegistry; + + public MonitoringFilter(MetricRegistry metricRegistry) { + this.metricRegistry = metricRegistry; + sessionOpenedCounter = this.metricRegistry.counter("Sessions Opened"); + sessionClosedCounter = this.metricRegistry.counter("Sessions Closed"); + sessionIdleCounter = this.metricRegistry.counter("Sessions Idle"); + rawMessagesReceivedCounter = this.metricRegistry.counter("Messages Received"); + messagesSentCounter = this.metricRegistry.counter("Messages Sent"); + bytesIn = this.metricRegistry.counter("Bytes In"); + bytesOut = this.metricRegistry.counter("Bytes Out"); + } + + @Override + public void sessionOpened(IoSession session) { + sessionOpenedCounter.inc(); + super.sessionOpened(session); + } + + @Override + public void sessionClosed(IoSession session) { + sessionClosedCounter.inc(); + super.sessionClosed(session); + } + + @Override + public void sessionIdle(IoSession session, IdleStatus status) { + sessionIdleCounter.inc(); + super.sessionIdle(session, status); + } + + @Override + public void messageReceived(IoSession session, Object message, ReadFilterChainController controller) { + rawMessagesReceivedCounter.inc(); + if(message instanceof ByteBuffer) { + bytesIn.inc(((ByteBuffer)message).remaining()); + } + super.messageReceived(session, message, controller); + } + + @Override + public void messageWriting(IoSession session, WriteRequest message, WriteFilterChainController controller) { + if(message.getMessage() instanceof ByteBuffer) { + bytesOut.inc(((ByteBuffer)message.getMessage()).remaining()); + } + super.messageWriting(session, message, controller); + } + + @Override + public void messageSent(IoSession session, Object message) { + messagesSentCounter.inc(); + super.messageSent(session, message); + } + + +} http://git-wip-us.apache.org/repos/asf/mina/blob/44f87608/monitoring/src/test/java/org/apache/mina/monitoring/NioTcpEchoServerWithMonitoring.java ---------------------------------------------------------------------- diff --git a/monitoring/src/test/java/org/apache/mina/monitoring/NioTcpEchoServerWithMonitoring.java b/monitoring/src/test/java/org/apache/mina/monitoring/NioTcpEchoServerWithMonitoring.java new file mode 100644 index 0000000..6b9c431 --- /dev/null +++ b/monitoring/src/test/java/org/apache/mina/monitoring/NioTcpEchoServerWithMonitoring.java @@ -0,0 +1,131 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.apache.mina.monitoring; + +import com.codahale.metrics.JmxReporter; +import com.codahale.metrics.MetricRegistry; +import org.apache.mina.api.AbstractIoHandler; +import org.apache.mina.api.IdleStatus; +import org.apache.mina.api.IoFilter; +import org.apache.mina.api.IoSession; +import org.apache.mina.filter.logging.LoggingFilter; +import org.apache.mina.filterchain.ReadFilterChainController; +import org.apache.mina.filterchain.WriteFilterChainController; +import org.apache.mina.session.WriteRequest; +import org.apache.mina.transport.nio.NioTcpServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.nio.ByteBuffer; + +/** + * + */ +public class NioTcpEchoServerWithMonitoring { + + static final Logger LOG = LoggerFactory.getLogger(NioTcpEchoServerWithMonitoring.class); + + public static void main(final String[] args) { + LOG.info("starting echo server"); + + final NioTcpServer server = new NioTcpServer(); + final MetricRegistry metrics = new MetricRegistry(); + final JmxReporter reporter = JmxReporter.forRegistry(metrics).build(); + reporter.start(); + server.getSessionConfig().setIdleTimeInMillis(IdleStatus.READ_IDLE, 60*600*1000); + server.getSessionConfig().setIdleTimeInMillis(IdleStatus.WRITE_IDLE, 60*600*1000); + + // create the filter chain for this service + server.setFilters(new MonitoringFilter(metrics), new LoggingFilter("LoggingFilter1"), new IoFilter() { + + @Override + public void sessionOpened(final IoSession session) { + LOG.info("session {} open", session); + } + + @Override + public void sessionIdle(final IoSession session, final IdleStatus status) { + LOG.info("session {} idle", session); + } + + @Override + public void sessionClosed(final IoSession session) { + LOG.info("session {} open", session); + } + + @Override + public void messageWriting(final IoSession session, WriteRequest message, + final WriteFilterChainController controller) { + // we just push the message in the chain + controller.callWriteNextFilter(message); + } + + @Override + public void messageReceived(final IoSession session, final Object message, + final ReadFilterChainController controller) { + + if (message instanceof ByteBuffer) { + LOG.info("echoing"); + session.write(message); + } + } + + @Override + public void messageSent(final IoSession session, final Object message) { + LOG.info("message {} sent", message); + } + }); + server.setIoHandler(new AbstractIoHandler() { + @Override + public void sessionOpened(final IoSession session) { + LOG.info("session opened {}", session); + + final String welcomeStr = "welcome\n"; + final ByteBuffer bf = ByteBuffer.allocate(welcomeStr.length()); + bf.put(welcomeStr.getBytes()); + bf.flip(); + session.write(bf); + + } + + @Override + public void exceptionCaught(IoSession session, Exception cause) { + cause.printStackTrace(); + } + }); + + try { + final SocketAddress address = new InetSocketAddress(51000); + server.bind(address); + LOG.debug("Running the server for 25 sec"); + Thread.sleep(25000 * 1000); + LOG.debug("Unbinding the UDP port"); + server.unbind(); + } catch (final InterruptedException e) { + LOG.error("Interrupted exception", e); + } finally { + reporter.stop(); + } + } + +} http://git-wip-us.apache.org/repos/asf/mina/blob/44f87608/monitoring/src/test/resources/log4j.properties ---------------------------------------------------------------------- diff --git a/monitoring/src/test/resources/log4j.properties b/monitoring/src/test/resources/log4j.properties new file mode 100644 index 0000000..0c442cf --- /dev/null +++ b/monitoring/src/test/resources/log4j.properties @@ -0,0 +1,9 @@ +# Set root logger level to DEBUG and its only appender to A1. +log4j.rootLogger=INFO, A1 + +# A1 is set to be a ConsoleAppender. +log4j.appender.A1=org.apache.log4j.ConsoleAppender + +# A1 uses PatternLayout. +log4j.appender.A1.layout=org.apache.log4j.PatternLayout +log4j.appender.A1.layout.ConversionPattern=[%d{dd MMM yyyy HH:mm:ss,SSS}] [%t] %-5p %c %x - %m%n \ No newline at end of file http://git-wip-us.apache.org/repos/asf/mina/blob/44f87608/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index d9c0db0..ad01a41 100644 --- a/pom.xml +++ b/pom.xml @@ -118,6 +118,7 @@ <module>benchmarks</module> <module>benchmarks2</module> <module>avro</module> + <module>monitoring</module> </modules> <dependencyManagement>
