From ec951738b52643be4656be83474ea155ee7d8e14 Mon Sep 17 00:00:00 2001 From: andsel Date: Tue, 20 Jun 2017 22:12:42 +0200 Subject: [PATCH] Added BugSnag error tracker --- broker/pom.xml | 16 +++++--- .../java/io/moquette/BrokerConstants.java | 4 ++ .../server/netty/BugSnagErrorsHandler.java | 39 +++++++++++++++++++ .../moquette/server/netty/NettyAcceptor.java | 9 +++++ .../io/moquette/spi/ClientSessionTest.java | 5 ++- distribution/src/main/resources/moquette.conf | 8 ++++ 6 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 broker/src/main/java/io/moquette/server/netty/BugSnagErrorsHandler.java diff --git a/broker/pom.xml b/broker/pom.xml index 5adad2b1..e7ba66d8 100644 --- a/broker/pom.xml +++ b/broker/pom.xml @@ -100,17 +100,23 @@ 5.1.0 + + com.bugsnag + bugsnag + [3.0,4.0) + + commons-codec commons-codec 1.10 - - org.hdrhistogram - HdrHistogram - 2.1.4 - + + + + + com.hazelcast diff --git a/broker/src/main/java/io/moquette/BrokerConstants.java b/broker/src/main/java/io/moquette/BrokerConstants.java index c29c0149..72686f8a 100644 --- a/broker/src/main/java/io/moquette/BrokerConstants.java +++ b/broker/src/main/java/io/moquette/BrokerConstants.java @@ -61,6 +61,10 @@ public final class BrokerConstants { public static final String METRICS_LIBRATO_EMAIL_PROPERTY_NAME = "metrics.librato.email"; public static final String METRICS_LIBRATO_TOKEN_PROPERTY_NAME = "metrics.librato.token"; public static final String METRICS_LIBRATO_SOURCE_PROPERTY_NAME = "metrics.librato.source"; + + public static final String BUGSNAG_ENABLE_PROPERTY_NAME = "use_bugsnag"; + public static final String BUGSNAG_TOKEN_PROPERTY_NAME = "bugsnag.token"; + public static final String STORAGE_CLASS_NAME = "storage_class"; private BrokerConstants() { diff --git a/broker/src/main/java/io/moquette/server/netty/BugSnagErrorsHandler.java b/broker/src/main/java/io/moquette/server/netty/BugSnagErrorsHandler.java new file mode 100644 index 00000000..3dd8d5dd --- /dev/null +++ b/broker/src/main/java/io/moquette/server/netty/BugSnagErrorsHandler.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2012-2017 The original author or authors + * ------------------------------------------------------ + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * The Apache License v2.0 is available at + * http://www.opensource.org/licenses/apache2.0.php + * + * You may elect to redistribute this code under either of these licenses. + */ +package io.moquette.server.netty; + +import com.bugsnag.Bugsnag; +import io.moquette.server.config.IConfig; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; + +import static io.moquette.BrokerConstants.BUGSNAG_TOKEN_PROPERTY_NAME; + +public class BugSnagErrorsHandler extends ChannelInboundHandlerAdapter { + + private Bugsnag bugsnag; + + public void init(IConfig props) { + final String token = props.getProperty(BUGSNAG_TOKEN_PROPERTY_NAME); + this.bugsnag = new Bugsnag(token); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + bugsnag.notify(cause); + ctx.fireExceptionCaught(cause); + } +} diff --git a/broker/src/main/java/io/moquette/server/netty/NettyAcceptor.java b/broker/src/main/java/io/moquette/server/netty/NettyAcceptor.java index 4da62609..6952cb35 100644 --- a/broker/src/main/java/io/moquette/server/netty/NettyAcceptor.java +++ b/broker/src/main/java/io/moquette/server/netty/NettyAcceptor.java @@ -97,6 +97,7 @@ public class NettyAcceptor implements ServerAcceptor { BytesMetricsCollector m_bytesMetricsCollector = new BytesMetricsCollector(); MessageMetricsCollector m_metricsCollector = new MessageMetricsCollector(); private Optional metrics; + private Optional errorsCather; private int nettySoBacklog; private boolean nettySoReuseaddr; @@ -145,6 +146,14 @@ public class NettyAcceptor implements ServerAcceptor { this.metrics = Optional.empty(); } + final boolean useBugSnag = Boolean.parseBoolean(props.getProperty(BUGSNAG_ENABLE_PROPERTY_NAME, "false")); + if (useBugSnag) { + BugSnagErrorsHandler bugSnagHandler = new BugSnagErrorsHandler(); + bugSnagHandler.init(props); + this.errorsCather = Optional.of(bugSnagHandler); + } else { + this.errorsCather = Optional.empty(); + } initializePlainTCPTransport(mqttHandler, props); initializeWebSocketTransport(mqttHandler, props); String sslTcpPortProp = props.getProperty(BrokerConstants.SSL_PORT_PROPERTY_NAME); diff --git a/broker/src/test/java/io/moquette/spi/ClientSessionTest.java b/broker/src/test/java/io/moquette/spi/ClientSessionTest.java index aa43eb92..0d895b15 100644 --- a/broker/src/test/java/io/moquette/spi/ClientSessionTest.java +++ b/broker/src/test/java/io/moquette/spi/ClientSessionTest.java @@ -20,10 +20,13 @@ import io.moquette.persistence.MemoryStorageService; import io.moquette.spi.impl.subscriptions.Subscription; import io.moquette.spi.impl.subscriptions.SubscriptionsDirectory; import io.moquette.spi.impl.subscriptions.Topic; -import static io.netty.handler.codec.mqtt.MqttQoS.*; import org.junit.Before; import org.junit.Test; + import java.util.List; + +import static io.netty.handler.codec.mqtt.MqttQoS.AT_MOST_ONCE; +import static io.netty.handler.codec.mqtt.MqttQoS.EXACTLY_ONCE; import static org.junit.Assert.assertEquals; public class ClientSessionTest { diff --git a/distribution/src/main/resources/moquette.conf b/distribution/src/main/resources/moquette.conf index 72aca208..c257eb83 100644 --- a/distribution/src/main/resources/moquette.conf +++ b/distribution/src/main/resources/moquette.conf @@ -126,3 +126,11 @@ password_file config/password_file.conf # metrics.librato.token sdfergheghliuhyr2283ehd9827398h # metrics.librato.source My Fantastic Service +#********************************************************************* +# Error Monitoring +# +# use_bugsnag: used to enable BugSnag error handler catcher. +# bugsnag.token: the token from your's bugsnag account +#********************************************************************* +# use_bugsnag false +# bugsnag.token wleifb8723784dbfeig74