This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 33e9b7010b49d7ce64f6d56d590149d96abe4e09 Author: Benoit Tellier <[email protected]> AuthorDate: Tue Feb 23 15:51:07 2021 +0700 JAMES-3504 Register metrics for POP3 commands --- protocols/pop3/pom.xml | 9 +++++++ .../pop3/core/AbstractApopCmdHandler.java | 8 ++++++ .../pop3/core/AbstractPassCmdHandler.java | 13 ++++++++-- .../james/protocols/pop3/core/CapaCmdHandler.java | 19 +++++++++++--- .../james/protocols/pop3/core/DeleCmdHandler.java | 21 +++++++++++---- .../james/protocols/pop3/core/ListCmdHandler.java | 21 +++++++++++---- .../james/protocols/pop3/core/NoopCmdHandler.java | 21 +++++++++++---- .../james/protocols/pop3/core/QuitCmdHandler.java | 21 +++++++++++---- .../james/protocols/pop3/core/RetrCmdHandler.java | 23 ++++++++++++----- .../james/protocols/pop3/core/RsetCmdHandler.java | 22 +++++++++++----- .../james/protocols/pop3/core/StatCmdHandler.java | 21 +++++++++++---- .../james/protocols/pop3/core/StlsCmdHandler.java | 21 +++++++++++---- .../james/protocols/pop3/core/TopCmdHandler.java | 24 ++++++++++++----- .../james/protocols/pop3/core/UidlCmdHandler.java | 23 ++++++++++++----- .../james/protocols/pop3/core/UserCmdHandler.java | 23 ++++++++++++----- .../protocols/pop3/AbstractPOP3ServerTest.java | 30 +++++++++++++--------- .../pop3/AbstractStartTlsPOP3ServerTest.java | 3 ++- .../protocols/pop3/POP3ProtocolHandlerChain.java | 27 ++++++++++--------- .../protocols/pop3/core/RetrCmdHandlerTest.java | 5 ++-- .../protocols/pop3/utils/TestPassCmdHandler.java | 7 ++++- .../james/pop3server/core/PassCmdHandler.java | 4 ++- .../apache/james/pop3server/POP3ServerTest.java | 3 +++ 22 files changed, 274 insertions(+), 95 deletions(-) diff --git a/protocols/pop3/pom.xml b/protocols/pop3/pom.xml index f77786e..784c795 100644 --- a/protocols/pop3/pom.xml +++ b/protocols/pop3/pom.xml @@ -39,6 +39,15 @@ </dependency> <dependency> <groupId>${james.groupId}</groupId> + <artifactId>metrics-api</artifactId> + </dependency> + <dependency> + <groupId>${james.groupId}</groupId> + <artifactId>metrics-tests</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>${james.groupId}</groupId> <artifactId>testing-base</artifactId> <scope>test</scope> </dependency> diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractApopCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractApopCmdHandler.java index 521c1df..06af37e 100644 --- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractApopCmdHandler.java +++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractApopCmdHandler.java @@ -21,7 +21,10 @@ package org.apache.james.protocols.pop3.core; import java.util.Collection; +import javax.inject.Inject; + import org.apache.james.core.Username; +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.protocols.api.ProtocolSession.State; import org.apache.james.protocols.api.Request; import org.apache.james.protocols.api.Response; @@ -39,6 +42,11 @@ public abstract class AbstractApopCmdHandler extends AbstractPassCmdHandler { private static final Collection<String> COMMANDS = ImmutableSet.of("APOP"); private static final String MISSING_APOP_TIMESTAMP = ""; + @Inject + public AbstractApopCmdHandler(MetricFactory metricFactory) { + super(metricFactory); + } + @Override public Response onCommand(POP3Session session, Request request) { if (!session.getAttachment(POP3Session.APOP_TIMESTAMP, State.Connection).isPresent()) { diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractPassCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractPassCmdHandler.java index 8b1a48e..ba70287 100644 --- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractPassCmdHandler.java +++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractPassCmdHandler.java @@ -23,6 +23,7 @@ import java.util.Collection; import java.util.Optional; import org.apache.james.core.Username; +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.protocols.api.Request; import org.apache.james.protocols.api.Response; import org.apache.james.protocols.pop3.POP3Response; @@ -43,17 +44,25 @@ public abstract class AbstractPassCmdHandler extends RsetCmdHandler { private static final Response UNEXPECTED_ERROR = new POP3Response(POP3Response.ERR_RESPONSE, "Unexpected error accessing mailbox").immutable(); protected static final Response AUTH_FAILED = new POP3Response(POP3Response.ERR_RESPONSE, "Authentication failed.").immutable(); + private final MetricFactory metricFactory; + + public AbstractPassCmdHandler(MetricFactory metricFactory) { + super(metricFactory); + this.metricFactory = metricFactory; + } + /** * Handler method called upon receipt of a PASS command. Reads in and * validates the password. */ @Override public Response onCommand(POP3Session session, Request request) { - return MDCBuilder.withMdc( + return metricFactory.decorateSupplierWithTimerMetric("pop3-pass", () -> + MDCBuilder.withMdc( MDCBuilder.create() .addContext(MDCBuilder.ACTION, "AUTH") .addContext(MDCConstants.withSession(session)), - () -> doAuth(session, request)); + () -> doAuth(session, request))); } private Response doAuth(POP3Session session, Request request) { diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/CapaCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/CapaCmdHandler.java index b902f6e..8c93abc 100644 --- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/CapaCmdHandler.java +++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/CapaCmdHandler.java @@ -24,6 +24,9 @@ import java.util.Collection; import java.util.List; import java.util.Set; +import javax.inject.Inject; + +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.protocols.api.Request; import org.apache.james.protocols.api.Response; import org.apache.james.protocols.api.handler.CommandHandler; @@ -43,12 +46,20 @@ public class CapaCmdHandler implements CommandHandler<POP3Session>, ExtensibleHa private static final Collection<String> COMMANDS = ImmutableSet.of("CAPA"); private static final Set<String> CAPS = ImmutableSet.of("PIPELINING"); + private final MetricFactory metricFactory; + + @Inject + public CapaCmdHandler(MetricFactory metricFactory) { + this.metricFactory = metricFactory; + } + @Override public Response onCommand(POP3Session session, Request request) { - return MDCBuilder.withMdc(MDCBuilder.create() - .addContext(MDCBuilder.ACTION, "CAPA") - .addContext(MDCConstants.withSession(session)), - () -> capa(session)); + return metricFactory.decorateSupplierWithTimerMetric("pop3-capa", () -> + MDCBuilder.withMdc(MDCBuilder.create() + .addContext(MDCBuilder.ACTION, "CAPA") + .addContext(MDCConstants.withSession(session)), + () -> capa(session))); } private Response capa(POP3Session session) { diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/DeleCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/DeleCmdHandler.java index d32624a..cdc0771 100644 --- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/DeleCmdHandler.java +++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/DeleCmdHandler.java @@ -23,6 +23,9 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import javax.inject.Inject; + +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.protocols.api.ProtocolSession.State; import org.apache.james.protocols.api.Request; import org.apache.james.protocols.api.Response; @@ -43,6 +46,13 @@ public class DeleCmdHandler implements CommandHandler<POP3Session> { private static final Response SYNTAX_ERROR = new POP3Response(POP3Response.ERR_RESPONSE, "Usage: DELE [mail number]").immutable(); private static final Response DELETED = new POP3Response(POP3Response.OK_RESPONSE, "Message deleted").immutable(); + private final MetricFactory metricFactory; + + @Inject + public DeleCmdHandler(MetricFactory metricFactory) { + this.metricFactory = metricFactory; + } + /** * Handler method called upon receipt of a DELE command. This command * deletes a particular mail message from the mailbox. @@ -50,11 +60,12 @@ public class DeleCmdHandler implements CommandHandler<POP3Session> { @Override @SuppressWarnings("unchecked") public Response onCommand(POP3Session session, Request request) { - return MDCBuilder.withMdc(MDCBuilder.create() - .addContext(MDCBuilder.ACTION, "DELE") - .addContext(MDCConstants.withSession(session)) - .addContext(MDCConstants.forRequest(request)), - () -> delete(session, request)); + return metricFactory.decorateSupplierWithTimerMetric("pop3-dele", () -> + MDCBuilder.withMdc(MDCBuilder.create() + .addContext(MDCBuilder.ACTION, "DELE") + .addContext(MDCConstants.withSession(session)) + .addContext(MDCConstants.forRequest(request)), + () -> delete(session, request))); } private Response delete(POP3Session session, Request request) { diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/ListCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/ListCmdHandler.java index 4c16b54..60649de 100644 --- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/ListCmdHandler.java +++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/ListCmdHandler.java @@ -23,6 +23,9 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import javax.inject.Inject; + +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.protocols.api.ProtocolSession.State; import org.apache.james.protocols.api.Request; import org.apache.james.protocols.api.Response; @@ -42,6 +45,13 @@ public class ListCmdHandler implements CommandHandler<POP3Session> { private static final Collection<String> COMMANDS = ImmutableSet.of("LIST"); + private final MetricFactory metricFactory; + + @Inject + public ListCmdHandler(MetricFactory metricFactory) { + this.metricFactory = metricFactory; + } + /** * Handler method called upon receipt of a LIST command. Returns the number * of messages in the mailbox and its aggregate size, or optionally, the @@ -56,11 +66,12 @@ public class ListCmdHandler implements CommandHandler<POP3Session> { @Override @SuppressWarnings("unchecked") public Response onCommand(POP3Session session, Request request) { - return MDCBuilder.withMdc(MDCBuilder.create() - .addContext(MDCBuilder.ACTION, "LIST") - .addContext(MDCConstants.withSession(session)) - .addContext(MDCConstants.forRequest(request)), - () -> list(session, request)); + return metricFactory.decorateSupplierWithTimerMetric("pop3-list", () -> + MDCBuilder.withMdc(MDCBuilder.create() + .addContext(MDCBuilder.ACTION, "LIST") + .addContext(MDCConstants.withSession(session)) + .addContext(MDCConstants.forRequest(request)), + () -> list(session, request))); } private Response list(POP3Session session, Request request) { diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/NoopCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/NoopCmdHandler.java index 0796771..4bdc7b5 100644 --- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/NoopCmdHandler.java +++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/NoopCmdHandler.java @@ -21,6 +21,9 @@ package org.apache.james.protocols.pop3.core; import java.util.Collection; +import javax.inject.Inject; + +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.protocols.api.Request; import org.apache.james.protocols.api.Response; import org.apache.james.protocols.api.handler.CommandHandler; @@ -39,17 +42,25 @@ public class NoopCmdHandler implements CommandHandler<POP3Session> { private static final Logger LOGGER = LoggerFactory.getLogger(NoopCmdHandler.class); private static final Collection<String> COMMANDS = ImmutableSet.of("NOOP"); + private final MetricFactory metricFactory; + + @Inject + public NoopCmdHandler(MetricFactory metricFactory) { + this.metricFactory = metricFactory; + } + /** * Handler method called upon receipt of a NOOP command. Like all good * NOOPs, does nothing much. */ @Override public Response onCommand(POP3Session session, Request request) { - return MDCBuilder.withMdc( - MDCBuilder.create() - .addContext(MDCBuilder.ACTION, "NOOP") - .addContext(MDCConstants.withSession(session)), - () -> noop(session)); + return metricFactory.decorateSupplierWithTimerMetric("pop3-noop", () -> + MDCBuilder.withMdc( + MDCBuilder.create() + .addContext(MDCBuilder.ACTION, "NOOP") + .addContext(MDCConstants.withSession(session)), + () -> noop(session))); } private Response noop(POP3Session session) { diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/QuitCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/QuitCmdHandler.java index 248cd4d..902ae72 100644 --- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/QuitCmdHandler.java +++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/QuitCmdHandler.java @@ -23,6 +23,9 @@ import java.io.IOException; import java.util.Collection; import java.util.List; +import javax.inject.Inject; + +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.protocols.api.ProtocolSession.State; import org.apache.james.protocols.api.Request; import org.apache.james.protocols.api.Response; @@ -56,17 +59,25 @@ public class QuitCmdHandler implements CommandHandler<POP3Session> { SIGN_OFF_NOT_CLEAN = response.immutable(); } + private final MetricFactory metricFactory; + + @Inject + public QuitCmdHandler(MetricFactory metricFactory) { + this.metricFactory = metricFactory; + } + /** * Handler method called upon receipt of a QUIT command. This method handles * cleanup of the POP3Handler state. */ @Override public Response onCommand(POP3Session session, Request request) { - return MDCBuilder.withMdc( - MDCBuilder.create() - .addContext(MDCBuilder.ACTION, "QUIT") - .addContext(MDCConstants.withSession(session)), - () -> quit(session)); + return metricFactory.decorateSupplierWithTimerMetric("pop3-quit", () -> + MDCBuilder.withMdc( + MDCBuilder.create() + .addContext(MDCBuilder.ACTION, "QUIT") + .addContext(MDCConstants.withSession(session)), + () -> quit(session))); } private Response quit(POP3Session session) { diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RetrCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RetrCmdHandler.java index 8544741..fc05faa 100644 --- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RetrCmdHandler.java +++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RetrCmdHandler.java @@ -24,6 +24,9 @@ import java.io.InputStream; import java.util.Collection; import java.util.List; +import javax.inject.Inject; + +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.protocols.api.ProtocolSession.State; import org.apache.james.protocols.api.Request; import org.apache.james.protocols.api.Response; @@ -50,6 +53,13 @@ public class RetrCmdHandler implements CommandHandler<POP3Session> { static final Response SYNTAX_ERROR = new POP3Response(POP3Response.ERR_RESPONSE, "Usage: RETR [mail number]").immutable(); private static final Response ERROR_MESSAGE_RETRIEVE = new POP3Response(POP3Response.ERR_RESPONSE, "Error while retrieving message.").immutable(); + private final MetricFactory metricFactory; + + @Inject + public RetrCmdHandler(MetricFactory metricFactory) { + this.metricFactory = metricFactory; + } + /** * Handler method called upon receipt of a RETR command. This command * retrieves a particular mail message from the mailbox. @@ -57,12 +67,13 @@ public class RetrCmdHandler implements CommandHandler<POP3Session> { @Override @SuppressWarnings("unchecked") public Response onCommand(POP3Session session, Request request) { - return MDCBuilder.withMdc( - MDCBuilder.create() - .addContext(MDCBuilder.ACTION, "RETR") - .addContext(MDCConstants.withSession(session)) - .addContext(MDCConstants.forRequest(request)), - () -> retr(session, request)); + return metricFactory.decorateSupplierWithTimerMetric("pop3-retr", () -> + MDCBuilder.withMdc( + MDCBuilder.create() + .addContext(MDCBuilder.ACTION, "RETR") + .addContext(MDCConstants.withSession(session)) + .addContext(MDCConstants.forRequest(request)), + () -> retr(session, request))); } private Response retr(POP3Session session, Request request) { diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RsetCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RsetCmdHandler.java index b74e4ec..003e0d0 100644 --- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RsetCmdHandler.java +++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RsetCmdHandler.java @@ -24,6 +24,9 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import javax.inject.Inject; + +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.protocols.api.ProtocolSession.State; import org.apache.james.protocols.api.Request; import org.apache.james.protocols.api.Response; @@ -44,18 +47,25 @@ public class RsetCmdHandler implements CommandHandler<POP3Session> { private static final Collection<String> COMMANDS = ImmutableSet.of("RSET"); private static final Logger LOGGER = LoggerFactory.getLogger(RsetCmdHandler.class); + private final MetricFactory metricFactory; + + @Inject + public RsetCmdHandler(MetricFactory metricFactory) { + this.metricFactory = metricFactory; + } + /** * Handler method called upon receipt of a RSET command. Calls stat() to * reset the mailbox. */ @Override public Response onCommand(POP3Session session, Request request) { - return MDCBuilder.withMdc( - MDCBuilder.create() - .addContext(MDCBuilder.ACTION, "RSET") - .addContext(MDCConstants.withSession(session)), - () -> rset(session)); - + return metricFactory.decorateSupplierWithTimerMetric("pop3-rset", () -> + MDCBuilder.withMdc( + MDCBuilder.create() + .addContext(MDCBuilder.ACTION, "RSET") + .addContext(MDCConstants.withSession(session)), + () -> rset(session))); } private Response rset(POP3Session session) { diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/StatCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/StatCmdHandler.java index 09b3bdc..c2a3e83 100644 --- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/StatCmdHandler.java +++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/StatCmdHandler.java @@ -23,6 +23,9 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import javax.inject.Inject; + +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.protocols.api.ProtocolSession.State; import org.apache.james.protocols.api.Request; import org.apache.james.protocols.api.Response; @@ -44,17 +47,25 @@ public class StatCmdHandler implements CommandHandler<POP3Session> { private static final Logger LOGGER = LoggerFactory.getLogger(StatCmdHandler.class); private static final Collection<String> COMMANDS = ImmutableSet.of("STAT"); + private final MetricFactory metricFactory; + + @Inject + public StatCmdHandler(MetricFactory metricFactory) { + this.metricFactory = metricFactory; + } + /** * Handler method called upon receipt of a STAT command. Returns the number * of messages in the mailbox and its aggregate size. */ @Override public Response onCommand(POP3Session session, Request request) { - return MDCBuilder.withMdc( - MDCBuilder.create() - .addContext(MDCBuilder.ACTION, "STAT") - .addContext(MDCConstants.withSession(session)), - () -> stat(session)); + return metricFactory.decorateSupplierWithTimerMetric("pop3-stat", () -> + MDCBuilder.withMdc( + MDCBuilder.create() + .addContext(MDCBuilder.ACTION, "STAT") + .addContext(MDCConstants.withSession(session)), + () -> stat(session))); } private Response stat(POP3Session session) { diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/StlsCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/StlsCmdHandler.java index c242172..7e5ab10 100644 --- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/StlsCmdHandler.java +++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/StlsCmdHandler.java @@ -23,6 +23,9 @@ import java.util.Collection; import java.util.Collections; import java.util.Set; +import javax.inject.Inject; + +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.protocols.api.Request; import org.apache.james.protocols.api.Response; import org.apache.james.protocols.api.handler.CommandHandler; @@ -46,13 +49,21 @@ public class StlsCmdHandler implements CommandHandler<POP3Session>, CapaCapabili private static final Response BEGIN_TLS = new POP3StartTlsResponse(POP3Response.OK_RESPONSE, "Begin TLS negotiation").immutable(); + private final MetricFactory metricFactory; + + @Inject + public StlsCmdHandler(MetricFactory metricFactory) { + this.metricFactory = metricFactory; + } + @Override public Response onCommand(POP3Session session, Request request) { - return MDCBuilder.withMdc( - MDCBuilder.create() - .addContext(MDCBuilder.ACTION, "START_TLS") - .addContext(MDCConstants.withSession(session)), - () -> stls(session)); + return metricFactory.decorateSupplierWithTimerMetric("pop3-stls", () -> + MDCBuilder.withMdc( + MDCBuilder.create() + .addContext(MDCBuilder.ACTION, "START_TLS") + .addContext(MDCConstants.withSession(session)), + () -> stls(session))); } private Response stls(POP3Session session) { diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/TopCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/TopCmdHandler.java index 7351b20..f7d8a52 100644 --- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/TopCmdHandler.java +++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/TopCmdHandler.java @@ -27,6 +27,9 @@ import java.util.List; import java.util.NoSuchElementException; import java.util.Set; +import javax.inject.Inject; + +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.protocols.api.ProtocolSession.State; import org.apache.james.protocols.api.Request; import org.apache.james.protocols.api.Response; @@ -52,6 +55,14 @@ public class TopCmdHandler extends RetrCmdHandler implements CapaCapability { private static final Response SYNTAX_ERROR = new POP3Response(POP3Response.ERR_RESPONSE, "Usage: TOP [mail number] [Line number]").immutable(); private static final Response ERROR_MESSAGE_RETR = new POP3Response(POP3Response.ERR_RESPONSE, "Error while retrieving message.").immutable(); + private final MetricFactory metricFactory; + + @Inject + public TopCmdHandler(MetricFactory metricFactory) { + super(metricFactory); + this.metricFactory = metricFactory; + } + /** * Handler method called upon receipt of a TOP command. This command * retrieves the top N lines of a specified message in the mailbox. @@ -62,12 +73,13 @@ public class TopCmdHandler extends RetrCmdHandler implements CapaCapability { @SuppressWarnings("unchecked") @Override public Response onCommand(POP3Session session, Request request) { - return MDCBuilder.withMdc( - MDCBuilder.create() - .addContext(MDCBuilder.ACTION, "TOP") - .addContext(MDCConstants.withSession(session)) - .addContext(MDCConstants.forRequest(request)), - () -> top(session, request)); + return metricFactory.decorateSupplierWithTimerMetric("pop3-top", () -> + MDCBuilder.withMdc( + MDCBuilder.create() + .addContext(MDCBuilder.ACTION, "TOP") + .addContext(MDCConstants.withSession(session)) + .addContext(MDCConstants.forRequest(request)), + () -> top(session, request))); } private Response top(POP3Session session, Request request) { diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UidlCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UidlCmdHandler.java index ef3aa8f..feee247 100644 --- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UidlCmdHandler.java +++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UidlCmdHandler.java @@ -25,6 +25,9 @@ import java.util.Collections; import java.util.List; import java.util.Set; +import javax.inject.Inject; + +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.protocols.api.ProtocolSession.State; import org.apache.james.protocols.api.Request; import org.apache.james.protocols.api.Response; @@ -47,18 +50,26 @@ public class UidlCmdHandler implements CommandHandler<POP3Session>, CapaCapabili private static final Collection<String> COMMANDS = ImmutableSet.of("UIDL"); private static final Set<String> CAPS = ImmutableSet.of("UIDL"); + private final MetricFactory metricFactory; + + @Inject + public UidlCmdHandler(MetricFactory metricFactory) { + this.metricFactory = metricFactory; + } + /** * Handler method called upon receipt of a UIDL command. Returns a listing * of message ids to the client. */ @Override public Response onCommand(POP3Session session, Request request) { - return MDCBuilder.withMdc( - MDCBuilder.create() - .addContext(MDCBuilder.ACTION, "UIDL") - .addContext(MDCConstants.withSession(session)) - .addContext(MDCConstants.forRequest(request)), - () -> uidl(session, request)); + return metricFactory.decorateSupplierWithTimerMetric("pop3-uidl", () -> + MDCBuilder.withMdc( + MDCBuilder.create() + .addContext(MDCBuilder.ACTION, "UIDL") + .addContext(MDCConstants.withSession(session)) + .addContext(MDCConstants.forRequest(request)), + () -> uidl(session, request))); } private Response uidl(POP3Session session, Request request) { diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UserCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UserCmdHandler.java index a08780f..5980485 100644 --- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UserCmdHandler.java +++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/UserCmdHandler.java @@ -22,7 +22,10 @@ package org.apache.james.protocols.pop3.core; import java.util.Collection; import java.util.Set; +import javax.inject.Inject; + import org.apache.james.core.Username; +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.protocols.api.Request; import org.apache.james.protocols.api.Response; import org.apache.james.protocols.api.handler.CommandHandler; @@ -42,18 +45,26 @@ public class UserCmdHandler implements CommandHandler<POP3Session>, CapaCapabili private static final Collection<String> COMMANDS = ImmutableSet.of("USER"); private static final Set<String> CAPS = ImmutableSet.of("USER"); + private final MetricFactory metricFactory; + + @Inject + public UserCmdHandler(MetricFactory metricFactory) { + this.metricFactory = metricFactory; + } + /** * Handler method called upon receipt of a USER command. Reads in the user * id. */ @Override public Response onCommand(POP3Session session, Request request) { - return MDCBuilder.withMdc( - MDCBuilder.create() - .addContext(MDCBuilder.ACTION, "USER") - .addContext(MDCConstants.withSession(session)) - .addContext(MDCConstants.forRequest(request)), - () -> user(session, request)); + return metricFactory.decorateSupplierWithTimerMetric("pop3-user", () -> + MDCBuilder.withMdc( + MDCBuilder.create() + .addContext(MDCBuilder.ACTION, "USER") + .addContext(MDCConstants.withSession(session)) + .addContext(MDCConstants.forRequest(request)), + () -> user(session, request))); } private Response user(POP3Session session, Request request) { diff --git a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractPOP3ServerTest.java b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractPOP3ServerTest.java index 4cbc395..a9b0212 100644 --- a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractPOP3ServerTest.java +++ b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractPOP3ServerTest.java @@ -32,6 +32,8 @@ import org.apache.commons.net.pop3.POP3Client; import org.apache.commons.net.pop3.POP3MessageInfo; import org.apache.commons.net.pop3.POP3Reply; import org.apache.james.core.Username; +import org.apache.james.metrics.api.MetricFactory; +import org.apache.james.metrics.tests.RecordingMetricFactory; import org.apache.james.protocols.api.Protocol; import org.apache.james.protocols.api.ProtocolServer; import org.apache.james.protocols.api.handler.WiringException; @@ -63,7 +65,7 @@ public abstract class AbstractPOP3ServerTest { public void testInvalidAuth() throws Exception { ProtocolServer server = null; try { - server = createServer(createProtocol(new TestPassCmdHandler())); + server = createServer(createProtocol(new TestPassCmdHandler(new RecordingMetricFactory()))); server.bind(); POP3Client client = createClient(); @@ -86,7 +88,7 @@ public abstract class AbstractPOP3ServerTest { ProtocolServer server = null; try { String identifier = "id"; - TestPassCmdHandler handler = new TestPassCmdHandler(); + TestPassCmdHandler handler = new TestPassCmdHandler(new RecordingMetricFactory()); handler.add("valid", new MockMailbox(identifier)); server = createServer(createProtocol(handler)); @@ -117,7 +119,7 @@ public abstract class AbstractPOP3ServerTest { ProtocolServer server = null; try { String identifier = "id"; - TestPassCmdHandler handler = new TestPassCmdHandler(); + TestPassCmdHandler handler = new TestPassCmdHandler(new RecordingMetricFactory()); handler.add("valid", new MockMailbox(identifier, MESSAGE1, MESSAGE2)); server = createServer(createProtocol(handler)); @@ -173,7 +175,7 @@ public abstract class AbstractPOP3ServerTest { ProtocolServer server = null; try { String identifier = "id"; - TestPassCmdHandler factory = new TestPassCmdHandler(); + TestPassCmdHandler factory = new TestPassCmdHandler(new RecordingMetricFactory()); factory.add("valid", new MockMailbox(identifier, MESSAGE1, MESSAGE2)); server = createServer(createProtocol(factory)); @@ -215,7 +217,7 @@ public abstract class AbstractPOP3ServerTest { ProtocolServer server = null; try { String identifier = "id"; - TestPassCmdHandler factory = new TestPassCmdHandler(); + TestPassCmdHandler factory = new TestPassCmdHandler(new RecordingMetricFactory()); factory.add("valid", new MockMailbox(identifier, MESSAGE1, MESSAGE2)); server = createServer(createProtocol(factory)); @@ -260,7 +262,7 @@ public abstract class AbstractPOP3ServerTest { ProtocolServer server = null; try { String identifier = "id"; - TestPassCmdHandler factory = new TestPassCmdHandler(); + TestPassCmdHandler factory = new TestPassCmdHandler(new RecordingMetricFactory()); factory.add("valid", new MockMailbox(identifier, MESSAGE1, MESSAGE2)); server = createServer(createProtocol(factory)); @@ -312,7 +314,7 @@ public abstract class AbstractPOP3ServerTest { ProtocolServer server = null; try { String identifier = "id"; - TestPassCmdHandler factory = new TestPassCmdHandler(); + TestPassCmdHandler factory = new TestPassCmdHandler(new RecordingMetricFactory()); factory.add("valid", new MockMailbox(identifier)); server = createServer(createProtocol(factory)); @@ -339,7 +341,7 @@ public abstract class AbstractPOP3ServerTest { ProtocolServer server = null; try { String identifier = "id"; - TestPassCmdHandler factory = new TestPassCmdHandler(); + TestPassCmdHandler factory = new TestPassCmdHandler(new RecordingMetricFactory()); factory.add("valid", new MockMailbox(identifier, MESSAGE1)); server = createServer(createProtocol(factory)); @@ -373,7 +375,7 @@ public abstract class AbstractPOP3ServerTest { ProtocolServer server = null; try { String identifier = "id"; - TestPassCmdHandler factory = new TestPassCmdHandler(); + TestPassCmdHandler factory = new TestPassCmdHandler(new RecordingMetricFactory()); factory.add("valid", new MockMailbox(identifier, MESSAGE1, MESSAGE2)); server = createServer(createProtocol(factory)); @@ -402,7 +404,7 @@ public abstract class AbstractPOP3ServerTest { ProtocolServer server = null; try { String identifier = "id"; - TestPassCmdHandler factory = new TestPassCmdHandler(); + TestPassCmdHandler factory = new TestPassCmdHandler(new RecordingMetricFactory()); factory.add("valid", new MockMailbox(identifier, MESSAGE1, MESSAGE2)); server = createServer(createProtocol(factory)); @@ -451,7 +453,7 @@ public abstract class AbstractPOP3ServerTest { public void testAPop() throws Exception { ProtocolServer server = null; try { - TestApopCmdHandler handler = new TestApopCmdHandler(); + TestApopCmdHandler handler = new TestApopCmdHandler(new RecordingMetricFactory()); server = createServer(createProtocol(handler)); server.bind(); @@ -515,7 +517,11 @@ public abstract class AbstractPOP3ServerTest { private final class TestApopCmdHandler extends AbstractApopCmdHandler { private final Map<String, Mailbox> mailboxes = new HashMap<>(); - + + public TestApopCmdHandler(MetricFactory metricFactory) { + super(metricFactory); + } + public void add(String username, Mailbox mailbox) { mailboxes.put(username, mailbox); } diff --git a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractStartTlsPOP3ServerTest.java b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractStartTlsPOP3ServerTest.java index 388404b..03c1aea 100644 --- a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractStartTlsPOP3ServerTest.java +++ b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/AbstractStartTlsPOP3ServerTest.java @@ -25,6 +25,7 @@ import java.util.Arrays; import org.apache.commons.net.pop3.POP3Reply; import org.apache.commons.net.pop3.POP3SClient; +import org.apache.james.metrics.tests.RecordingMetricFactory; import org.apache.james.protocols.api.Encryption; import org.apache.james.protocols.api.Protocol; import org.apache.james.protocols.api.ProtocolServer; @@ -62,7 +63,7 @@ public abstract class AbstractStartTlsPOP3ServerTest { ProtocolServer server = null; try { String identifier = "id"; - TestPassCmdHandler handler = new TestPassCmdHandler(); + TestPassCmdHandler handler = new TestPassCmdHandler(new RecordingMetricFactory()); handler.add("valid", new MockMailbox(identifier)); server = createServer(createProtocol(handler), address, Encryption.createStartTls(BogusSslContextFactory.getServerContext())); diff --git a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/POP3ProtocolHandlerChain.java b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/POP3ProtocolHandlerChain.java index 8035ff7..fa0e632 100644 --- a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/POP3ProtocolHandlerChain.java +++ b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/POP3ProtocolHandlerChain.java @@ -23,6 +23,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.apache.james.metrics.api.MetricFactory; +import org.apache.james.metrics.tests.RecordingMetricFactory; import org.apache.james.protocols.api.handler.CommandDispatcher; import org.apache.james.protocols.api.handler.CommandHandlerResultLogger; import org.apache.james.protocols.api.handler.ProtocolHandler; @@ -50,6 +52,7 @@ import org.apache.james.protocols.pop3.core.WelcomeMessageHandler; * */ public class POP3ProtocolHandlerChain extends ProtocolHandlerChainImpl { + private final MetricFactory metricFactory = new RecordingMetricFactory(); /** * The {@link AbstractPassCmdHandler}'s to use. If at least one {@link AbstractPassCmdHandler} is given, the {@link POP3ProtocolHandlerChain} @@ -67,20 +70,20 @@ public class POP3ProtocolHandlerChain extends ProtocolHandlerChainImpl { // add all pass handlers Collections.addAll(handlers, authHandlers); - handlers.add(new CapaCmdHandler()); - handlers.add(new UserCmdHandler()); - handlers.add(new ListCmdHandler()); - handlers.add(new UidlCmdHandler()); - handlers.add(new RsetCmdHandler()); - handlers.add(new DeleCmdHandler()); - handlers.add(new NoopCmdHandler()); - handlers.add(new RetrCmdHandler()); - handlers.add(new TopCmdHandler()); - handlers.add(new StatCmdHandler()); - handlers.add(new QuitCmdHandler()); + handlers.add(new CapaCmdHandler(metricFactory)); + handlers.add(new UserCmdHandler(metricFactory)); + handlers.add(new ListCmdHandler(metricFactory)); + handlers.add(new UidlCmdHandler(metricFactory)); + handlers.add(new RsetCmdHandler(metricFactory)); + handlers.add(new DeleCmdHandler(metricFactory)); + handlers.add(new NoopCmdHandler(metricFactory)); + handlers.add(new RetrCmdHandler(metricFactory)); + handlers.add(new TopCmdHandler(metricFactory)); + handlers.add(new StatCmdHandler(metricFactory)); + handlers.add(new QuitCmdHandler(metricFactory)); + handlers.add(new StlsCmdHandler(metricFactory)); handlers.add(new WelcomeMessageHandler()); handlers.add(new UnknownCmdHandler()); - handlers.add(new StlsCmdHandler()); handlers.add(new CommandDispatcher<POP3Session>()); handlers.add(new CommandHandlerResultLogger()); diff --git a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/core/RetrCmdHandlerTest.java b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/core/RetrCmdHandlerTest.java index 7ffb6d0..dd0ebac 100644 --- a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/core/RetrCmdHandlerTest.java +++ b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/core/RetrCmdHandlerTest.java @@ -26,6 +26,7 @@ import static org.mockito.Mockito.when; import java.util.Collections; import java.util.stream.Collectors; +import org.apache.james.metrics.tests.RecordingMetricFactory; import org.apache.james.protocols.api.Request; import org.apache.james.protocols.pop3.POP3Session; import org.junit.jupiter.api.Test; @@ -44,7 +45,7 @@ class RetrCmdHandlerTest { String overflowedNumber = Collections.nCopies(pad, "\\xff").stream().collect(Collectors.joining()); when(request.getArgument()).thenReturn(overflowedNumber); - assertThat(new RetrCmdHandler().onCommand(session, request)) + assertThat(new RetrCmdHandler(new RecordingMetricFactory()).onCommand(session, request)) .isEqualTo(RetrCmdHandler.SYNTAX_ERROR); } @@ -57,7 +58,7 @@ class RetrCmdHandlerTest { String overflowedNumber = Long.toString(Long.MAX_VALUE); when(request.getArgument()).thenReturn(overflowedNumber); - assertThat(new RetrCmdHandler().onCommand(session, request)) + assertThat(new RetrCmdHandler(new RecordingMetricFactory()).onCommand(session, request)) .isEqualTo(RetrCmdHandler.SYNTAX_ERROR); } } diff --git a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/utils/TestPassCmdHandler.java b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/utils/TestPassCmdHandler.java index fdaa284..b0baa42 100644 --- a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/utils/TestPassCmdHandler.java +++ b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/utils/TestPassCmdHandler.java @@ -22,13 +22,18 @@ import java.util.HashMap; import java.util.Map; import org.apache.james.core.Username; +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.protocols.pop3.POP3Session; import org.apache.james.protocols.pop3.core.AbstractPassCmdHandler; import org.apache.james.protocols.pop3.mailbox.Mailbox; public class TestPassCmdHandler extends AbstractPassCmdHandler { private final Map<String, Mailbox> mailboxes = new HashMap<>(); - + + public TestPassCmdHandler(MetricFactory metricFactory) { + super(metricFactory); + } + public void add(String username, Mailbox mailbox) { mailboxes.put(username, mailbox); } diff --git a/server/protocols/protocols-pop3/src/main/java/org/apache/james/pop3server/core/PassCmdHandler.java b/server/protocols/protocols-pop3/src/main/java/org/apache/james/pop3server/core/PassCmdHandler.java index dca994f..0e4fffe 100644 --- a/server/protocols/protocols-pop3/src/main/java/org/apache/james/pop3server/core/PassCmdHandler.java +++ b/server/protocols/protocols-pop3/src/main/java/org/apache/james/pop3server/core/PassCmdHandler.java @@ -32,6 +32,7 @@ import org.apache.james.mailbox.exception.BadCredentialsException; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.MailboxPath; +import org.apache.james.metrics.api.MetricFactory; import org.apache.james.pop3server.mailbox.MailboxAdapter; import org.apache.james.protocols.api.Request; import org.apache.james.protocols.api.Response; @@ -58,7 +59,8 @@ public class PassCmdHandler extends AbstractPassCmdHandler { private final MailboxManager manager; @Inject - public PassCmdHandler(@Named("mailboxmanager") MailboxManager manager) { + public PassCmdHandler(@Named("mailboxmanager") MailboxManager manager, MetricFactory metricFactory) { + super(metricFactory); this.manager = manager; } diff --git a/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3ServerTest.java b/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3ServerTest.java index 10120fa..0eaa19c 100644 --- a/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3ServerTest.java +++ b/server/protocols/protocols-pop3/src/test/java/org/apache/james/pop3server/POP3ServerTest.java @@ -41,6 +41,8 @@ import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.inmemory.manager.InMemoryIntegrationResources; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mailbox.store.StoreMailboxManager; +import org.apache.james.metrics.api.MetricFactory; +import org.apache.james.metrics.tests.RecordingMetricFactory; import org.apache.james.mime4j.dom.Message; import org.apache.james.pop3server.netty.POP3Server; import org.apache.james.protocols.api.utils.ProtocolServerUtils; @@ -738,6 +740,7 @@ class POP3ServerTest { .put(binder -> binder.bind(UsersRepository.class).toInstance(usersRepository)) .put(binder -> binder.bind(MailboxManager.class).annotatedWith(Names.named("mailboxmanager")).toInstance(mailboxManager)) .put(binder -> binder.bind(FileSystem.class).toInstance(fileSystem)) + .put(binder -> binder.bind(MetricFactory.class).toInstance(new RecordingMetricFactory())) .build(); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
